59

public void setTextAppearance (Context context, int resId) Added in API level 1
This method was deprecated in API level 23. Use setTextAppearance(int) instead.

My Question: Why it's been deprecated? Why it doesn't need Context anymore? And most importantly, how to use setTextAppearance(int resId) for older versions?

Alireza Farahani
  • 2,238
  • 3
  • 30
  • 54
  • You can do that for older version just check `Build.VERSION.SDK_INT` which would be < MarshMallow. – Pankaj Nov 07 '15 at 10:54

3 Answers3

130

You can use TextViewCompat from the support/androidX library:

    import android.support.v4.widget.TextViewCompat // for support-library
    import androidx.core.widget.TextViewCompat      // for androidX library

    // ...

    TextViewCompat.setTextAppearance(view, resId)

Internally it gets the context from the view (view.getContext()) on API < 23.

Source for TextViewCompat

Source for TextView (API23)

RustamG
  • 1,815
  • 1
  • 14
  • 16
  • 2
    I'm creating TextView's using `TextView(context)` and I tried switching to TextViewCompat and it has no public constructor plus I can't cast the newly created TextView and use it that way. How do you use (androidx) TextViewCompat if you're creating text views programmatically? – Francisc0 Aug 15 '19 at 14:22
  • for buttons there is AppCompatButton and setTextAppearance(context, resId) – 10101101 Oct 17 '19 at 13:04
51
  1. how to use setTextAppearance(int resId) for older versions?

    Use it like this:

    if (Build.VERSION.SDK_INT < 23) {
        super.setTextAppearance(context, resId);
    } else {
        super.setTextAppearance(resId);
    }
    

    For more info: https://stackoverflow.com/a/33393762/4747587

  2. Why it's been deprecated? Why it doesn't need Context anymore?

    The Reason why it is deprecated is there is no need to pass a context. It uses the default context provided by the View. Look at the source code below. That should explain it.

    public void setTextAppearance(@StyleRes int resId) {
         setTextAppearance(mContext, resId);
    }
    

    The mContext here is defined in the View class. So you need not pass a Context to this method anymore. The TextView will use the context provided to it during it's creation. That makes more sense.

UPDATE

This functionality is added as part of the Support Library. So instead of TextView, use TextViewCompat [documentation]. There are also other classes introduced along with this, like ImageViewCompat.

Henry
  • 17,490
  • 7
  • 63
  • 98
  • 19
    This is awful, why the heck didn't they add setTextAppearance as part of the ContextCompat class? Now we have to wrap all our setTextAppearance methods or run the risk of deprecated code failure in our client code. – worked Jan 10 '16 at 20:07
  • 9
    @worked see my answer: http://stackoverflow.com/a/37028325/2613692 They did add the method to TextViewCompat class before this question was posted. – RustamG Jul 26 '16 at 10:34
  • Nice! Thanks @RustamG – worked Nov 05 '16 at 01:19
1

The above answer is correct - here's another way too. In Kotlin I wrote an extension which makes life easier if you do support SDK 23 and below as well as above.

   fun TextView.setAppearance(context: Context, res: Int) {
        if (Build.VERSION.SDK_INT < 23) {
            setTextAppearance(context, res)
        } else {
            setTextAppearance(res)
        }
    }
azwethinkweiz
  • 522
  • 4
  • 8