I am building an app with three toggle buttons. When each is pressed, it changes the textView in the same activity to three different colors. When two toggle buttons are pressed together, it changes the same textview to other colors, and when all three are pressed together, it changes the textview to white. How can i do this? i tried using switch statement, and if (isChecked), but it had errors
This is my MainActivity.java file
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
ToggleButton redtoggleButton;
ToggleButton bluetoggleButton;
ToggleButton greentoggleButton;
TextView june;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
redtoggleButton=(ToggleButton)findViewById(R.id.toggleButton);
bluetoggleButton=(ToggleButton)findViewById(R.id.toggleButton2);
greentoggleButton=(ToggleButton)findViewById(R.id.toggleButton3);
june=(TextView)findViewById(R.id.textView);
if (redtoggleButton.isChecked()){
june.setTextColor(Color.RED);
}else if (bluetoggleButton.isChecked()){
june.setTextColor(Color.BLUE);
}else if (greentoggleButton.isChecked()){
june.setTextColor(Color.GREEN);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and my activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="June 2015"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:textSize="50dp" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton"
android:layout_gravity="center_horizontal"
android:textOff="Off"
android:textOn="On" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton2"
android:layout_gravity="center_horizontal"
android:textOff="Off"
android:textOn="On"/>
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton3"
android:layout_gravity="center_horizontal"
android:textOff="Off"
android:textOn="On"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:minWidth="150dp"
android:textSize="20dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:minHeight="60dp"
android:layout_marginTop="150dp">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Top Rated"
android:id="@+id/button2"
android:minWidth="160dp" />
<Button
android:layout_width="137dp"
android:layout_height="match_parent"
android:text="Random"
android:id="@+id/button3"
android:layout_marginLeft="25dp"
android:minWidth="160dp" />
</LinearLayout>
</LinearLayout>
please help any help would be appreciated!
Replace your whole code with this:
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
ToggleButton redtoggleButton;
ToggleButton bluetoggleButton;
ToggleButton greentoggleButton;
TextView june;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
redtoggleButton = (ToggleButton) findViewById(R.id.toggleButton);
bluetoggleButton = (ToggleButton) findViewById(R.id.toggleButton2);
greentoggleButton = (ToggleButton) findViewById(R.id.toggleButton3);
june = (TextView) findViewById(R.id.textView);
redtoggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
changeTextColor();
}
});
bluetoggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
changeTextColor();
}
});
greentoggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
changeTextColor();
}
});
}
@Override
protected void onResume() {
super.onResume();
changeTextColor();
}
public void changeTextColor() {
boolean redToggle = redtoggleButton.isChecked();
boolean greenToggle = greentoggleButton.isChecked();
boolean blueToggle = bluetoggleButton.isChecked();
int redValue;
int greenValue;
int blueValue;
if (redToggle) {
redValue = 255;
} else {
redValue = 0;
}
if (greenToggle) {
greenValue = 255;
} else {
greenValue = 0;
}
if (blueToggle) {
blueValue = 255;
} else {
blueValue = 0;
}
june.setTextColor(Color.rgb(redValue, greenValue, blueValue));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}