Mahmoud.G Mahmoud.G - 1 year ago 221
Android Question

Android Studio - Grid layout display

Hello am trying to build a simple app on android studio.
on emulator the grid displays the buttons perfectly , but when installing the app to my mobile the grid looks horrible.
below is the XML file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.modysoft.gridlayoutdemo.MainActivity">

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

<Button
android:text="Good Morning"
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:onClick="Buttontapped"
android:layout_rowWeight="1"

android:id="@+id/goodmorning" />

<Button
android:text="Good Evening"
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:onClick="Buttontapped"
android:id="@+id/goodevening" />

<Button
android:text="HI / Bye"
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:onClick="Buttontapped"
android:id="@+id/hibye" />

<Button
android:text="Good Bye"
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:onClick="Buttontapped"
android:id="@+id/goodbye" />

<Button
android:text="Please"
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:onClick="Buttontapped"
android:id="@+id/please" />

<Button
android:text="Thank You &#10; Very Much"
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:onClick="Buttontapped"
android:id="@+id/thankyou" />

<Button
android:text="Welcome"
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:onClick="Buttontapped"
android:id="@+id/welcome" />

<Button
android:text="How are you?"
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:onClick="Buttontapped"
android:id="@+id/howareyou" />

</GridLayout>




this how shown in mobile

this how shown in emulator

Answer Source

You can achieve the same what you expect without using <GridLayout>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp">
        <Button
            android:text="Good Morning"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:onClick="Buttontapped"
            android:id="@+id/goodmorning" />
        <Button
            android:text="Good Evening"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:onClick="Buttontapped"
            android:id="@+id/goodevening" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp">
        <Button
            android:text="Good Bye"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:onClick="Buttontapped"
            android:id="@+id/goodbye" />
        <Button
            android:text="HI / Bye"
            android:id="@+id/hibye"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:onClick="Buttontapped"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp">
        <Button
            android:text="Please"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:onClick="Buttontapped"
            android:id="@+id/please" />
        <Button
            android:text="Thank You &#10; Very Much"
            android:id="@+id/thankyou"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:onClick="Buttontapped"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp">

        <Button
            android:text="Welcome"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:onClick="Buttontapped"
            android:id="@+id/welcome" />
        <Button
            android:text="How are you?"
            android:id="@+id/howareyou"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:onClick="Buttontapped"/>
    </LinearLayout>
</LinearLayout>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download