When we are making UI design for any of the applications the most important part which we should take care of while building the application is that we should be using the proper color combination in our app which should be sometimes the same as that of the image. Using this API we can update the colors of our widgets according to the colors which are present in our Image. This API will help us to extract colors from the image and then we can use these colors in our widgets.
What we are going to build in this article?
We will be building a simple application in which we will be using two different images to change our layout UI component colors according to our image file. A sample video 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 by Step Implementation
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 dependency in build.gradle file
Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.
implementation ‘com.android.support:palette-v7:30.3.0’
After adding the dependency now sync your project and we will move towards working with our layout file.
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout android:id = "@+id/idRLBack" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" tools:context = ".MainActivity" > <!--Image view for displaying our image--> < ImageView android:id = "@+id/idIVImage" android:layout_width = "200dp" android:layout_height = "200dp" android:layout_centerHorizontal = "true" android:layout_marginTop = "70dp" android:src = "@drawable/logo1" /> <!--linear layout for our buttons to change image and layout colors--> < LinearLayout android:id = "@+id/idLL" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_below = "@id/idIVImage" android:layout_centerHorizontal = "true" android:layout_margin = "10dp" android:orientation = "horizontal" android:weightSum = "2" > <!--first button for our first image--> < Button android:id = "@+id/idBtnChangeImg" android:layout_width = "0dp" android:layout_height = "wrap_content" android:layout_margin = "10dp" android:layout_weight = "1" android:text = "Image 1" android:textAllCaps = "false" /> <!--second button for our second image--> < Button android:id = "@+id/idBtnChangeImg2" android:layout_width = "0dp" android:layout_height = "wrap_content" android:layout_margin = "10dp" android:layout_weight = "1" android:text = "Image 2" android:textAllCaps = "false" /> </ LinearLayout > <!--sample text view for heading--> < TextView android:id = "@+id/idTVGFG" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_below = "@id/idLL" android:layout_margin = "4dp" android:padding = "4dp" android:text = "Welcome to Geeks for Geeks" android:textAlignment = "center" android:textSize = "25sp" /> <!--sample text view for second heading--> < TextView android:id = "@+id/idTVHead" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_below = "@id/idTVGFG" android:layout_margin = "3dp" android:padding = "4dp" android:text = "Geeks For Geeks Carnival" android:textAlignment = "center" /> </ RelativeLayout > |
Note: The images used in the ImageView are present in the drawable folder. You can add yours to it.
Step 4: Working with the MainActivity.java file
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.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import androidx.palette.graphics.Palette; public class MainActivity extends AppCompatActivity { // creating variables for our UI components.. private TextView headTV, gfgTV; private ImageView gfgIV; private Button changeBtn, changeBtn2; private RelativeLayout backRL; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); // initializing all our variables with their ids. headTV = findViewById(R.id.idTVHead); gfgTV = findViewById(R.id.idTVGFG); gfgIV = findViewById(R.id.idIVImage); backRL = findViewById(R.id.idRLBack); changeBtn = findViewById(R.id.idBtnChangeImg); changeBtn2 = findViewById(R.id.idBtnChangeImg2); // on below line we are decoding our image from image resource. Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.logo1); Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.logo2); // Get bitmap from drawable resources // on below line we are calling a method // to change the layout color according to first image. createPaletteAsync(bitmap1); // on below line we are setting // bitmap to our image view. gfgIV.setImageBitmap(bitmap1); // on below line we are adding click listener to our button1. changeBtn.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { // inside on click we are calling a second method // to change our layout colors with second image bitmaps. . createDarkPaletteAsync(bitmap2); // on below line we are setting bitmap to our image view. gfgIV.setImageBitmap(bitmap2); } }); // adding on click listener for our second button. changeBtn2.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { // on below line we are calling a method to change // our layout colors and we are passing our first image bitmap. createPaletteAsync(bitmap1); // on below line we are setting // the bitmap to our image view. gfgIV.setImageBitmap(bitmap1); } }); } public void createDarkPaletteAsync(Bitmap bitmap) { // on below line we are calling a palette // method from bitmap to get colors from our image. Palette.from(bitmap).generate( new Palette.PaletteAsyncListener() { public void onGenerated(Palette p) { // Use generated instance // on below line we are passing // a default value to it. int defaultValue = 0x000000 ; // on below line we are adding colors to our different views. headTV.setTextColor(p.getLightVibrantColor(defaultValue)); gfgTV.setTextColor(p.getLightVibrantColor(defaultValue)); backRL.setBackgroundColor(p.getDarkVibrantColor(defaultValue)); changeBtn.setTextColor(p.getDarkVibrantColor(defaultValue)); changeBtn.setBackgroundColor(p.getLightVibrantColor(defaultValue)); changeBtn2.setTextColor(p.getDarkVibrantColor(defaultValue)); changeBtn2.setBackgroundColor(p.getLightVibrantColor(defaultValue)); } }); } public void createPaletteAsync(Bitmap bitmap) { // on below line we are calling a palette method // from bitmap to get colors from our image. Palette.from(bitmap).generate( new Palette.PaletteAsyncListener() { public void onGenerated(Palette p) { // Use generated instance // on below line we are passing // a default value to it. int defaultValue = 0x000000 ; // on below line we are adding colors to our different views. headTV.setTextColor(p.getDarkVibrantColor(defaultValue)); gfgTV.setTextColor(p.getDominantColor(defaultValue)); backRL.setBackgroundColor(p.getLightVibrantColor(defaultValue)); changeBtn.setTextColor(p.getLightMutedColor(defaultValue)); changeBtn.setBackgroundColor(p.getDarkVibrantColor(defaultValue)); changeBtn2.setTextColor(p.getLightMutedColor(defaultValue)); changeBtn2.setBackgroundColor(p.getDarkVibrantColor(defaultValue)); } }); } } |
Now run your app and see the output of the app.
Output: