Fix android studio warning about `layout_marginHorizontal`
Asked Answered
O

2

18

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?

Ophthalmoscope answered 13/3, 2018 at 2:18 Comment(4)
Did you find out why your layout_marginHorizontal worked on pre-26 devices? I have the same "problem" now. Actually, layout_marginHorizontal even works on Android 4.4Huelva
@MaksimDmitriev Nope, sorry - never got around to figuring this one out. Let me know if you do!Ophthalmoscope
@Ophthalmoscope I don't think lowering your targetSdkVersion is recommended. To my understanding: android:minSdkVersion="min_sdk_version_required_to_install_the_app" android:targetSdkVersion="sdk_version_that_the_app_works_well" android:buildSdkVersion="latest_sdk_available". As you mentioned, android:layout_marginVertical still works with some magic. But if you want to get rid of the warning without lowering the targetVersion, you can use tools:targetApi="26" or tools:ignore="UnusedAttribute" to suppress the warning.Proximity
@MaksimDmitriev The reason for it working is AAPT android.googlesource.com/platform/frameworks/base/+/master/…Cradle
B
3

Works via AAPT2 since version 2.16. Here is the confirmation. But you should be careful, it works only in layout files. It will not work in styles files, even when there is no warning.

Brause answered 30/11, 2022 at 8:17 Comment(0)
O
1

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.

Ofori answered 9/1, 2020 at 17:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.