
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to change colors of a Drawable in Android?
This example demonstrates how to change the colors of a Drawable in Android.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background" android:id="@+id/activity_main" android:orientation="vertical" tools:context=".MainActivity" tools:ignore="InvalidId"> </LinearLayout>
Step 3 − Add the following code to src/MainActivity.java
package com.app.sample; import androidx.appcompat.app.AppCompatActivity; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.widget.LinearLayout; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LinearLayout your_Layout = (LinearLayout) findViewById(R.id.activity_main); AnimationDrawable animationDrawable = (AnimationDrawable) your_Layout.getBackground(); animationDrawable.setEnterFadeDuration(4000); animationDrawable.setExitFadeDuration(4000); animationDrawable.start(); } }
Step 4 − Add the following code to res/drawable/background.xml
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/gradient1" android:duration="4000" /> <item android:drawable="@drawable/gradient2" android:duration="4000" /> </animation-list>
Step 5 − Add the following code to res/drawable/gradient1.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:angle="225" android:endColor="#044fab" android:startColor="#21d6d3" /> </shape>
Step 6 − Add the following code to res/drawable/gradient2.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:angle="45" android:endColor="#933c94" android:startColor="#517f95" /> </shape>
Step 7 − Add the following code to Manifests/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.app.sample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from the android studio, open one of your project's activity files and click Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −
Click here to download the project code.
- Related Questions & Answers
- How to convert Drawable to a Bitmap in Android?
- How to convert Bitmap to drawable in Android?
- How to pass drawable between activities in android?
- How to set background drawable programmatically in android?
- How to convert a Drawable to a Bitmap in Android using Kotlin?
- How to change the background and foreground colors of tab in Java?
- How to change bullet colors for lists with CSS?
- How to convert a Bitmap to Drawable in Kotlin?
- How to change position of Toast in Android?
- How to change font color of textView in android?
- How to change font Family of textView In Android?
- How to change font face of Webview in Android?
- How to create custom notification layouts and text colors in Android?
- How to change the color of a CheckBox in Android using Kotlin?
- How to handle orientation change android?