android - Theme does not toggle -


i trying toggle between themes. looked honeycombgallery sample code , found out code toggle between themes. not working in application.

code:

mainactivity.java

public class mainactivity extends activity {  boolean success = true;     private int mthemeid = -1; button btnviewlib; button btnlog;   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);      if(savedinstancestate != null && savedinstancestate.getint("theme", -1) != -1) {         mthemeid = savedinstancestate.getint("theme");         this.settheme(mthemeid);     }       setcontentview(r.layout.activity_main);      btnviewlib = (button) findviewbyid(r.id.btnlib);     btnlog = (button) findviewbyid(r.id.btnlog);       btnviewlib.setonclicklistener(new view.onclicklistener() {          @override         public void onclick(view view) {             // launching products activity             intent = new intent(getapplicationcontext(), libactivity.class);             startactivity(i);          }     });      btnlog.setonclicklistener(new view.onclicklistener() {          @override         public void onclick(view view) {             // launching products activity             intent = new intent(getapplicationcontext(), someactivity.class);             startactivity(i);          }     });  }   @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.main, menu);     return true; }  @override public boolean onoptionsitemselected(menuitem item) {     switch (item.getitemid()) {      case r.id.toggletheme:         if (mthemeid == r.style.apptheme_dark) {             mthemeid = r.style.apptheme_light;         } else {             mthemeid = r.style.apptheme_dark;         }         this.recreate();         return true;      default:         return super.onoptionsitemselected(item);     } } } 

main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >  <item     android:id="@+id/action_settings"     android:orderincategory="100"     android:showasaction="never"     android:title="@string/action_settings"/>    <!-- example of items in overflow menu -->   <item android:id="@+id/toggletheme"     android:title="@string/theme_toggle"     android:showasaction="never" />   </menu> 

styles.xml

<resources> <style name="actionbar" parent="@android:style/widget.holo.actionbar" />  <style name="actionbar.light" parent="@style/actionbar">     <item name="android:background">@color/actionbar_background_light</item> </style>  <style name="actionbar.dark" parent="@style/actionbar">     <item name="android:background">@color/actionbar_background_dark</item> </style>  <style name="apptheme.light" parent="@android:style/theme.holo.light">     <item name="android:actionbarstyle">@style/actionbar.light</item>     <item name="android:windowactionbaroverlay">true</item>     <item name="listdragshadowbackground">@android:color/background_light</item>     <item name="menuiconcamera">@drawable/ic_menu_camera_holo_light</item>     <item name="menuicontoggle">@drawable/ic_menu_toggle_holo_light</item>     <item name="menuiconshare">@drawable/ic_menu_share_holo_light</item> </style>  <style name="apptheme.dark" parent="@android:style/theme.holo">     <item name="android:actionbarstyle">@style/actionbar.dark</item>     <item name="android:windowactionbaroverlay">true</item>     <item name="listdragshadowbackground">@android:color/background_dark</item>     <item name="menuiconcamera">@drawable/ic_menu_camera_holo_dark</item>     <item name="menuicontoggle">@drawable/ic_menu_toggle_holo_dark</item>     <item name="menuiconshare">@drawable/ic_menu_share_holo_dark</item> </style> </resources> 

theme should set before super.oncreate(savedinstancestate); call.

edit:

and if want savedinstancestate, should first save in there. need override onsaveinstancestate:

@override public void onsaveinstancestate(bundle savedinstancestate) {   super.onsaveinstancestate(savedinstancestate);    savedinstancestate.putint("theme", mthemeid );  } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -