Skip to content

Instantly share code, notes, and snippets.

@meeDamian
Last active December 25, 2015 16:39
Show Gist options
  • Save meeDamian/7007829 to your computer and use it in GitHub Desktop.
Save meeDamian/7007829 to your computer and use it in GitHub Desktop.
Just a handy trick to handle lack of `android:textAllCaps="true"` on devices prior to ICS
<com.example.MyTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/someIdICreated"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAllCaps="true"
android:text="@string/that_awesome_string_to_be_capitalized" />
public class MyTextView extends TextView {
private boolean allCaps = false;
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
if( isLegacy() ) {
allCaps = attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res/android", "textAllCaps", false);
if(allCaps) setText(getText()); // this line is really stupid, but initial setText() is called in TextView constructor, once allCaps isn't yet properly set...
}
}
@Override
public void setText(CharSequence text, BufferType type) {
if( allCaps && isLegacy() ) text = text.toString().toUpperCase();
super.setText(text, type);
}
private boolean isLegacy() {
return Build.VERSION.SDK_INT<Build.VERSION_CODES.ICE_CREAM_SANDWICH;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment