2

I want to use xml attributes from higher api on lower android system. How to do that without changing much? like android:layout_columnWeight and android:layout_rowWeigh. Here is my xml code:

<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_column="0"
    android:layout_row="0"
    android:layout_gravity="fill"
    android:layout_columnWeight="1"
    android:layout_rowWeight="1"
    android:text="Hello"
    android:id="@+id/hello"
    android:onClick="buttonTapped"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_column="1"
    android:layout_row="0"
    android:layout_gravity="fill"
    android:layout_columnWeight="1"
    android:layout_rowWeight="1"
    android:text="How are you"
    android:id="@+id/howareyou"
    android:onClick="buttonTapped"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_column="0"
    android:layout_row="1"
    android:layout_gravity="fill"
    android:layout_columnWeight="1"
    android:layout_rowWeight="1"
    android:text="good evening"
    android:id="@+id/goodevening"
    android:onClick="buttonTapped"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_column="1"
    android:layout_row="1"
    android:layout_gravity="fill"
    android:layout_columnWeight="1"
    android:layout_rowWeight="1"
    android:text="please"
    android:id="@+id/please"
    android:onClick="buttonTapped"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_column="0"
    android:layout_row="2"
    android:layout_gravity="fill"
    android:layout_columnWeight="1"
    android:layout_rowWeight="1"
    android:text="my name is"
    android:id="@+id/mynameis"
    android:onClick="buttonTapped"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_column="1"
    android:layout_row="2"
    android:layout_gravity="fill"
    android:layout_columnWeight="1"
    android:layout_rowWeight="1"
    android:text="do you &#10;speak english"
    android:id="@+id/doyouspeakenglish"
    android:onClick="buttonTapped"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_column="0"
    android:layout_row="3"
    android:layout_gravity="fill"
    android:layout_columnWeight="1"
    android:layout_rowWeight="1"
    android:text="welcome"
    android:id="@+id/welcome"
    android:onClick="buttonTapped"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_column="1"
    android:layout_row="3"
    android:layout_gravity="fill"
    android:layout_columnWeight="1"
    android:layout_rowWeight="1"
    android:text="i live in"
    android:id="@+id/ilivein"
    android:onClick="buttonTapped"/>

please don't say to use another method to do that, gridLayout is very useful but I can't use on lower android systems.

future2020
  • 65
  • 1
  • 7
  • 1
    It might be helpful to specify what API level you are targetting that is too low for GridLayout. Isn't it a v7 support library? – emerssso Aug 22 '16 at 16:18
  • These attributes are not tied to an API level. These attributes are tied to the container. You can use [the attributes defined on `GridLayout.LayoutParams`](https://developer.android.com/reference/android/widget/GridLayout.LayoutParams.html) because your widgets are inside of a `GridLayout`. And, as the answers note, use the `GridLayout` backport in the `support-v4` library, if you want to use `GridLayout` prior to API Level 14. – CommonsWare Aug 22 '16 at 16:26

2 Answers2

2

In this case, you can use android.support.v7.widget.GridLayout from the Android Support Library in place of the standard GridLayout.

Barend
  • 17,296
  • 2
  • 61
  • 80
1

Just switch to the GridLayout from the support library.

That way you get to use your layout_columnWeight on older APIs, but replace the android namespace prefix with app.

Reference

The only other alternative is to use an alternative layout file inside your layout-v21 directory or something.

Community
  • 1
  • 1
Passer by
  • 562
  • 9
  • 14