0

I have the following situation: there's a gradient background in my activity:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="90"
        android:endColor="#0F7D71"
        android:startColor="#0E725B"
        android:type="linear" />
</shape>

Here's the way how I set this background to my activity:

<ScrollView 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/login_gradient_background"
    tools:context=".ui.login.LoginActivity">

But I also need to use gradient background in some other screens with other colors. Now I create a new gradient file for each screen, but I don't think that it is the best way. So, can you help me to explain how to pass colors from code to have only one gradient xml for the project. Thanks in advance!

Sergei Mikhailovskii
  • 2,100
  • 2
  • 21
  • 43

1 Answers1

1

There's no need to use the XML file at all, You should create a GradientDrawable from code and customize it according to each screen then set it as a background for the root XML of the activity or the fragment. Click here to see how to use it.

Alaa AbuZarifa
  • 1,171
  • 20
  • 39