java - How to change theme in app settings -
i working on app in user able pick list of color options in settings (all code shown below). when not change after settings activity. changes when leave activity(say home) , come back. believe because oncreate()
not being called, problem if setcontentview
() again whole app unresponsive. , if recreate()
crashes because of music player (again code below). please leave answer if think work. in advance.
code: settingsactivity.java (also cant bar change on one)
public class settingsactivity extends preferenceactivity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); sharedpreferences settings = preferencemanager.getdefaultsharedpreferences(getapplicationcontext()); string themename = settings.getstring("theme", "darkblue"); int theme = mythemer.getthemeid(themename); if(build.version.sdk_int > 20){ int themedark = mythemer.getcolorprimarydark(themename); getwindow().setstatusbarcolor(getresources().getcolor(themedark)); } settheme(theme); addpreferencesfromresource(r.layout.settings_activity); string themname = settings.getstring("theme", "darkblue"); int barcolor = mythemer.getcolorprimary(themname); linearlayout root = (linearlayout)findviewbyid(android.r.id.list).getparent().getparent().getparent(); toolbar bar = (toolbar) layoutinflater.from(this).inflate(r.layout.settings_toolbar, root, false); bar.setbackgroundcolor(getresources().getcolor(barcolor)); root.addview(bar, 0); // insert @ top bar.setnavigationonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { finish(); } }); } }
code: mainactivity.java
oncreate()
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); settings = preferencemanager.getdefaultsharedpreferences(getapplicationcontext()); mysettheme(); setcontentview(r.layout.activity_game_over);
settheme()
private void mysettheme(){ themename = settings.getstring("theme", "darkblue"); int theme = mythemer.getthemeid(themename); settheme(theme); if(build.version.sdk_int > 20){ int themedark = mythemer.getcolorprimarydark(themename); getwindow().setstatusbarcolor(getresources().getcolor(themedark)); } }
onresume()
public void onresume(){ super.onresume(); playmusic = settings.getboolean("playmusic", true); if(playmusic){ backgroundmusicplayer = new backgroundmusicplayer(); backgroundmusicplayer.execute(getapplicationcontext()); } }
onpause()
public void onpause(){ super.onpause(); if(playmusic){ backgroundmusicplayer.cancelmusic(); backgroundmusicplayer.cancel(true); } }
backgroundmusicplayer.java
public class backgroundmusicplayer extends asynctask<context, void, void>{ mediaplayer player; @override protected void doinbackground(context... params) { player = mediaplayer.create(params[0], r.raw.background); player.setlooping(true); player.setvolume(50,50); player.start(); return null; } public void cancelmusic(){ player.stop(); player.reset(); player.release(); } }
just had replace finish();
intent home = new intent(getapplicationcontext(), mainactivity.class); startactivity(home);
thank reading question.
Comments
Post a Comment