2

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. https://i.stack.imgur.com/y1W9l.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)

Community
  • 1
  • 1
Tremblex
  • 21
  • 4

1 Answers1

3

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));
Sanjeev
  • 4,255
  • 3
  • 28
  • 37
  • also you have to change the value of gradientRadius up or down depending on your needs – Sanjeev Aug 12 '15 at 21:16
  • 1
    The thing is, with this code the image is fading out directly from the middle. I only want the borders to fade. So if I have a flat RED background, only the last 10% on the outsides will start fading out – Tremblex Aug 17 '15 at 15:49
  • there is one more attribute centerColor if you will make it same as startColor your fade will not start from center of image ,also try changing gradientRadius attribute to suit your need , because apart from radial gradient you have choice of linear gradient , but problem with that is only 2 borders will fade – Sanjeev Aug 17 '15 at 18:23