2
votes

I have a TextView, to which I want to add a solid color background. I want this background only have the borders fading to transparency. Like this picture here, but instead of a photo, it's only a solid color. http://i.imgur.com/zAoazUy.png (Sorry I cannot post images yet)

This TextView is dynamically generated so its size can vary. The gradient has to end to transparent.

Is this possible? I fiddled a lot with XML Drawable ressources but got to nothing remotely close to what I want.

Thanks in advance
PS: (Picture stolen from Draw transparent gradient with alpha transparency from 0 to 1)

1

1 Answers

2
votes

You have to define gradient in drawable folder ,startColor can be any color of your choice but for endColor use argb value to get transparent effect at corner

grad.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:type="radial"
        android:gradientRadius="250"
        android:startColor="#0000ff"
         android:endColor="#64ffffff"/>
</shape>

and then simply use in textView

textView.setBackground(getApplicationContext().getDrawable(R.drawable.grad));