6

Button setTextAppearance(Context context, int resid) is deprecated

and setTextAppearance(int resid) - only available for API level 23

What should I use instead?

Sibelius Seraphini
  • 5,303
  • 9
  • 34
  • 55
  • What api are you developing for – Tim Oct 28 '15 at 14:07
  • What's your issue exactly ? Calling the old method on new Android versions will call the new method internally so you end up with the same result. – Froyo Oct 28 '15 at 14:14

2 Answers2

8

Deprecated means that support will be dropped for it sometimes in the future, but it is still working as expected. On older APIs, there is no alternative, since the new setTextAppearance(int resid) got only released with API level 23.

If you want to be safe for a long time, you can use the following code:

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

This code prefers the new version on phones with API level 23 or higher, but uses the old one when the API level 23 one isn't available.

Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71
8

I am going to say the same this as @Daniel Zolnai. But do not make the check Build.VERSION>SDK_INT < 23 in all the places in your code. Put this in one place, so it will be easy for you to remove this in the future or make changes to it. So how to do it? I will do this for the yourButton case.

  1. Never use Button or any other view provided by android just like that. I say this, because in the future you will need to tweak something and hence it's better to have your own MyButton or something of that sort. So create MyButton extends Button.

  2. Inside MyButton, put the below code:

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


This way you can always use setTextAppearance without needing to worry about checking for BUILD versions. If in future, you plan to remove this whole thing, then you have to refactor in just one place. This is a bit of work, but in the long run, this will help you a lot and will reduce some maintanance nightmares.

Henry
  • 17,490
  • 7
  • 63
  • 98
  • Can we be sure the `TextView.setTextAppearance(int resId)` will not be called as well from the system, resulting in a double call to `super..` ? – guy_m Nov 25 '15 at 20:34
  • Yes. Only API < 23 will call the `setTextAppearance(int resId)`. There is no double call to `super`. – Henry Nov 25 '15 at 20:41
  • you mean API >= 23 calls *only* `STA(int)` and *only* API < 23 calls `STA(context, int)`, right? :) When does the system calls these methods btw? – guy_m Nov 28 '15 at 10:48
  • Sorry about that :) Well, the system doesn't call these methods, but we do, when we want to set the text appearance. – Henry Nov 28 '15 at 12:53
  • Oh really? No way an xml attribute will tell the system to ca it? Well, in that case that's easy:) – guy_m Nov 28 '15 at 13:44