Android switch app style, night mode

Source: Internet
Author: User

Add the Sharedpreference flag to remember the last style you chose, so you don't have to reset it the next time you start.

Package Com.zms.nightstyle;import Android.app.activity;import Android.content.context;import Android.content.sharedpreferences;import Android.content.sharedpreferences.editor;import Android.os.Bundle;    Import Android.view.view;import Android.widget.button;import Android.widget.toast;public class Main extends Activity {    Private Boolean isnight = false;    Private Button Btnset;    Private Button Btnget;    Private Sharedpreferences sharedpreferences;        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Sharedpreferences = getsharedpreferences ("Usestyle", context.mode_world_readable);        Isnight = Sharedpreferences.getboolean ("Isnight", false);        if (isnight) {this.settheme (r.style.mythemenight);        } else {this.settheme (r.style.mythemedefault);        } setcontentview (R.layout.main);        Btnset = (Button) Findviewbyid (R.id.btnset); Btnget = (Button) Findviewbyid (r.id.btnGet);        Btnset.setonclicklistener (New Onclicklistenerimp ());    Btnget.setonclicklistener (New Onclicklistenerimp ());            } class Onclicklistenerimp implements View.onclicklistener {@Override public void OnClick (View v) {                if (v = = btnset) {Editor editor = Sharedpreferences.edit ();                    if (isnight) {setTheme (R.style.mythemedefault);                Isnight = false;                    } else {setTheme (r.style.mythemenight);                Isnight = true;                } editor.putboolean ("Isnight", isnight);                Editor.commit ();                Setcontentview (R.layout.main);                Btnset = (Button) Findviewbyid (R.id.btnset);                Btnget = (Button) Findviewbyid (r.id.btnget);                Btnset.setonclicklistener (New Onclicklistenerimp ());            Btnget.setonclicklistener (New Onclicklistenerimp ()); } else if (v = = BtngeT) {Toast.maketext (Main.this, "isnight:" + isnight, Toast.length_short). Show (); }        }    }}

Two styles of themes:

<?xml version= "1.0" encoding= "Utf-8"?><resources>    <!--default Theme--    <style name= " Mythemedefault "parent=" @android: Style/theme ">        <item name=" Btncolor "> #00ff00 </item>        < Item Name= "Mainbackground" > #ffffff </item>        <item name= "Maintextcolor" > #000000 </item>        <item name= "TextString" > Default theme </item>    </style>    <!--night Theme--    <style name = "Mythemenight" parent= "@android: Style/theme" >        <item name= "Btncolor" > #0000ff </item>        <item name= "Mainbackground" > #000000 </item>        <item name= "Maintextcolor" > #ffffff </item >        <item name= "TextString" > Night theme </item>    </style></resources>

Layout file:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:background="? MainBackgrou nd "android:orientation=" vertical "> <textview android:layout_width=" fill_parent "Android:layout_ height= "Wrap_content" android:text= "TextString"/> <imageview android:id= "@+id/ivbook" Android Oid:layout_width= "62dip" android:layout_height= "42dip" android:layout_gravity= "center" android:layout _margintop= "0dip" android:gravity= "center" android:src= "? Btncolor"/> <button android:id= "@+ Id/btnset "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layout_ma rgintop= "50DP" android:text= "Change Theme"/> <button android:id= "@+id/btnget" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_margintop= "50DP" android:text= "Get Flag"/></linearlayout> 

Reprint Please specify source: Zhou Mushi's CSDN blogHttp://blog.csdn.net/zhoumushui 
my github: Zhou Mushi's GitHubHttps://github.com/zhoumushui

Android switch app style, night mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.