I have two different activities. One’s view contains RadioButtons that give the user choice to select color, then when the user draws on the Canvas,the color selected in the options activity is used to draw.
Here’s the code for the Options Activity:
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
int checkedRadioButton = radioGroup.getCheckedRadioButtonId();
String radioButtonSelected = "";
switch (checkedRadioButton) {
case R.id.CheckRed : radioButtonSelected = "Red";
break;
case R.id.CheckBlue : radioButtonSelected = "Blue";
break;
case R.id.CheckWhite : radioButtonSelected = "White";
break;
}
SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
Editor prefsEditor = appSharedPrefs.edit();
prefsEditor.putString("setColor", radioButtonSelected);
prefsEditor.commit();
Here’s the xml for this Activity:
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="@+id/CheckWhite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="White" />
<RadioButton
android:id="@+id/CheckRed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red" />
<RadioButton
android:id="@+id/CheckBlue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue" />
</RadioGroup>
Notice that CheckWhite has android:checked="true". I want that to be true for whichever color the user selects later on.
Here’s the code for the Activity that does the drawing:
SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
drawColor = appSharedPrefs.getString("setColor", "White");
if(drawColor.equals("White"))
intColor = 1;
if(drawColor.equals("Red"))
intColor = 2;
if(drawColor.equals("Blue"))
intColor = 3;
mPaint = new Paint();
if(intColor == 1)
mPaint.setColor(Color.WHITE);
if(intColor == 2)
mPaint.setColor(Color.RED);
if(intColor == 3)
mPaint.setColor(Color.BLUE);
Unfortunately,the color doesn’t change. Kindly help!
Add a
OnCheckedChangedListenerto yourRadioGroupand in the methodonCheckedChanged(RadioGroup group, int checkedId)put the code that sets the preferences for the user color. Some code:EDIT:
Remove the
android:checked="true"from the xml layout (from the whiteRadioButon) and then in your Options screen set the checkedRadioButtonaccording to your preferences: