Search code examples
androidcolorsbackgrounddrawablecolor-picker

Colorpicker changing 9 separate drawables?


I have 9 ImageViews that when onClick each open a colorpicker, I would like have the colorpicker change the color of a drawable correlating to that same view that was used the onclick at that time. I am unsure of how to conduct this? I have searched for examples online but cant seem to find anything that is related.


Solution

  • If you have a fixed set of colors that you want to select from, you can use a level list drawable. In XML, it might look something like this:

    <?xml version="1.0" encoding="utf-8"?>
    <level-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item
            android:drawable="@drawable/color_0"
            android:maxLevel="0" />
        <item
            android:drawable="@drawable/color_1"
            android:maxLevel="1" />
        . . .
    </level-list>
    

    You can then make this the drawable for an ImageView and select which color to display by calling the image view's setImageLevel() method.

    EDIT

    You asked for an example of doing this programmatically. Let's say you have the following layout:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" />
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" />
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:tag="0"
                android:onClick="changeColor"
                android:text="Change color" />
    
            <View
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:tag="color_0" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" />
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:tag="1"
                android:onClick="changeColor"
                android:text="Change color" />
    
            <View
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:tag="color_1" />
        </LinearLayout>
    
        <!-- etc. -->
    </LinearLayout>
    

    Then, in your activity, define your handler function something like this:

    public void changeColor(View view) {
        String tag = "color_" + view.getTag();
        View target = findViewById(android.R.id.content).findViewWithTag(tag);
        int color = getColorFromUser();
        target.setBackgroundColor(color); // or whatever you want to do
    }
    

    One might want to include some checking in this process for null tags, unfound views, the user canceling the color choice, etc.