preferences - Android How to update Background color from PreferenceList directly -
i have listpreference containing colors. when restart application colors being changed, not directly. how make change directly when click ?
public class preferences extends preferenceactivity { sharedpreferences sharedpreferences; sharedpreferences sharedpreferences2; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.layout.preferences); sharedpreferences2 = preferencemanager .getdefaultsharedpreferences(getbasecontext()); listpreference listpreference = (listpreference) findpreference("color"); charsequence entry = listpreference.getentry(); final string value = listpreference.getvalue(); final listview rel = this.getlistview(); if (sharedpreferences2.contains("color")) { if (value.tostring().equals("red")) { rel.setbackgroundcolor(color.red); } if (value.tostring().equals("blue")) { rel.setbackgroundcolor(color.blue); } if (value.tostring().equalsignorecase("green")) { rel.setbackgroundcolor(color.green); } } }
you need onsharedpreferencechangelistener. use registeronsharedpreferencechangelistener()
in onresume()
, override onsharedpreferencechanged()
method perform actions on preference change.
unregister listener in onpause()
method.
p.s. preferenceactivity
deprecated, consider using preferencefragment instead.
Comments
Post a Comment