mercredi 19 août 2015

Checkbox activity in action bar to change the color of textView in another activity

On my welcome page, there is an action bar with preferences as its only option. When clicked, it takes the user to another activity with three options that all have checkboxes. When one or two of the checkboxes are pressed, when the user goes back to the welcome page, the color of the textView should change as chosen. I believe I have coded the checkboxes properly, but I am unable to make the changes to reflect on the TextView. What can I do to solve that?

This is my onOptionsSelected from the window page,

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_preferences){
        startActivity(new Intent(Welcome.this,Preferences.class));
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

and this is my preferences page which has the code for all the checkboxes.

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class Preferences extends Activity {

CheckBox checkBox;
CheckBox checkBox2;
CheckBox checkBox3;
TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.preferences);
    checkBox=(CheckBox)findViewById(R.id.checkBox);
    checkBox2=(CheckBox)findViewById(R.id.checkBox2);
    checkBox3=(CheckBox)findViewById(R.id.checkBox3);

    if (checkBox.isChecked()){
        textView.setTextColor(Color.RED);
    }
    if(checkBox2.isChecked()){
        textView.setTextColor(Color.GREEN);
    }
    if(checkBox3.isChecked()){
        textView.setTextColor(Color.BLUE);
    }
    if (checkBox.isChecked()&&(checkBox2.isChecked())){
        textView.setTextColor(Color.YELLOW);
    }
    if (checkBox.isChecked()&&(checkBox3.isChecked())){
        textView.setTextColor(Color.MAGENTA);
    }

}
}

I need the checkboxes in the preferences page to change the textView on the welcome page. Please help!




Aucun commentaire:

Enregistrer un commentaire