Implementation of Android Night mode

Source: Internet
Author: User

Package Com.loaderman.daynightdemo;import Android.os.bundle;import android.support.v7.app.AppCompatActivity; Import Android.support.v7.app.appcompatdelegate;import Android.support.v7.widget.toolbar;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 (R.id.toolbar);        Setsupportactionbar (toolbar);        Findviewbyid (R.id.btn_auto). Setonclicklistener (this);        Findviewbyid (R.id.btn_day). Setonclicklistener (this);        Findviewbyid (r.id.btn_night). Setonclicklistener (this);    Findviewbyid (R.id.fab). Setonclicklistener (this); } @Override public void OnClick (View v) {switch (V.getid ()) {R.ID.BTN_auto: {getdelegate (). Setlocalnightmode (Appcompatdelegate.mode_night_auto);                Recreate ();            Break                } case R.id.btn_day: {getdelegate (). Setlocalnightmode (Appcompatdelegate.mode_night_no);                Recreate ();            Break                } case r.id.btn_night: {getdelegate (). Setlocalnightmode (Appcompatdelegate.mode_night_yes);                Recreate ();            Break }        }    }}

 activity_main.xml

<?xml version= "1.0" encoding= "Utf-8"? ><android.support.design.widget.coordinatorlayout xmlns:android= " Http://schemas.android.com/apk/res/android "xmlns:app=" Http://schemas.android.com/apk/res-auto "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Androi D:fitssystemwindows= "true" tools:context= "com.yanzhenjie.daynight.MainActivity" > < Android.support.design.widget.AppBarLayout android:layout_width= "match_parent" android:layout_height= "Wrap_c Ontent "Android:theme=" @style/apptheme.appbaroverlay "> <android.support.v7.widget.toolbar 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;/android.support.design.widget.appbarlayout> <include Layout= "@layout/content_main"/> <android.support.design.widget.floatingactionbutton 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 Droid.support.design.widget.coordinatorlayout>

 content_main.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:app=" Http://schemas.android.com/apk/res-auto "xmlns:tools=" Http://schemas.android.com/tools "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>

Style.xml

<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>

Androidmainfest.xml

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ 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: 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.