textAppearance does not work on custom TextView

Having a custom TextView is a totally normal thing these days. The problem comes when you try the following:

<com.test.view.MyCustomTextView 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:textAppearance="@style/MyCustomTextAppearance" />

If your custom TextView extends from the TextView class then this textAppearance that you have set will probably not work. It works in the Preview but it is never displayed correctly on the emulator or on a real device.

The fix is very simple. Just extend from AppCompatTextView. And now everything works!

The AppCompat documentation says it alone:

This will automatically be used when you use TextView in your layouts and the top-level activity / dialog is provided by appcompat. You should only need to manually use this class when writing custom views.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s