Skip to content

Instantly share code, notes, and snippets.

@rahulchowdhury
Created August 4, 2016 15:47
Show Gist options
  • Select an option

  • Save rahulchowdhury/5f7328dc99986e1b4cf132fee60cc819 to your computer and use it in GitHub Desktop.

Select an option

Save rahulchowdhury/5f7328dc99986e1b4cf132fee60cc819 to your computer and use it in GitHub Desktop.
Custom TextView for Android which shows text with a gradient fill
import android.content.Context;
import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* Created by rahulchowdhury on 04/08/16.
*/
public class GradientTextView extends TextView {
public GradientTextView(Context context) {
super(context);
}
public GradientTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public GradientTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
//Setting the gradient if layout is changed
if (changed) {
getPaint().setShader(new LinearGradient(0, 0, getWidth(), getHeight(),
ContextCompat.getColor(getContext(), R.color.colorStart),
ContextCompat.getColor(getContext(), R.color.colorEnd),
Shader.TileMode.CLAMP));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment