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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -