11

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.

Community
  • 1
  • 1
Sakiboy
  • 7,252
  • 7
  • 52
  • 69

4 Answers4

11

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).

David Miguel
  • 12,154
  • 3
  • 66
  • 68
1

paddingHorizontal and paddingVertical is indeed introduced with API 26 (O). See the Android O update note for proof: https://developer.android.com/about/versions/oreo/android-8.0

user690421
  • 422
  • 1
  • 5
  • 15
  • Yes this is not false, but the question clearly states that we know it’s introduced in android O (api 26).... but the xml attribute can only be used if your minSdk is 26 or higher. – Sakiboy Oct 23 '19 at 14:57
1

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"
Leonardo Costa
  • 994
  • 11
  • 26
  • Yeah of course. Just curious why this API is seemingly available in lower SDKs, but will show an error when using a minSDK lower than `O`. – Sakiboy Dec 08 '20 at 17:46
-1

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

Kvaibhav01
  • 391
  • 1
  • 4
  • 14
  • Why am I getting a warning on the Android Studio XML editor saying it's ONLY available for API 26 (O)? I have my `targetSDKVersion` set to `26` and my minSdkVERSION set to `16`? Also why are there so many sites explaining how "`paddingHorizontal/Vertical`" are finally here with API 26? Why would these blogs/forums/posts rave about something that's always been there? – Sakiboy Nov 12 '17 at 19:46
  • I don't know why there's a warning as the `padding` [attribute](https://developer.android.com/reference/android/R.attr.html#padding) was added as early as in API level 1. Maybe you need to clean build and then rebuild your project. Also check that in SDK Manager all required components are installed successfully. – Kvaibhav01 Nov 13 '17 at 07:04
  • When you scroll down to the specific `paddingHorizontal/Vertical` attributes you’ll see the text saying “Added in API level 26” though. – Sakiboy Nov 13 '17 at 07:26
  • I clean and rebuild my project all the time. What SDK components would I need... I have all my `buildTools`, relevant API sources, and support libraries all up to date... Are you saying I should make sure I explicitly have the Android API references from all the way back to API 1? That sounds a little ridiculous since they build on each other... one last thing have you even tried this out yet and verified this works? Or are you just giving information that you’ve found online....? – Sakiboy Nov 16 '17 at 10:05
  • I tried to use this in my project. There are two solutions: 1. Make a new v21 XML file using alt+enter in the tag. 2. Update the `minSDKVersion` in your Gradle to 26. I wonder why is this a warning if we want our app to be available to more users by a much less minSDKVersion... – Kvaibhav01 Nov 16 '17 at 18:23