Android Night Mode implementation

Source: Internet
Author: User

View all my open source projects "open Source Labs"
Welcome to join my QQ group: "201055521", this blog client download "please click"
This article original, reprint please specify address: http://blog.kymjs.com/

Recently in the function of doing an Android night mode, and re-examined the theme mechanism. Implement my style, to solve the problem as the goal, wrote a small demo, simple to achieve the switch night mode, for everyone to explain.
In activity there is a method called Settheme (), you can set the activity of the theme, of course, application class also has the same method, you can also set the current application of theme in application. It's like we can set the theme in the manifest file by Android:theme= "".

Create a property name

Create a new XML file under the project catalog res/values/, the name can be customized, here my name Theme_attr.xml content is as follows:

    <?xml version="1.0" encoding="utf-8"?>    <resources>        <attr name="text_bg" format="reference|color"/>        <attr name="bottom_bg" format="reference|color"/>    </resources>

Where TEXT_BG and BOTTOM_BG are the two attribute names we define, and the following format refers to the type of the property name, where I use Reference|color to represent either a reference or a ARGB color value. In addition to these you can also choose data types such as int,boolan,string.

Defining Resource Styles

After you create the property name, you need to create a theme style. Still create a new XML file under Res/values/, or it can be written in the original Style.xml, is the same. Here for the project structure is clear, write a separate file called Theme_dark.xml we need to define here the value of each property name defined in step one under the current topic.

    <resources>        <!--Base application theme.        <style name="Darktheme" parent="@android: Style/theme"> <item name= "text_bg" >@drawable/selector_text_bg_dark</item> <item name="BOTTOM_BG"> @drawable/bg_bottombar_dark</item> </style>    </Resources>

As you can see here, because I have a property type that defines a reference or a color, I can either pass a reference to the @drawable type or directly assign a color value such as a #ff00ff value.

Use in layouts

When using our custom files in a layout file, you need to add a question mark to represent the reference. For example

    <linearlayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  android:layout_width  =" match_parent " android:layout_height  = "match_parent"   Android:orientation  =;           <buttonandroid:id="@+id/login_btn"android:layout_width="Match_ Parent "android:layout_height=" Wrap_content "android:background="? Text_bg "  Android:text="Login"/>                                                                </linearlayout>

This means that the background of our login button is using the previously defined property name TEXT_BG under the current topic, which is the @drawable/selector_text_bg_dark image (actually a selector) defined in step two.

Used in code

As a simple example, it is important to note that the Settheme () method must be called before the activity's Setcontentview call. There are a lot of introductions on the internet saying that before super, this is actually not accurate. When we switch themes dynamically, for example by clicking on a button, we need to manually call the Setcontentview () method again, and recall the control initialization and listener settings. But here's an easy way to do this: call the recreate () method directly and create the view again.

    public class UserInfor extends KJActivity {        @BindView(id = R.id.logout_btn, click = true)        private Button mBtnLogout;        @Override        public void setRootView() {            setContentView(R.layout.activity_userinfor);        }        @Override        public void widgetClick(View v) {            super.widgetClick(v);            switch (v.getId()) {                case R.id.logout_btn:                    setTheme(R.style.DarkTheme);                    recreate();                    break;            }        }    }

Android Night Mode implementation

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.