2

I have a text and I want to set a gradient color as its foreground. May I do it in XML?

Or should I do it in an activity? I am programming with Kotlin in Android Studio.

File activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:gravity="center"
        android:text="Text Color is Gradient"
        android:textColor="#000"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

File gradient.xml

<?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:centerColor="#555994"
        android:endColor="#b5b6d2"
        android:startColor="#555994"
        android:type="linear" />

    <corners
        android:radius="0dp"/>

</shape>

My activity is this:

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MMG
  • 3,226
  • 5
  • 16
  • 43
  • inside xml, set background to the textview.. replace android:background="#FFFFFF" to android:background="@drawable/gradient" – Raza Mar 11 '20 at 07:52
  • It doesn't work @Raza, does it work for you? – MMG Mar 11 '20 at 07:54

2 Answers2

11

In order to set the gradient as Textview text color, you have to use textshader with parsing gradients color. You can customise the colors as of your requirement.

 val paint = textView.paint
        val width = paint.measureText(textView.text.toString())
        val textShader: Shader = LinearGradient(0f, 0f, width, textView.textSize, intArrayOf(
            Color.parseColor("#F97C3C"),
            Color.parseColor("#FDB54E"),
            /*Color.parseColor("#64B678"),
            Color.parseColor("#478AEA"),*/
            Color.parseColor("#8446CC")
        ), null, Shader.TileMode.REPEAT)

        textView.paint.setShader(textShader)

Image

Raza
  • 791
  • 7
  • 22
  • Thank you @Raza, but this code will make background of my textview gradient while I want my text to be gradient. In other words, now my text is black, I want to make it gradient. – MMG Mar 11 '20 at 10:11
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/209700/discussion-between-mohammadmoeingolchin-and-raza). – MMG Mar 16 '20 at 10:46
  • Hi Raza, see my bounty question. Had you this problem when you used alertdialog? https://stackoverflow.com/questions/60462748/ – MMG Mar 17 '20 at 05:58
  • don't you know answer of this question? https://stackoverflow.com/questions/60582383/bottomnavigationviews-titles-color-being-a-gradient – MMG Mar 23 '20 at 02:25
1

try like this in kotlin

binding.textView.setTextColor(resources.getColor(R.color.snooze_gradient_start))

        val textShader: Shader = LinearGradient(
            0f, binding.textView.paint.measureText(binding.textView.text.toString()), 0f, 0f, intArrayOf(
                resources.getColor(R.color.gradient_start), resources.getColor(R.color.gradient_end)
            ), floatArrayOf(0f, 1f), Shader.TileMode.CLAMP
        )
    binding.textView.paint.shader = textShader