Use XML attribute paddingHorizontal on pre API 26 devies?

Viewed 7381

Is the XML attribute android:paddingHorizontal that was introduced in API 26 (Android O) only available in 26 and above? Is there a support version of it? I require my minSdkVersion to be much lower than 26.

I’m under the impression that the Android Asset Packaging Tool 2.0 (AAPT2) , that’s been included in Android Studio 3, could possibly help out with this.

From the release AAPT2 release notes:

Version 2.16

aapt2 link ...

Versioning of XML files is more intelligent, using a small set of rules to degrade specific newer attributes to backwards compatible versions of them. Ex: android:paddingHorizontal degrades to android:paddingLeft and android:paddingRight.

But have not idea how to use AAPT2, and I’m not finding much information out there on how to you it, I just see mentioning of it. Is my request even possible?

Any help/answers are greatly appreciated.

Update: Yeah, it doesn’t work on lower SDK versions.

4 Answers

Although paddingHorizontal, paddingVertical, layout_marginHorizontal andlayout_marginVertical were introduced in API 26 (Android O), they can be used safely when targetting older SDK versions thanks to the desugaring performed by aapt2 (added in v2.16) that converts these to left/right or top/bottom automatically at build time.

Note: sadly they are not supported in styles (nor planned to be) (ref).

To deal with this warning I changed the attributes for use the combination of paddingTop and paddingBottom, like this:

android:paddingTop="16dp"   
android:paddingBottom="16dp"

I think the support for android:paddingHorizontal is there even in API level 10 as seen in its website.

Related