ColorPrefUtil
ColorPrefUtil for Android. Easily change theme and view/layout/text colors or custom background drawables.
Setup
Gradle
Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
implementation 'com.github.akndmr:ColorPrefUtil:1.0.1'
}
Usage
For a complete sample, check app folder.
Changing Theme
First, you need to 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>
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set theme before setContentView
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
MainActivity.java
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);