Implementation of Android Night mode

Source: Internet
Author: User

Package Com.loaderman.daynightdemo;import Android.os.bundle;import; Import;import;import Android.view.View ;p ublic class Mainactivity extends Appcompatactivity implements View.onclicklistener {{appcompatdelegate.setde    Faultnightmode (Appcompatdelegate.mode_night_auto);        } @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Toolbar Toolbar = (Toolbar) Findviewbyid (;        Setsupportactionbar (toolbar);        Findviewbyid ( Setonclicklistener (this);        Findviewbyid ( Setonclicklistener (this);        Findviewbyid ( Setonclicklistener (this);    Findviewbyid ( Setonclicklistener (this); } @Override public void OnClick (View v) {switch (V.getid ()) {R.ID.BTN_auto: {getdelegate (). Setlocalnightmode (Appcompatdelegate.mode_night_auto);                Recreate ();            Break                } case {getdelegate (). Setlocalnightmode (Appcompatdelegate.mode_night_no);                Recreate ();            Break                } case {getdelegate (). Setlocalnightmode (Appcompatdelegate.mode_night_yes);                Recreate ();            Break }        }    }}


<?xml version= "1.0" encoding= "Utf-8"? >< xmlns:android= " Http:// "xmlns:app=" Http:// "xmlns:tools=" Http:// "android:layout_width=" match_parent "android:layout_height=" Match_parent "Androi D:fitssystemwindows= "true" tools:context= "com.yanzhenjie.daynight.MainActivity" > < android:layout_width= "match_parent" android:layout_height= "Wrap_c Ontent "Android:theme=" @style/apptheme.appbaroverlay "> < A Ndroid:id= "@+id/toolbar" android:layout_width= "match_parent" android:layout_height= "? Attr/actionBarS Ize "android:background="? Attr/colorprimary "App:popuptheme=" @style/apptheme.popupoverlay "/> & Lt;/> <include Layout= "@layout/content_main"/> < android:id= "@+id/fab" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "Botto M|end "android:layout_margin=" @dimen/fab_margin "android:src=" @android:d rawable/ic_dialog_email "/></an>


<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= " Android "xmlns:app=" Http:// "xmlns:tools=" Http:// "Andro Id:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" Android:paddin gbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" app:layout _behavior= "@string/appbar_scrolling_view_behavior" tools:context= "com.yanzhenjie.daynight.MainActivity" Tools: showin= "@layout/activity_main" > <button android:id= "@+id/btn_auto" android:layout_width= "Match_pare NT "android:layout_height=" Wrap_content "android:text=" Auto mode "/> <button android:id=" @+id/btn       _day "android:layout_width=" Match_parent " android:layout_height= "Wrap_content" android:text= "daytime mode"/> <button android:id= "@+id/btn_night" Android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:text= "Night Mode"/></ Linearlayout>


<resources>    <style name= "Apptheme" parent= "Theme.AppCompat.DayNight.DarkActionBar" >        <item Name= "Colorprimary" > @color/colorprimary</item>        <item name= "Colorprimarydark" > @color/ colorprimarydark</item>        <item name= "coloraccent" > @color/coloraccent</item>    </style >    <style name= "Apptheme.noactionbar" >        <item name= "Windowactionbar" >false</item>        <item name= "Windownotitle" >true</item>    </style>    <style name= " Apptheme.appbaroverlay "parent=" ThemeOverlay.AppCompat.Dark.ActionBar "/>    <style name=" Apptheme.popupoverlay "parent=" ThemeOverlay.AppCompat.Light "/></resources>


<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= " Android "    package=" Com.loaderman.daynightdemo ">    <application        android:allowbackup=" true "        android:icon= "@mipmap/ic_launcher"        android:label= "@string/app_name"        android:supportsrtl= "true"        Android:theme= "@style/apptheme" >        <activity            android:name= ". Mainactivity "            android:label=" @string/app_name "            android:theme=" @style/apptheme.noactionbar ">            <intent-filter>                <action android:name= "Android.intent.action.MAIN"/>                <category android: Name= "Android.intent.category.LAUNCHER"/>            </intent-filter>        </activity>    </ Application></manifest>


Implementation of Android 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: 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.