44

In the support library appCompat v7 of the Android platform, there is a android.support.v7.widget.LinearLayoutCompat class.

Does someone know why this class exist? The original LinearLayout class exist since API level 1 so I don't understand why there is a compat version.

ol_v_er
  • 27,094
  • 6
  • 48
  • 61

1 Answers1

58

The class LinearLayout exists since API level 1, but some APIs were added after that, for example, setShowDividers introduced on API level 11.

So in this case setShowDividers (and it's parameters) should be invoked using LinearLayoutCompat instead LinearLayout if you are targeting a platform with API level below 11.

tato.rodrigo
  • 2,783
  • 21
  • 26
  • I am confused. If target sdk level is 10. Why should the app use APIs that is in sdk 11? – Weishi Z Mar 09 '16 at 06:56
  • 4
    @WeishiZeng the target will usually be higher, but the if you choose to include old devices with minSdk < 11, you need the AppCompat Version so that the old devices understand the new parameters as well. – Björn Kechel Apr 27 '16 at 08:06
  • generally, as of now we always set target higher so i think no need to use `LinearLayoutCompat` – Anand Savjani May 03 '17 at 10:17
  • @Anand Savjani there are many new features added in new apis for example getGravity added in API level 24. If you want to use getGravity and your application's min api version lower then 24 you have to use LinearLayoutCompat. You can find methods with their api versions at the following link. https://developer.android.com/reference/android/widget/LinearLayout?hl=en – Gökberk Yağcı Jan 03 '21 at 22:48