GradientDrawable in Android
Last Updated :
20 Dec, 2021
The user interface (UI) of modern apps is improving all the time. Designers are experimenting with various styles and color combinations to see which ones work best with the Android App. GradientDrawable is a key component of Android that is commonly used these days.
What actually is a gradient drawable?
A GradientDrawable is drawable with a color gradient that can be used for buttons, backgrounds, and so on. Let's begin with a simple example of creating a button in Android with an aqua colored background:
This can be achieved in the following way:
XML
<Button
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:text="Geeks for Geeks"
android:background="#00FFFF"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
What if you want something similar to this:
Gradients are required for this.
There are two approaches to this:
#1. Using an XML drawable resource
Create a new file gfg color drawable.xml in the drawable package and enter the following code:
XML
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:angle="45"
android:startColor="#D98880"
android:centerColor="#F4D03F"
android:endColor="#48C9B0"/>
</shape>
We've used the <gradient /> tag and added three colour codes: startColor, centerColor, and endColor.Â
GeekTip: We've also set the angle to 45 to indicate a TOP-BOTTOM orientation.
Finally, start the app!
#2. Drawable Gradient
Using GradientDrawable, we can achieve the same functionality programmatically. Create the GradientDrawable in your activity.
Kotlin
val gfgGradient = GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
intArrayOf(
0XFFD98880.toInt(),
0XFFF4D03F.toInt(),
0XFF48C9B0.toInt()
))
In this case, the orientation is TOP BOTTOM (which corresponds to 45 we added in XML earlier). We've also used the same three colors as before. Then, make this gradientDrawable the button's background:
Kotlin
val gfgButton: Button = findViewById(R.id.gfgButton)
gfgButton.background = gradientDrawable
Color 1 takes up half of the entire space (50 percent), while the other two colors cover the remaining space equally. (Each contributes 25%). This is not possible with XML. This is where GradientDrawable's true power is revealed. To achieve the aforementioned result, we can do the following:
val gfgGradient = GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
intArrayOf(
0XFFD98880.toInt(),
0XFFD98880.toInt(),
0XFFF4D03F.toInt(),
0XFF48C9B0.toInt()
))
This is also not possible with XML, but it is simple to do with GradientDrawable, as shown below:
Kotlin
val gfgGradient = GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
intArrayOf(
0XFFD98880.toInt(),
0XFFF4D03F.toInt(),
0XFF48C9B0.toInt(),
0XFF2C3E50.toInt(),
0XFFAF7AC5.toInt()
))
Things to Keep in Mind
1. Positioning:
- It can be any of the orientations defined in the GradientDrawable enum.
- Orientation. LEFT RIGHT has been used in our examples. TOP BOTTOM, TR BL, RIGHT LEFT, and other orientations are also possible. They're pretty self-explanatory.
2. Color Palette:
- We must provide an array of hexadecimal color values here.
- For the value 0XFFD98880,
- 0X - Denotes a hexadecimal number
- FF - The alpha value that will be applied to the color. Alpha can range from 0 to 100. In this case, FF represents 100 percent alpha
- D98880 - The hexadecimal color code RRGGBB is represented by this value.
Similar Reads
DrawableView in Android
In this article, we are going to show the implementation of the DrawableView. We are going to going to implement paint as we have on our laptop. Â Let's see the implementation of this feature. A sample video is given below to get an idea about what we are going to do in this article. Note that we are
3 min read
GravityView in Android
In this article, we are going to show the GravityView in android. In this article, we are going to see the gravity effect on an image. As we move our phone we will see different parts of the image. Here we will be using Horizontal ScrollView so we will be moving our phone horizontally. In the below
2 min read
Android Vector Drawables
With new, exciting mobile devices coming out every day with new screen sizes and resolutions, it can be difficult making sure that the used images look best on every new device that hits the stores. So what many developers end up doing is creating multiple versions of each image for different displa
3 min read
How to create AnimatedGradient in Android?
In this article, we are going to learn how to create AnimatedGradient in android. It can be used in the background of our app. In this, we add different color gradients and animate them. Even in the older versions of Instagram, the developers used AnimatedGradient for the background of the login scr
3 min read
CardView in Android With Example
CardView is a new widget in Android that can be used to display any sort of data by providing a rounded corner layout along with a specific elevation. CardView is the view that can display views on top of each other. The main usage of CardView is that it helps to give a rich feel and look to the UI
3 min read
Expandable TextView in Android
ExpandableTextView is an Android library which allows us to easily create a TextView which can expand/collapse when user clicks on it .we can use this feature in many apps such as movie review app or storytelling app and in many other apps. A sample GIF is given below to get an idea about what we ar
4 min read
Croller in Android
In this article, we will learn about how to add Croller in android. Croller is used to implement circular Seekbar in android. Seekbar is a type of progress bar. We can drag the seekbar from left to right and vice versa and hence changes the current progress. This is how a Croller look like. Table of
3 min read
React Chakra UI Gradient
React Chakra UI Gradients allow you to add stylish and visually appealing backgrounds. Gradients are a blend of two or more colors that transition smoothly from one to another. In this article, we will learn about the implementation of the Gradients in Chakra UI. Prerequisites:NPM & NodeReactRea
2 min read
SlidingDrawer in Android with Example
SlidingDrawer is a common requirement in android mobiles to view the content whenever needed by sliding horizontally as well as vertically. In this article, let us check out Sliding Drawer which is used to hide the content out from the screen and allows the user to drag a handle to bring the content
9 min read
How to Add Gradient to CardView in Android?
UI stands for User Interface, which represents the graphical interface that allows the user to interact with software or any other devices. It comprises some visual elements like Buttons, icons, text, and Images that allow users to steer and interact with the software. UI basically concentrate on cr
2 min read