English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Three Lines of Android Code to Realize Smooth Switching Between Day and Night Modes

Usage xml android:background= &63;attr/zzbackground app:backgroundAttr= zzbackground //If the current page needs to refresh immediately, pass the attribute name here, for example, R.attr.zzbackground, pass zzbackground android:textColor= &63;attr/zztextColor app:textColorAttr= zztextColor // 

Demonstration effect

 

Usage xml    

android:background="&#"63;attr/zzbackground"
 app:backgroundAttr="zzbackground"//If the current page needs to refresh immediately, pass the attribute name here, for example, R.attr.zzbackground, pass zzbackground 
 android:textColor="&#"63;attr/zztextColor"
 app:textColorAttr="zztextColor"//If you need to refresh the page effect immediately, do the same as above 

java

 @Override
 protected void onCreate(Bundle savedInstanceState) {
   // Call this method on the page where 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 mode management
  // ChangeModeController.getInstance().addBackgroundColor(toolbar, R.attr.colorPrimary);
  //ChangeModeController.getInstance().addBackgroundDrawable(view, R.attr.colorAccent);
  // ChangeModeController.getInstance().addTextColor(view, R.attr.colorAccent);
  // Set switch
  //ChangeModeController.changeDay(this, R.style.DayTheme);
  //ChangeModeController.changeNight(this, R.style.NightTheme);
 }
 @Override
 protected void onDestroy() {
  super.onDestroy();
  // Called in onDestroy
  ChangeModeController.onDestory();
 } 

Detailed operation description

Step 1:Custom attributes

 <?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 2:Configure the night style file 

resources
 <!-- Base application theme. -->
 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  <!-- Customize your theme here. -->
  <item name="colorPrimary">@color/colorPrimary</item>
  <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  <item name="colorAccent">@color/colorAccent</item>
  <item name="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 name="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 name="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 corresponding attribute value for the corresponding mode for the related attributes: 

<?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 3:Configure the corresponding attribute 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="&#"63;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=""63;attr/actionBarSize"
   android:background="&#"63;attr/colorPrimary"
   app:backgroundAttr="colorPrimary"
   app:titleTextColor="&#"63;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="&#"63;attr/zztextColor"
   app:textColorAttr="zztextColor"
   android:background="&#"63;attr/zzItemBackground"
   app:backgroundAttr="zzItemBackground"
   android:padding=""10dp"
   android:layout_marginBottom=""8dp"
   android:textSize=""22sp"
   android:textAllCaps="false"
   android:text="Switch to Night Mode 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 the textColorAttr, backgroundAttr, and backgroundDrawableAttr three attributes. If the current page needs to be refreshed immediately, add the corresponding attributes.

Fourth step:Page calls java code

 @Override
 protected void onCreate(Bundle savedInstanceState) {
   //1. Call this method on the page where the effect needs to be switched 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 the switch between night and day mode
  //ChangeModeController.changeDay(this, R.style.DayTheme);//Switch to day mode
  //ChangeModeController.changeNight(this, R.style.NightTheme);//Switch to night mode
 }
 @Override
 protected void onDestroy() {
  super.onDestroy();
  //3. In the onDestroy call
  ChangeModeController.onDestory();
 } 

Three steps of code calls can start the night journey.
If there are newly created views on the page that need to be added to the night mode control, the Android source code calls:

   //Add additional view to night mode 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 attributes when changing the night mode, you can call the following code after ChangeModeController.changeDay or ChangeModeController.changeNight to assign values to the relevant attributes:
   TypedValue attrTypedValue = ChangeModeController.getAttrTypedValue(this, R.attr.zztextColor);
   toolbar.setTitleTextColor(getResources().getColor(attrTypedValue.resourceId));

Source code download address:http://download.jb51.net/201609/source code/AndroidChangeMode(jb51.net).rar

That's all for this article, I hope it will be helpful to everyone's learning, and I also hope everyone will support the Shouting Tutorial more.

Declaration: The content of this article is from the Internet, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, has not been manually edited, nor does it assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

You May Also Like