0

Hello I have this Screen

enter image description here

that i made by this code .

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.example.test.project"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

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

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_gravity="fill_horizontal"
                android:text="1" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_gravity="fill_horizontal"
                android:text="2" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_gravity="fill_horizontal"
                android:text="3" />
        </LinearLayout>

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

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_gravity="fill_horizontal"
                android:text="4" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_gravity="fill_horizontal"
                android:text="5" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_gravity="fill_horizontal"
                android:text="6" />
        </LinearLayout>

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

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_gravity="fill_horizontal"
                android:text="7" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_gravity="fill_horizontal"
                android:text="8" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_gravity="fill_horizontal"
                android:text="9" />
        </LinearLayout>

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

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_gravity="fill_horizontal"
                android:text="10" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_gravity="fill_horizontal"
                android:text="11" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_gravity="fill_horizontal"
                android:text="12" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

Is there a way to do it with GridLayout , i tried to have GridLayout but it works only with 2 buttons in a row ,but with 3 it doesnot work , i used the code in the this answer?

Community
  • 1
  • 1
user4o01
  • 2,688
  • 4
  • 39
  • 54
  • GridLayout will work with 3 buttons in a row if you specify android:columnCount="3" – IgorGanapolsky Dec 27 '13 at 18:16
  • You can't, but try with a Table Layout, is pretty similar. [Link.](http://stackoverflow.com/questions/5538366/android-tablerow-stretch-vertically-to-fill-screen) – Mr. Potato Mar 04 '16 at 19:52

2 Answers2

0

You wont like my answer, but put plainly, you can't. GridLayout doesn't support any type of layout "weighting" per say. For text, you can usually use the app:layout_gravity options, but for items like buttons, that won't work.

Your best bet is to just use linear layouts inside of the GridLayout. Doesn't really improve much from what you already have, but at least you can get rid of the first linear layout you have. All you would need is GridLayout at top, then 1 linear layout for each row of buttons. That would increase performance.

http://android-developers.blogspot.com/2011/11/new-layout-widgets-space-and-gridlayout.html

The "Emulating Features from other Layouts" section hints at how using the LinearLayout in the GridLayout for this scenario is the better option.

Razgriz231
  • 105
  • 2
  • 10
-1

You have to change GridLayout android:rowCount="3". try this link and this

<GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="3"
    android:rowCount="3" >

    <TextView
        android:text="1,1" />

    <TextView
        android:text="1,2" />

    <TextView
        android:text="1,3" />


    <TextView
        android:text="2,1" />

    <TextView
        android:text="2,2" />

    <TextView
        android:text="2,3" />

    <TextView
        android:text="3,1" />

    <TextView
        android:text="3,2" />

    <TextView
        android:text="3,3" />


</GridLayout>

Edit: Read this link, link

Zohaib
  • 2,845
  • 2
  • 23
  • 33