Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fix android studio warning about `layout_marginHorizontal`

I'm getting these warnings in my android build.

Warning:(19, 9) Attribute `layout_marginVertical` is only used in API level 26 and higher (current min is 24)
Warning:(20, 9) Attribute `layout_marginHorizontal` is only used in API level 26 and higher (current min is 24)

Clearly our app's layouts make use of layout_marginVertical and layout_marginHorizontal attributes, which were introduced in API level 26. The warning makes it sound like these attributes won't work on level 24 devices, but indeed they work perfectly. I would like to understand why I'm getting these warnings, despite there seeming to be nothing wrong.

One obvious solution is to use the "longhand": marginTop and marginBottom instead of marginVertical, but I'm hoping to continue to use these "short" parameters if possible, to improve readability and reduce repetition.

Were these attributes undocumented for level 24? Or is there some kind of backward compatibility at play? If so, why the warning?

We are using:

<uses-sdk
    android:minSdkVersion="24"
    android:targetSdkVersion="26" />

Update: Setting targetSdkVersion="24" seems to fix the warnings, and the desired margins continue to work. This solves my immediate problem, but I'm left confused about why a level 26 feature works on level 24, and why targeting level 26 causes warnings. What do I do if I want to target level 26?

like image 654
aaaidan Avatar asked Mar 13 '18 02:03

aaaidan


1 Answers

This API seems to be introduced in API level 26. Since you might be using AppCompatActivity, maybe they have also introduced some backward compatibility for pre 26 SDK. Documentation

Moreover, the warning that you are receiving might just be a lint check error.

like image 169
Swapnil Avatar answered Sep 30 '22 03:09

Swapnil