So I have an Android library that I want others to be able to easily customize its color when using it. The problem is that the color is not just a single property (like the view background), but it's more like a theme color (the view background, the text color, the stroke for the button, etc...) so I'm not able to just pass it as a view attribute. So I ended up using a color reference
and use it in styles, layout and drawables:
colors.xml:
<resources>
<attr name="color" format="reference" />
</resources>
layout.xml:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/color">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?attr/color"/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="@style/LibraryTheme.TextAppearance"/>
</LinearLayout>
styles.xml in library project:
<style name="LibraryTheme.TextAppearance">
<item name="android:textColor">?attr/color</item>
<item name="colorControlNormal">?attr/color</item>
<item name="android:textColorHint">?attr/color</item>
<item name="colorControlActivated">?attr/color</item>
<item name="colorControlHighlight">?attr/color</item>
</style>
That works great, and when someone is using my lib he must declare the color he wants you use in his theme:
styles.xml in app project:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="color">@color/my_color</item>
</style>
The problem is this: I want to deliver this library with a default color scheme. It can be performed now with 2 options:
The first one is really bad, because I can't force the user to use specific app theme (like AppCompact.Light). The second one is not that great either, because I want the user to be able to use the default seamlessly and I have a couple of colors in the theme that he needs to set.
Is there any other way that you think I will be able to let other users play with the colors easily?
Thanks.
By simply switching wallpapers, you can completely change the colors of the Android UI. It’s super easy to do and has a dramatic effect. We’ll show you. How does it work? Well, it’s pretty simple. Android analyzes your wallpaper and chooses a few accent colors to use throughout the operating system.
So, we use Glide library to request images asynchronously but also it helps to keep these images in cache which will help the app to run more smoothly. After that, we use Android Palette to get the dominant color of the selected image. In order to get Android Palette image, we need to get the bitmap of the image from Glide.
Here are a few examples of how the wallpaper changes the colors. Now that you’ve seen what it can do, let’s change the wallpaper to get a new theme! First, tap and hold on the home screen. Select “Wallpapers & Style” or the equivalent for the launcher that you’re using. Choose the wallpaper that you want and apply it to the home screen.
It takes a bit more effort than normal, but here’s how to go into the OS to change the Android 10 system accent color: First, tap on the Settings icon on your phone. Then, scroll down and tap on the About phone selection. After that, you have to tap on the build number seven times.
The problem is that you are using a color reference, in your case I would use a regular color property- those can be overridden by the user in colors.xml.
So in you library project:
colors.xml:
<resources>
<color name="color">#FFFFFF</color>
</resources>
layout.xml:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/color"/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="@style/LibraryTheme.TextAppearance"/>
</LinearLayout>
styles.xml:
<style name="LibraryTheme.TextAppearance">
<item name="android:textColor">@color/color</item>
<item name="colorControlNormal">@color/color</item>
<item name="android:textColorHint">@color/color</item>
<item name="colorControlActivated">@color/color</item>
<item name="colorControlHighlight">@color/color</item>
</style>
And if the user wants to use different colors, he can declare his own color in his app's colors.xml:
<resources>
<color name="color">#000000</color>
</resources>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With