// my program is working fine no error, also color works fine when changing toolbars color. but sharedpreferences is not working for color , it is not able to save the changed color. after changing the color of toolbar when i restart app again after closing it. it start with default color, i am not able to save by using shared preferences.//
public class MainActivity extends AppCompatActivity {
Toolbar mtoolbar;
Button mredcolour;
Button mgreencolour;
Button myellowcolour;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mtoolbar =(Toolbar) findViewById(R.id.toolbar2);
mredcolour=(Button) findViewById(R.id.button);
mgreencolour=(Button) findViewById(R.id.button2);
myellowcolour=(Button) findViewById(R.id.button3);
if(getcolour()!=getResources().getColor(R.color.colorPrimary)){
mtoolbar.setBackgroundColor(getcolour());
getWindow().setStatusBarColor(getcolour() );
}
mredcolour.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
mtoolbar.setBackgroundColor(getResources().getColor(R.color.colorred));
getWindow().setStatusBarColor(getResources().getColor(R.color.colorred));
storecoloru(getResources().getColor(R.color.colorred));
}
});
mgreencolour.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
mtoolbar.setBackgroundColor(getResources().getColor(R.color.colorgreen));
getWindow().setStatusBarColor(getResources().getColor(R.color.colorgreen));
storecoloru(getResources().getColor(R.color.colorgreen));
}
});
myellowcolour.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
mtoolbar.setBackgroundColor(getResources().getColor(R.color.coloryellow));
getWindow().setStatusBarColor(getResources().getColor(R.color.coloryellow));
storecoloru(getResources().getColor(R.color.coloryellow));
}
});
}
private void storecoloru(int color){
SharedPreferences msharedpreferences =getSharedPreferences("toolbarcolour",MODE_PRIVATE);
SharedPreferences.Editor meditor=msharedpreferences.edit();
meditor.putInt("color",color );
meditor.apply();
}
private int getcolour(){
SharedPreferences msharedpreferences= getSharedPreferences("toolbarcolour",MODE_PRIVATE );
int selectedcolour=msharedpreferences.getInt("colour",getResources().getColor(R.color.colorPrimary) );
return selectedcolour;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar2"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="0dp"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="26dp"
tools:layout_editor_absoluteY="287dp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="362dp" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="464dp" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="colorred">#ff0000</color>
<color name="colorgreen">#008000</color>
<color name="coloryellow">#ffff00</color>
</resources>
You are using a different key to get the color
int selectedcolour=msharedpreferences.getInt("colour",getResources().getColor(R.color.colorPrimary) );
You are using color
when saving to shared preferences and colour
when retrieving. If this is a mistake when typing the code in stackoverflow then we will need more information for solving your problem.