java - Changing mainactivity inner layout background from preference activity in android -
i have preferences activity. activity want able change background color of 1 of layouts in main activity.
right when try run settings activity "if" loops carry change in background color, app crashes.
the click listener opens settings activity:
settings.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent intentpreferences = new intent(getapplicationcontext(), settingsactivity.class); startactivity(intentpreferences); //startactivityforresult(intentpreferences); } });
the preferencescreen layout:
<?xml version="1.0" encoding="utf-8"?> <preferencescreen xmlns:android="http://schemas.android.com/apk/res/android"> <switchpreference android:key="pref_sync" android:id="@+id/prefswitch" android:defaultvalue="false" android:title="you want yellow or white background?" /> </preferencescreen>
my preferences activity:
public class settingsactivity extends preferenceactivity { private static final boolean always_simple_prefs = false; private switchpreference settingsswitch; @suppresswarnings("deprecation") @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.xml.preferences); preferencemanager mpreferencemanager = getpreferencemanager(); relativelayout rl = (relativelayout) findviewbyid(r.id.newdeliverylayout); if (mpreferencemanager.getsharedpreferences().getboolean("pref_sync", true)){ rl.setbackgroundcolor(color.yellow); } else{ rl.setbackgroundcolor(color.white); } } @override protected void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); } }
the preferences screen opens if comment out rl.setbackgroundcolor lines in if loop
you each time main activity loads checks shared preferences , loads color ever in preferences. if preferences null set default color?
so example on can set color this. (ive put hex color reference can change whatever wish.
public void setbackgroundcolor() { sharedpreferences sharedpreferences = getpreferences(context.mode_private); sharedpreferences.editor editor = sharedpreferences.edit(); editor.putstring("color", "#ffffff"); editor.commit() }
then in main activity make sure call method every time loads / reloads
public void getbackgroundcolor() { sharedpreferences sharedpreferences = getpreferences(context.mode_private); if (sharedpreferences.contains("color")) { string mycolor = sharedpreferences.getstring("color", null); mybackground.setbackgroundcolor(color.parsecolor(mycolor)); } }
Comments
Post a Comment