What's in this blog? Hide
  1. Getting Started
  2. android_dvlprs

Android Change Theme Programmatically using ColorPrefUtil library. Easily set Dark, Light or any colorful theme for your android app with few lines of code.

Need some career advice or prepping for an Android developer interview? Hit me up on Topmate.io, and let's chat!

Do you want your android app user to change your Android App color theme, dark or light or maybe some colorful theme. Here is this simple android library called ‘ColorPrefUtil‘ which does this job easily.

Note: This library works with API level 19 and above only!

Here is a sample preview:

Android Change Theme Programmatically
Android Change Theme Programmatically
Android Change Theme Programmatically
Android Change Theme Programmatically

Getting Started

Add the JitPack repository to your root build.gradle at the end of repositories:

allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}

Then add the following dependencies:

dependencies {
	        implementation com.github.akndmr:ColorPrefUtil:1.0.1'
	}

Now Sync your project.

Now go to styles folder in res and set different styles for different color themes – if you want to change theme(setTheme(R.style.id)).

styles.xml

<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>  
</style>  
  
<style name="AppThemePurple" parent="Theme.AppCompat.Light.DarkActionBar">  
    <item name="colorPrimary">@color/colorPrimaryPurple</item>  
    <item name="colorPrimaryDark">@color/colorPrimaryDarkPurple</item>  
    <item name="colorAccent">@color/colorAccentPurple</item>  
</style>

Now go to MainActivity.java file and create SharedPreference to save selected color scheme

MainActivity.java

int selectedBackgroundColorId;

@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
  
    // Set theme before setContentView  
   SharedPreferences mSharedPreferences = getSharedPreferences(PREF_COLOR, MODE_PRIVATE);  
    int themeSelected = mSharedPreferences.getInt(THEME_SELECTED, R.style.AppTheme);  
    ColorPrefUtil.changeThemeStyle(this, themeSelected);  
  
    setContentView(R.layout.activity_main);  
  
//...  
}  

Changing BackgroundColor of Single View and All Views

int selectedBackgroundColorId;

@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  

// Get selected colorId from preferences, if null, use default background color(colorPrimary)
selectedBackgroundColorId = mSharedPreferences.getInt(COLOR_SELECTED, R.color.colorPrimary);

// Single view  
mButton = findViewById(R.id.button);  
ColorPrefUtil.changeBackgroundColorOfSingleView(this, mButton, selectedBackgroundColorId);  
  
// All views inside parent layout
mConstraintLayout = findViewById(R.id.cl_container);
ColorPrefUtil.changeBackgroundColorOfChildViews(this, mConstraintLayout, selectedBackgroundColorId);  
}

Changing Text Color of Single View and All Views

// All views inside given parent layout
  ColorPrefUtil.changeTextColorOfChildViews(this, mConstraintLayout, textColorId, hintColorId);  
 
 // Single view
  ColorPrefUtil.changeTextColorOfSingleView(this, mTextView, textColorId, hintColorId);

Changing Tint Color of Icons (ImageView)

ColorPrefUtil.changeTintColorOfIcon(this, mImageViewIcon, iconTintColorId);

Changing Background Drawable of Single View and All Views You can set custom background drawables for views.

ColorPrefUtil.changeBackgroundDrawableOfChildViews(this, mConstraintLayout, backgroundColorId);  
ColorPrefUtil.changeBackgroundDrawableOfSingleView(this, mButton, backgroundDrawableId);

Changing Item Colors of NavigationView Change icon tint color and text colors of Nav view.

ColorPrefUtil.changeColorOfItemsOfNavView(mNavigationView, iconColorId, textColorId);

Changing Colors of TabLayout Change TabLayout background, selected tab, indicator, text colors.

ColorPrefUtil.changeColorOfTabLayout(this, mTabLayout, backgroundColorId, selectedTabColorId, indicatorColorId, textColorId);

You can read more about this library here.

Want to get a custom button shape for your Android App, read here.

2 comments
Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

You May Also Like