Skip to content

Instantly share code, notes, and snippets.

@baleen37
Created January 24, 2017 05:28
Show Gist options
  • Save baleen37/ec3a4505819df1bfd327e86f06a125ba to your computer and use it in GitHub Desktop.
Save baleen37/ec3a4505819df1bfd327e86f06a125ba to your computer and use it in GitHub Desktop.
public class RoundedTextView extends TextView {
private float mRadius = 0f;
public RoundedTextView(Context context) {
super(context);
init(context, null);
}
public RoundedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public RoundedTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundedTextView);
mRadius = a.getDimension(R.styleable.RoundedTextView_rtvRadius, 0f);
a.recycle();
GradientDrawable gd = new GradientDrawable();
gd.setCornerRadius(mRadius);
int color = Color.TRANSPARENT;
Drawable background = getBackground();
if (background instanceof ColorDrawable)
color = ((ColorDrawable) background).getColor();
gd.setColor(color);
setBackground(gd);
}
public float getRadius() {
return this.mRadius;
}
public void setRadius(int radius) {
this.mRadius = radius;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment