android text gradient

5

Is it possible to create a gradient in the text (and not the background ) of a textView using XML only?

I know that using shader can do with code ( here >)

Is it possible to do this using only XML ?

    
asked by anonymous 24.05.2016 / 15:43

1 answer

2

It does not seem possible to create TextView with gradient text only using XML. However it is possible to achieve this effect by creating a canvas and drawing on it. You need to declare our custom UI element.

XML

<br.pacote.TextGradient
                android:id="@+id/txtVersao"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignRight="@+id/btnEntrar"
                android:gravity="right"
                android:textSize="40sp"
                android:text="GRADIENT" />

Custom TextView

public class TextGradient extends TextView {

    public TextGradient(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public TextGradient(Context context) {
        super(context);
    }

    public TextGradient(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {

        Shader shader = new LinearGradient(0, 0, 0, super.getTextSize(), Color.RED, Color.BLUE,
                Shader.TileMode.CLAMP);
        super.getPaint().setShader(shader);
        super.setText(text.toString(), type);
    }
}

Image

In this way, you can use TextGradient in .xml .

    
29.09.2016 / 18:18