Last active
December 25, 2015 16:39
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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