I am trying to change the background colour of a activity where the user can change the colour using 3 radio buttons in an android app. I am doing this using sharedPreferences.
So I have the activity page where the colour is chosen with the radio group and the code to use sharedPrefernces looks like this (i know the switch statement works aswell as I have a toast message apperring when the colour is supposed to change):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
SharedPreferences prefs = getSharedPreferences("bgColour", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
String colourSelected = "";
switch (checkedId) {
case R.id.radioButton1 :
colourSelected = "YELLOW";
editor.putString("colour", colourSelected);
editor.commit();
break;
case R.id.radioButton2 :
colourSelected = "YELLOW";
editor.putString("colour", colourSelected);
editor.commit();
break;
case R.id.radioButton3 :
colourSelected = "BLUE";
editor.putString("colour", colourSelected);
editor.commit();
break;
}
}
});
}
The XML looks like this
<LinearLayout 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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.practical3_10327751_donnacha_holmes.Preferences" >
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/radiogroup"
>
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yellow" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue" />
</RadioGroup>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back" />
Then there is the activity that changes the background colour, which looks like this when trying to change the colour:
public class Activity2 extends ActionBarActivity {
RelativeLayout rl;
String colour;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity2);
SharedPreferences prefs = getSharedPreferences("bgColour", MODE_PRIVATE);
colour = prefs.getString("Colour", "WHITE");
rl = (RelativeLayout)findViewById(R.id.RelativeLayout);
if(colour=="GREEN"){
rl.setBackgroundColor(Color.GREEN);
}
else if(colour=="YELLOW"){
rl.setBackgroundColor(Color.YELLOW);
}
else if(colour=="BLUE"){
rl.setBackgroundColor(Color.BLUE);
}
else{
rl.setBackgroundColor(Color.RED);
}
I know that the background colour is being changed because it is being set to red every time I go to this page. Thanks for any help!
The ==
operator checks to see if the two strings are exactly the same object.
The .equals()
method will check if the two strings have the same value.
Therefore to compare Strings use .equals()
,i.e. rewrite your comparison as
if(colour.equals("GREEN")){
rl.setBackgroundColor(Color.GREEN);
}
else if(colour.equals("YELLOW")){
rl.setBackgroundColor(Color.YELLOW);
}
else if(colour.equals("BLUE")){
rl.setBackgroundColor(Color.BLUE);
}
else{
rl.setBackgroundColor(Color.RED);
}
and you are saving value as colour and try to retrieving as Colour,so change
colour = prefs.getString("Colour", "WHITE");
to
colour = prefs.getString("colour", "WHITE");