Button setTextAppearance(Context context, int resid) is deprecated
and setTextAppearance(int resid) - only available for API level 23
What should I use instead?
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.
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.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With