I used Android Studion 3.6.1 on Ubuntu Linux.
I start an Empty Project and I change the activity_main.xml file into this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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/gradient_background" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="GRADIENT COLORS" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> |
You can see I change the Hello Word! text into GRADIENT COLORS.
I add this source line of code:
1 | android:background="@drawable/gradient_background" |
I create this resource XML into res – drawable folder with click right and New – Drawable Resource File.
This will open an window and I add the name of the resource gradient_background.
This XML file will have this content:
1 2 3 4 5 6 7 8 9 10 | <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item><shape> <gradient android:angle="0" android:startColor="#FF0000" android:centerColor="#00FF00" android:endColor="#0000FF" android:type="linear" /> </shape></item> </selector> |
The result will be a gradient color with colors from red , green and blue.