Android Color Picker Library

ANDROID-COLOR-PICKER-LIBRARY.png

12th October, 2022

There are many color picker libraries available today. Ambilwarna android color picker library is one of the best. This library is used in many apps. Here’s a simple example that describes how to use the Android Color Picker Library in your app.

Step 1: Create New Project

Create a new project in Android Studio from File ⇒ New Project and select Empty Activity from the templates.

Step 2: Add dependency

To use color picker in the project add following dependency to your app level build.gradle.

Project ⇒ app ⇒ build.gradle

dependencies
{
    implementation 'com.github.yukuku:ambilwarna:2.0.1'
}

Step 3: Modify Activity

Open the Activity and modify it with the code below.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/rootLayout">

    <Button
        android:id="@+id/btnTest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Change Background Color" />

</RelativeLayout>
package com.codestringz.mytestapp;

import androidx.appcompat.app.AppCompatActivity;
import yuku.ambilwarna.AmbilWarnaDialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity
{
    private int mRootLayoutBgColor = Color.WHITE;
    private RelativeLayout mRootLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setUIRef();
    }

    private void setUIRef()
    {
        //Get root layout UI Reference
        mRootLayout = findViewById(R.id.rootLayout);
        
        //Get initial root layout background color
        Drawable drawable = mRootLayout.getBackground();
        if (drawable instanceof ColorDrawable)
        {
            mRootLayoutBgColor = ((ColorDrawable) drawable).getColor();
        }

        //Get Test button UI Reference
        Button testButton = findViewById(R.id.btnTest);
        
        //Handle test button clicks
        testButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                openColorPicker();
            }
        });
    }

    private void openColorPicker()
    {
        //Create color picker dialog
        AmbilWarnaDialog ambilWarnaDialog = new AmbilWarnaDialog(MainActivity.this, mRootLayoutBgColor, new AmbilWarnaDialog.OnAmbilWarnaListener()
        {
            @Override
            public void onCancel(AmbilWarnaDialog dialog){}

            @Override
            public void onOk(AmbilWarnaDialog dialog, int color)
            {
                mRootLayoutBgColor = color;

                //Set root layout background color
                mRootLayout.setBackgroundColor(mRootLayoutBgColor);
            }
        });

        //show color picker dialog
        ambilWarnaDialog.show();
    }
}

Happy coding!

Leave a Reply