Skip to content

Instantly share code, notes, and snippets.

@ayushhgoyal
Last active January 4, 2016 19:09
Show Gist options
  • Save ayushhgoyal/8665833 to your computer and use it in GitHub Desktop.
Save ayushhgoyal/8665833 to your computer and use it in GitHub Desktop.
These files can be used to implement shrinkable textviews in android projects, textview will change its textSize according to the length of data being set in textview. Values are supposed to be played with.
public class ShrinkableTextView extends TextView {
public ShrinkableTextView(Context context) {
super(context);
}
public ShrinkableTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ShrinkableTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, type);
if (text.length() < 7) {
this.setTextSize(20f);
} else if (text.length() > 7 && text.length() < 10) {
this.setTextSize(15f);
} else if (text.length() >= 10) {
this.setTextSize(12f);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment