How to Create a Basic Color Picker Tool in Android? - GeeksforGeeks

22 Oct, 2020
There are many open-source color picker tools for android applications to choose from. In this discussion, At the end of this article, one will be able to implement the color picker tool in the android application, have a look at the following image to get an overview of the discussion. In this article, it’s been discussed to implement the very basic color picker tool.
A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. 
Note that select Java as the programming language.
Step 2: Adding the AmbilWarna color picker library dependency
AmbilWarna is an open-source color picker library which can be found here. Which has only one release and this is one of the final releases.
Now adding its dependency to the app-level gradle file.
implementation ‘com.github.yukuku:ambilwarna:2.0.1’
Make sure the system should be connected to the network (so that it downloads the required files) and after invoking the dependency click on the “Sync Now” button.
Refer to the following image to locate the app-level gradle file and invoke the dependency.
Step 3: Working with the actvity_main.xml file
Next, go to the activity_main.xml file, which represents the UI of the project.
Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
XML
<?xml version="1.0" encoding="utf-8"?><LinearLayout     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"    android:orientation="vertical"    tools:context=".MainActivity"    tools:ignore="HardcodedText">      <!--Give all widgets, the proper id to handle         them in MainActivity.java-->      <!--GeeksforGeeks Text-->    <TextView        android:id="@+id/gfg_heading"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:layout_marginTop="32dp"        android:text="GeeksforGeeks"        android:textSize="42sp"        android:textStyle="bold" />      <!--Pick color Button-->    <Button        android:id="@+id/pick_color_button"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:layout_marginTop="32dp"        android:layout_marginStart="32dp"        android:layout_marginEnd="32dp"        android:text="Pick Color" />      <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:layout_marginTop="32dp"        android:textSize="18sp"        android:text="Your picked color is:" />      <!--sample view to preview selected color by user-->    <!--by default this has been set to darker gery-->    <!--this can be overridden after user chose the         color from color picker-->    <!--which has been handled in the MainActivity.java-->    <View        android:id="@+id/preview_selected_color"        android:layout_width="48dp"        android:layout_height="48dp"        android:layout_gravity="center"        android:background="@android:color/darker_gray"        android:layout_marginTop="8dp" />      <!--set color button to overwrite the         color for GeeksforGeeks text-->    <Button        android:id="@+id/set_color_button"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:layout_marginTop="32dp"        android:layout_marginStart="32dp"        android:layout_marginEnd="32dp"        android:text="Set Color" />  </LinearLayout>
Output UI:
Before going to handle the color picker tool dialog functionality, understanding the parts of the dialog box is necessary so that it can become easier while dealing with parts of the dialog box in java code.
Step 4: Working with the MainActivity.java file
Finally, go to the MainActivity.java file, and refer to the following code.
Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;import androidx.appcompat.app.AppCompatActivity;import yuku.ambilwarna.AmbilWarnaDialog;  public class MainActivity extends AppCompatActivity {      // text view variable to set the color for GFG text    private TextView gfgTextView;      // two buttons to open color picker dialog and one to    // set the color for GFG text    private Button mSetColorButton, mPickColorButton;      // view box to preview the selected color    private View mColorPreview;      // this is the default color of the preview box    private int mDefaultColor;      @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          // register the GFG text with appropriate ID        gfgTextView = findViewById(R.id.gfg_heading);          // register two of the buttons with their        // appropriate IDs        mPickColorButton = findViewById(R.id.pick_color_button);        mSetColorButton = findViewById(R.id.set_color_button);          // and also register the view which shows the        // preview of the color chosen by the user        mColorPreview = findViewById(R.id.preview_selected_color);          // set the default color to 0 as it is black        mDefaultColor = 0;          // button open the AmbilWanra color picker dialog.        mPickColorButton.setOnClickListener(                new View.OnClickListener() {                    @Override                    public void onClick(View v) {                        // to make code look cleaner the color                        // picker dialog functionality are                        // handled in openColorPickerDialogue()                        // function                        openColorPickerDialogue();                    }                });          // button to set the color GFG text        mSetColorButton.setOnClickListener(                new View.OnClickListener() {                    @Override                    public void onClick(View v) {                        // as the mDefaultColor is the global                        // variable its value will be changed as                        // soon as ok button is clicked from the                        // color picker dialog.                        gfgTextView.setTextColor(mDefaultColor);                    }                });    }      // the dialog functionality is handled separately    // using openColorPickerDialog this is triggered as    // soon as the user clicks on the Pick Color button And    // the AmbilWarnaDialog has 2 methods to be overridden    // those are onCancel and onOk which handle the "Cancel"    // and "OK" button of color picker dialog    public void openColorPickerDialogue() {          // the AmbilWarnaDialog callback needs 3 parameters        // one is the context, second is default color,        final AmbilWarnaDialog colorPickerDialogue = new AmbilWarnaDialog(this, mDefaultColor,                new AmbilWarnaDialog.OnAmbilWarnaListener() {                    @Override                    public void onCancel(AmbilWarnaDialog dialog) {                        // leave this function body as                        // blank, as the dialog                        // automatically closes when                        // clicked on cancel button                    }                      @Override                    public void onOk(AmbilWarnaDialog dialog, int color) {                        // change the mDefaultColor to                        // change the GFG text color as                        // it is returned when the OK                        // button is clicked from the                        // color picker dialog                        mDefaultColor = color;                          // now change the picked color                        // preview box to mDefaultColor                        mColorPreview.setBackgroundColor(mDefaultColor);                    }                });        colorPickerDialogue.show();    }}
android
Android
Java
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Create and Add Data to SQLite Database in Android?
Resource Raw Folder in Android Studio
Broadcast Receiver in Android With Example
Services in Android with Example
Android RecyclerView in Kotlin
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples