How to use layout weight in GridLayout for APIs lower than 21
Asked Answered
B

1

6

I want to evenly spread 8 buttons using GridLayout. I want to support the APIs lower than 21 (which is the minimum number that supports layout_columnWeight and layout_rowWeight in GridLayout)

Here's my code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="tk.adamnafie.basicphrases.MainActivity"
android:background="@drawable/background">

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

    <Button
        android:layout_column="0"
        android:layout_row="0"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="fill"
        android:id="@+id/button1"
        android:onClick="talk"
        android:text="Good Morning"
        android:layout_width="150dp" />

    <Button
        android:layout_column="1"
        android:layout_row="0"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="fill"
        android:id="@+id/button2"
        android:onClick="talk"
        android:text="Good Afternoon"
        android:layout_width="150dp" />

    <Button
        android:layout_column="0"
        android:layout_row="1"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="fill"
        android:id="@+id/button3"
        android:onClick="talk"
        android:text="Good night"
        android:layout_width="150dp" />

    <Button
        android:layout_column="1"
        android:layout_row="1"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="fill"
        android:id="@+id/button4"
        android:onClick="talk"
        android:text="How are you?"
        android:layout_width="150dp" />

    <Button
        android:layout_column="0"
        android:layout_row="2"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="fill"
        android:id="@+id/button5"
        android:onClick="talk"
        android:layout_width="150dp"
        android:text="Can you talk English?" />

    <Button
        android:layout_column="1"
        android:layout_row="2"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="fill"
        android:id="@+id/button6"
        android:onClick="talk"
        android:layout_width="150dp"
        android:text="I am learning German" />

    <Button
        android:layout_column="0"
        android:layout_row="3"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="fill"
        android:id="@+id/button7"
        android:onClick="talk"
        android:layout_width="150dp"
        android:text="Where do you live?" />

    <Button
        android:layout_column="1"
        android:layout_row="3"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="fill"
        android:id="@+id/button8"
        android:onClick="talk"
        android:layout_width="150dp"
        android:text="I come from Egypt" />

    </GridLayout>
</RelativeLayout>

I want to support Android 4.0 as minimum SDK. Is there an easy way to do so?

Bankrupt answered 9/1, 2016 at 14:4 Comment(0)
P
17

You can use the support library version of GridView which back-ports the features to earlier Android versions. In your build.gradle (module) you need the following dependancies:

compile "com.android.support:support-v4:23.1.1"
compile "com.android.support:gridlayout-v7:23.1.1"

Then in your xml layout files you use:

<android.support.v7.widget.GridLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        app:layout_column="0"
        app:layout_row="0"
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        app:layout_gravity="fill"
        android:id="@+id/button1"
        android:onClick="talk"
        android:text="Good Morning"
        android:layout_width="150dp" />

You also need to add the app namespace to the containing layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
Perales answered 9/1, 2016 at 14:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.