I am still having problem with this radio buttons :(. I am still trying to create a setting page which allow user to select their text colour and once they have selected from this setting activity and all other activities with listen to the sharedpreferences change as well.
I know how to save text and get text from a shared preferences between activities but int ? As I need the radio checked value and this method will be passed on to other activities to set those textview colours.
can anyone check the code for me please?
and I am very new to android and general programming.
public class Text_Colour extends Activity implements OnSharedPreferenceChangeListener {
RadioButton rb1, rb2, rb3, rb4, rb5;
TextView tv1, tv2;
RadioGroup rg1;
Button bt1;
String red, yellow, green, blue;
public static final String PREFS_NAME = "MyPrefsFile";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.colours);
loadPreferences();
tv1 = (TextView)findViewById(R.id.textview1);
tv2 = (TextView)findViewById(R.id.textview2);
bt1 = (Button) findViewById(R.id.button1);
rb1 = (RadioButton) findViewById(R.id.radioButton1);
rb2 = (RadioButton) findViewById(R.id.radioButton2);
rb3 = (RadioButton) findViewById(R.id.radioButton3);
rb4 = (RadioButton) findViewById(R.id.radioButton4);
rb5 = (RadioButton) findViewById(R.id.radioButton5);
rg1 = (RadioGroup) findViewById(R.id.radiogroup1);
bt1.setOnClickListener(Colour_change_b);
//tv1.setOnSharedPreferenceChangeListener(this);
}
View.OnClickListener Colour_change_b = new View.OnClickListener() {
public void onClick(View v) {
if(v == bt1) {
if (rb1.isChecked()== true) {
tv2.setTextColor(Color.RED);
}
if (rb2.isChecked() == true) {
tv2.setTextColor(Color.YELLOW);
}
if (rb3.isChecked() == true) {
tv2.setTextColor(Color.GREEN);
}
if (rb4.isChecked() == true) {
tv2.setTextColor(Color.BLUE);
}
if (rb5.isChecked() == true) {
rb5.getId();
tv2.setTextColor(Color.BLACK);
}
else {
}
SharedPreferences settings1 = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings1.edit();
editor.putInt("colour", rb1.getId());
editor.commit();
finish();
}
}
};
private void loadPreferences() {
// TODO Auto-generated method stub
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
tv1.setTextColor(settings.getId("colour", ""));
settings.registerOnSharedPreferenceChangeListener(Text_Colour.this);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
loadPreferences();
}
}
Much appreciated for your time guys.
Use your
RadioGroup. You can usesetOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener listener)to receive instantly the value of the new color, and you can usegetCheckedRadioButtonId()to get the checked radioButtonId.However, there is a catch. The Id is generated at compile time by android. It is not guaranteed to remain consistent between compilations. Use
indexOfChild(View child)if you want to get the index of the RadioButton in the RadioGroup.To get the value, you need a
getInt(), then agetChildAt(int index), then agetId()and acheck(int id).