Android gradient background styling is the best way to style your background without consuming too much memory or taxing user storage.
Need some career advice or prepping for an Android developer interview? Hit me up on Topmate.io, and let's chat!
How to do it?
Creating an Android Gradient background in Android Studio is achieved by creating an ‘xml’ drawable resource file and setting the activity or widget background as that drawable file i.e,
‘ android:background: “@drawable/name_of_file” ‘.
In this Android gradient background tutorial we’ll teach you how you can create 3 types of gradients i.e, linear, radial and sweep; with their codes.
To achieve this, you’ll have to first create a new android drawable resource file.
To create this file, in ‘Android’ section of Project Panel on the left, go to app > res > drawable
Right click on ‘drawable’ folder and select ‘New > Drawable Resource File’
Create a new file named “gradient_background”.
1. Linear Style
The most popular Linear Gradient style can be applied by writing following lines of code in ” gradient_background ” file.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="#FFC107"
android:centerColor="#FF5722"
android:startColor="#E91E63"
android:type="linear" />
<corners
android:radius="0dp"/>
</shape>
2. Radial Style
The Radial Gradient style can be applied by writing following lines of code in ” gradient_background ” file.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient
android:gradientRadius="200dp"
android:endColor="#FFC107"
android:centerColor="#FF5722"
android:startColor="#E91E63"
android:type="radial" />
<corners
android:radius="0dp"/>
</shape>
3. Sweep Style
The elegant Sweep style can be applied by writing following lines of code in ” gradient_background ” file.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient
android:centerX="0.5"
android:centerY="0.1"
android:endColor="#FFC107"
android:centerColor="#FF5722"
android:startColor="#E91E63"
android:type="sweep" />
<corners
android:radius="0dp"/>
</shape>
Interested in styling you app buttons? Read how to style your Android app buttons here!
4 comments