Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default colors to Android library

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:

  1. Declare my own theme with the default color sets and the user will need to inherit his theme from mine.
  2. Put some default_color in my color.xml so that the user will be able to use that as the color in his theme.

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.

like image 371
shem Avatar asked Jan 21 '16 17:01

shem


People also ask

How to change the colors of the Android UI?

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.

How to get the dominant color of the selected image in Android?

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.

How to change the wallpaper color on Android?

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.

How to change Android 10 system accent color?

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.


1 Answers

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>
like image 176
Mati Bot Avatar answered Oct 25 '22 17:10

Mati Bot