Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button setTextAppearance is deprecated

Button setTextAppearance(Context context, int resid) is deprecated

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

What should I use instead?

like image 564
Sibelius Seraphini Avatar asked Oct 28 '15 14:10

Sibelius Seraphini


2 Answers

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.

like image 199
Daniel Zolnai Avatar answered Nov 08 '22 08:11

Daniel Zolnai


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.

like image 8
Henry Avatar answered Nov 08 '22 08:11

Henry