44

I know LinearLayoutCompat was realized to give us some newer methods which were added in a higher levels of android to lower levels of Android.

My problem is this method:

 linearLayout.setPaddingRelative

Which was added in API 17 but we should have it in lower API by using the following code right?

 linearLayoutCompat.setPaddingRelative

But my Android Studio still shows the following error on it.

Call requires API level 17 (current min is 15): android.view.View#setPaddingRelativ

So what's the difference between LinearLayoutand LinearLayoutCompat?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
max
  • 5,963
  • 12
  • 49
  • 80

1 Answers1

49

LinearLayoutCompat was added to support methods which were added in newer API levels on old ones (like dividers). If you compare the methods in LinearLayout and LinearLayoutCompat you can see that the Compat layout has all methods of the LinearLayout without any API level limitation.
This brings us back to your question: You are trying to use a method which is part of the View class (LinearLayout inherits from the View class). The supported methods of the View class depend on the different API levels that's why you can't use this method before API level 17 neither with the LinearLayout nor the LinearLayoutCompat.

If you want to use this method no matter what API level you're on you can use the ViewCompat class and call ViewCompat.setPaddingRelative(View view, ...).

reVerse
  • 35,075
  • 22
  • 89
  • 84
  • ok thanks for your attention but why they use the ViewGroup for the parent of LinearLayoutCompat, not ViewGroupCompat? – max Oct 08 '16 at 18:48
  • 1
    I can't tell you sorry. But by looking at the docs ViewGroupCompat doesn't inherit View it's more like ViewCompat a helper class with some static methods. – reVerse Oct 08 '16 at 19:07
  • LinearLayout inherits ViewGroup – Boris Veriga Oct 08 '19 at 10:57
  • 1
    @BorisVeriga Well it directly inherits from ViewGroup however like with every other component everything inherits from View. So LinearLayout -> ViewGroup -> View. – reVerse Oct 08 '19 at 11:25