Sample Project to demonstrate that how to use multi theme in Android application without using any Libraries.
It's very easy to implement multiple themes in your Application, with any customization that you wants, you can refer this project to implement/learn multiple theme in your Android Application.
Steps to implement multiple themes as Followed in this Sample Project :
Create custom attributes :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="themes">
<attr name="myWindowBackground" format="reference"/>
<attr name="myTabBackgroundColor" format="reference"/>
<attr name="myTabTextColor" format="reference"/>
<attr name="myTabTextSelectedColor" format="reference"/>
<attr name="myTabIndicatorColor" format="reference"/>
<attr name="myPrimaryTextColor" format="reference"/>
<attr name="mySecondaryTextColor" format="reference"/>
</declare-styleable>
</resources>
Create custom theme style Based on your defined attributes :
<style xmlns:android="http://schemas.android.com/apk/res/android" name="Green">
<item name="colorPrimary">@color/greenColorPrimary</item>
<item name="colorPrimaryDark">@color/greenColorPrimaryDark</item>
<item name="colorAccent">@color/greenColorAccent</item>
<item name="myWindowBackground">@color/greenBackgroundColor</item>
<item name="myTabBackgroundColor">@color/greenTabBackgroundColor</item>
<item name="myTabTextColor">@color/greenTabTextColor</item>
<item name="myTabTextSelectedColor">@color/greenTabTextSelectedColor</item>
<item name="myTabIndicatorColor">@color/greenTabIndicatorColor</item>
<item name="myPrimaryTextColor">@color/greenPrimaryText</item>
<item name="mySecondaryTextColor">@color/greenSecondaryText</item>
</style>
Set values with custom attributes :
<android.support.design.widget.TabLayout
app:layout_constraintTop_toBottomOf="@+id/toolbar"
android:id="@+id/tabLayout"
android:layout_width="match_parent"
app:tabBackground="?myTabBackgroundColor"
app:tabTextColor="?myTabTextColor"
app:tabIndicator="?myTabIndicatorColor"
app:tabSelectedTextColor="?myTabTextSelectedColor"
android:layout_height="wrap_content"/>
To Apply any theme use setTheme()
just before super.onCreate
:
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.Green)
super.onCreate(savedInstanceState)
}
Above steps help in setup multi theme styles and attributes
If you found this project helpful or you learned something from the source code and want to thank me, consider buying me a cup of ☕