Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting app:drawableEndCompat instead of android:drawableEnd warning for Lollipop API 21 devices

AS 4.0.1

Use app:drawableEndCompat instead of android:drawableEnd warning on API 21 and above

 <TextView
        android:id="@+id/tvCheckStock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableEnd="@drawable/ic_pin_drop
        android:gravity="center_vertical"/>

The ic_pin_drop is a SVG (Vector Drawable) that was created using Android Studio File | New | Vector Asset | Configure Vector Assert

In my Build.gradle file I have the following configuration:

minSdkVersion 21
targetSdkVersion 29
vectorDrawables.useSupportLibrary true

As the min is API 21 which is Lollipop I was thinking that vector drawables are supported out of the box and we can use the DrawableEnd, DrawableStart etc without the compat versions?

I was thinking that the compat versions were for pre 21 API level. Kitkat and below. And as I am not targeting that minimum I am not sure why I am getting that warning.

This would result in a cash on those devices less than 21 if the compat version is not used.

Many thanks for any suggestions.

like image 258
ant2009 Avatar asked Aug 19 '20 03:08

ant2009


People also ask

What is the use of drawableendcompat?

By using "Compat", you would be ensuring that using your vector asset would not crash your app for devices below API 21(Lollipop). In short, using drawableEndCompatwould allow anyone to use the same features of drawableEndon older APIs(<21).

How to programmatically set drawableleft in Android?

AndroidMobile DevelopmentApps/Applications This example demonstrates how do I programmatically set drawableleft in android. Step 1− Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2− Add the following code to res/layout/activity_main.xml.

What is drawable shape in Android?

Shape drawable. A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable (int) or apply to another XML resource with attributes such as android:drawable and android:icon . There are several different types of drawables:

How to set the level value of the drawable in Android?

Setting the level value of the drawable with setLevel () loads the drawable resource in the level list that has a android:maxLevel value greater than or equal to the value passed to the method. The filename is used as the resource ID.


2 Answers

The primary motive behind this warning is to make your VectorDrawables look same on all devices by making them backward compatible. By using "Compat", you would be ensuring that using your vector asset would not crash your app for devices below API 21(Lollipop). In short, using drawableEndCompat would allow anyone to use the same features of drawableEnd on older APIs(<21). Now you would be thinking that what should I choose: If you are using drawableEndCompat, it would work as you expect in every device. Devices with API more than 21 would internally unwrap them as normal drawableEnd as far as I know. If you choose to use drawableEnd, it will be working for API 21 and above only.

If you think that I don't need any Compat Support : You may increase your application's minimum SDK from current value to atleast 21. Then you can use keyword drawableEnd peacefully without any warning. Also, you may opt to create different layout file for different APIs under which in layout file for below API 21, use drawableEndCompat and for API 21 and above use drawableEnd. In my opinion, you can also check whether you can use both attributes at same place. I feel like that they can work together as well.

If you are not supporting devices below API 21, there is no problem: You should have no issues/warning. Also, one more thing I need to tell you - Android Studio sometimes throw warnings or bugs; even if you are right. In that case, if you feel that you were correct, you should try invalidate/restart option after clicking File option in Menu Bar. I would also suggest to try restarting your system to everyone who are using system for long durations or always keeping it on sleep mode.

So, for a TextView, you should use app:drawableEndCompat (or start, top, bottom) instead of app:drawableEnd

like image 107
Anuj Kumar Avatar answered Oct 30 '22 00:10

Anuj Kumar


Vector drawables are supported since API 21 but new features are added over time, even to vector drawables. It may be a good idea to use AppCompat and not worry about that.

Tinting compound drawables on TextViews was added in API 23. The suggestion is pushing you towards the compat variant, where the feature is backported.

  • app:drawable*Compat
  • app:drawableTint and app:drawableTintMode

If you don't use compound drawable tinting you may be fine with the platform version of the attributes.

  • android:drawable*
  • android:drawableTint and android:drawableTintMode (added in API 23)
like image 39
Eugen Pechanec Avatar answered Oct 30 '22 00:10

Eugen Pechanec