Android implementation of a quick and easy way to implement Night mode example detailed _android

Source: Internet
Author: User
Tags recyclerview android

Changemode

Project Address: Changemode

Implementation of Night mode for Android.

Implement night mode in the simplest way, support ListView, Recyclerview.

Preview

Usage XML

Android:background= "Attr/zzbackground"
app:backgroundattr= "zzbackground"//If the current page is to be refreshed immediately, pass the attribute name here like R.attr.zzbackground Chuan Zzbackground can
android:textcolor= "? Attr/zztextcolor"
app:textcolorattr= " Zztextcolor "///For Immediate Refresh page effect ditto

Java

@Override
protected void onCreate (Bundle savedinstancestate) {
//1. Call this method
on the page that you want to switch effects immediately Changemodecontroller.getinstance (). Init (This,r.attr.class). Settheme (This, r.style.daytheme, r.style.nighttheme);
Call this method on other pages 
//changemodecontroller.settheme (this, r.style.daytheme, r.style.nighttheme);
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
Add additional view to night management
//Changemodecontroller.getinstance (). Addbackgroundcolor (toolbar, r.attr.colorprimary);
Changemodecontroller.getinstance (). addbackgrounddrawable (view,r.attr.coloraccent);
Changemodecontroller.getinstance (). Addtextcolor (view,r.attr.coloraccent);
2. Set Toggle
//changemodecontroller.changeday (this, r.style.daytheme);
Changemodecontroller.changenight (this, r.style.nighttheme);
@Override
protected void OnDestroy () {
Super.ondestroy ();
3. Call
changemodecontroller.ondestory () in OnDestroy;

Detailed Operation description

First Step: Custom properties

<?xml version= "1.0" encoding= "Utf-8"?>
<resources> <attr name= "Zzbackground" format= "color|"
Reference "/> <attr name= zzbackgrounddrawable" format= "reference"/> <attr name= "
zztextcolor" format= "Color"/>
<attr name= "zzitembackground" format= "color"/>
</resources>

Step Two: Configure the nightly style file

<resources> <!--Base application theme. --> <style name= "Apptheme" parent= "Theme.AppCompat.Light.DarkActionBar" > <!--Customize your here. --> <item name= "colorprimary" > @color/colorprimary</item> <item name= "Colorprimarydark" > @color /colorprimarydark</item> <item name= "coloraccent" > @color/coloraccent</item> <item " Windowactionbar ">false</item> <item name=" Android:windownotitle ">true</item> <item name=" Windownotitle ">true</item> </style> <!--day mode--> <style name=" Daytheme "parent=" Apptheme " > <item name= "zzbackground" > @color/daybackground</item> <item name= "zzbackgrounddrawable" >@ drawable/ic_launcher</item> <item name= "Zztextcolor" > @color/daytextcolor</item> <item " Zzitembackground "> @color/dayitembackground</item> </style> <!--night mode--> <style name=" Nighttheme "parent=" Apptheme "> <item name="Zzbackground "> @color/nightbackground</item> <item name=" zzbackgrounddrawable "> @color nightbackground</item> <item name= "Zztextcolor" > @color/nighttextcolor</item> <item " Zzitembackground "> @color/nightitembackground</item> <item name=" Colorprimary "> @color colorprimarynight</item> <item name= "Colorprimarydark" > @color/colorprimarydarknight</item> < Item Name= "Coloraccent" > @color/coloraccentnight</item> </style> <style name= " Apptheme.appbaroverlay "parent=" ThemeOverlay.AppCompat.Dark.ActionBar "/> <style name=" Apptheme.popupoverlay "Parent=" ThemeOverlay.AppCompat.Light "/> </resources>

Set the attribute value for the corresponding pattern for the related property:

<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<color name= "Daybackground" > #F2F4F7 </color>
<color name= "Daytextcolor" > #000 </color>
<color name= "Dayitembackground" > #fff </color>
<color name= "Nightitembackground" > #37474F </color>
<color name= " Nightbackground "> #263238 </color>
<color name=" Nighttextcolor "> #fff </color>
</ Resources>

Step Three: Configure the use of corresponding properties in the layout file

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "android:layout_height = "Match_parent" xmlns:app= "Http://schemas.android.com/apk/res-auto" android:orientation= "Vertical" Android: Background= "Attr/zzbackground" app:backgroundattr= "Zzbackground" tools:context= " Com.thinkfreely.changemode.MainActivity "> <android.support.design.widget.appbarlayout android:layout_width = "Match_parent" android:layout_height= "wrap_content" app:theme= "@style/apptheme.appbaroverlay" > < Android.support.v7.widget.Toolbar android:id= "@+id/toolbar" android:layout_width= "Match_parent" Android:layout_ height= "Attr/actionbarsize" android:background= "Attr/colorprimary" app:backgroundattr= "ColorPrimary" app: Titletextcolor= "Attr/zztextcolor" app:popuptheme= "@style/apptheme.popupoverlay"/> </ Android.support.design.widget.appbarlayout> <button andRoid:layout_width= "Match_parent" android:layout_height= "120DP" android:gravity= "center" android:textcolor= "attr/"? Zztextcolor "app:textcolorattr=" Zztextcolor "android:background="? Attr/zzitembackground "App:backgroundAttr=" Zzitembackground "android:padding=" 10DP "android:layout_marginbottom=" 8DP "android:textsize=" 22SP "Android: Textallcaps= "false" Android:text= night mode switch by Mr.zk "/> <android.support.v7.widget.recyclerview android:id=" @+id /recyclerview "android:layout_width=" match_parent "android:layout_height=" Match_parent "android:scrollbars=" Vertical "/> </LinearLayout>

Note Textcolorattr, Backgroundattr, backgrounddrawableattr three properties. If you want the current page to refresh immediately, you need to fill in the appropriate attributes.

Attribute description

Textcolorattr to modify the font color settings. such as R.attr.zztextcolor Chuan Zztextcolor can be. Example: app:textcolorattr= "Zztextcolor"
Backgroundattr to modify background color/background picture settings. Ditto. Example: app:backgroundattr= "Zzbackground"
Backgrounddrawableattr to modify background color/background picture settings. Ditto. Example: app:backgrounddrawableattr= "Zzbackground"

Fourth Step: page invoke Java code

@Override
protected void onCreate (Bundle savedinstancestate) {
//1. Call this method
on the page that you want to switch effects immediately Changemodecontroller.getinstance (). Init (This,r.attr.class). Settheme (This, r.style.daytheme, r.style.nighttheme);
Call this method on other pages 
//changemodecontroller.settheme (this, r.style.daytheme, r.style.nighttheme);
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
2. Set switch Night Live daytime mode
//changemodecontroller.changeday (this, r.style.daytheme);//Switch Day mode
// Changemodecontroller.changenight (this, r.style.nighttheme)//Toggle Night Mode
}
@Override
protected void OnDestroy () {
Super.ondestroy ();
3. Call
changemodecontroller.ondestory () in OnDestroy;

The code calls three steps to start the night trip. If the page has a newly created view to join the night mode control, the code calls:

Add additional view to night management
//Changemodecontroller.getinstance (). Addbackgroundcolor (toolbar, r.attr.colorprimary);
Changemodecontroller.getinstance (). addbackgrounddrawable (view,r.attr.coloraccent);
Changemodecontroller.getinstance (). Addtextcolor (view,r.attr.coloraccent);

If there are other non-standard defined properties when you change the night mode, you can call the following code after Changemodecontroller.changeday or changemodecontroller.changenight to assign a value to the related property:

Typedvalue Attrtypedvalue = Changemodecontroller.getattrtypedvalue (this, r.attr.zztextcolor);
Toolbar.settitletextcolor (Getresources (). GetColor (Attrtypedvalue.resourceid));
About Me
An Android Developer in Zhengzhou.

License
======= Copyright 2016 Zhangke
Licensed under the Apache License, Version 2.0 (the "License"); You are not to use this file except in compliance with the License. You could obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 unless required by applicable or AG Reed to in writing, software distributed under the License was distributed on a ' as is ' basis, without warranties OR Condi tions of any KIND, either express or implied. The License for the specific language governing permissions and limitations under the License.

The above is a small set of Android to introduce the implementation of the night model of the fast simple Method example, I hope to help you, if you have any questions welcome to my message, small series will promptly reply to everyone, here also thank you for your support cloud Habitat community site!

Related Article

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.