Michael A Michael A - 9 months ago 37
Android Question

Layout with buttons which fill screen size equally

I want to create a layout which consist of 8 buttons (four rows, two in a row).

I want the buttons to fill the screen size proportionally - equal margins so it will fit well on diffrend screen sizes.

This is what i have now:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<RelativeLayout
android:id="@+id/test_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_marginLeft="10dp" >

<Button
android:id="@+id/concession_btn"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/mybutton" />

<Button
android:layout_width="150dip"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/concession_btn"
android:text="test2"
android:layout_weight="1"
android:background="@drawable/mybutton" />

<Button
android:id="@+id/test_2"
android:layout_width="150dip"
android:layout_below="@+id/concession_btn"
android:layout_height="wrap_content"
android:text="test3"
android:layout_weight="1"
android:background="@drawable/mybutton"/>

<Button
android:layout_width="150dip"
android:layout_toRightOf="@+id/test_2"
android:layout_below="@+id/concession_btn"
android:layout_height="wrap_content"
android:text="test4"
android:layout_weight="1"
android:background="@drawable/mybutton" />
<Button
android:id="@+id/test_3"
android:layout_width="150dip"
android:layout_below="@+id/test_2"
android:layout_height="wrap_content"
android:text="test3"
android:layout_weight="1"
android:background="@drawable/mybutton"/>

<Button
android:layout_width="150dip"
android:layout_toRightOf="@+id/test_3"
android:layout_below="@+id/test_2"
android:layout_height="wrap_content"
android:text="test4"
android:layout_weight="1"
android:background="@drawable/mybutton" />
<Button
android:id="@+id/test_4"
android:layout_width="150dip"
android:layout_below="@+id/test_3"
android:layout_height="wrap_content"
android:text="test3"
android:layout_weight="1"
android:background="@drawable/mybutton"/>

<Button
android:layout_width="150dip"
android:layout_toRightOf="@+id/test_4"
android:layout_below="@+id/test_3"
android:layout_height="wrap_content"
android:text="test4"
android:layout_weight="1"
android:background="@drawable/mybutton" />
</RelativeLayout>




Which gives me eight buttons - four rows two in a row, but my xml uses fix margins so it will not fitt well on diffrent screen sizes.

Help will be much appreciated!

Answer Source
// try this way,hope this will help you...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/concession_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="test1"
            android:background="@drawable/mybutton" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="test2"
            android:layout_weight="1"
            android:background="@drawable/mybutton" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/test_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="test3"
            android:layout_weight="1"
            android:background="@drawable/mybutton"/>

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="test4"
            android:layout_weight="1"
            android:background="@drawable/mybutton" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/test_3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="test5"
            android:layout_weight="1"
            android:background="@drawable/mybutton"/>

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="test6"
            android:layout_weight="1"
            android:background="@drawable/mybutton" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/test_4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="test7"
            android:layout_weight="1"
            android:background="@drawable/mybutton"/>

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="test8"
            android:layout_weight="1"
            android:background="@drawable/mybutton" />
    </LinearLayout>
</LinearLayout>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download