Android GridLayout center horizontally - android

I'm trying to create a GridLayout with 2 columns which will be centered.
My avtual design is:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
custom:rowCount="4"
custom:columnCount="2"
android:orientation="horizontal">
<TimeTableKeeper.Tile
android:layout_width="75dp"
android:layout_height="75dp"
android:gravity="top|left"
android:background="#00FF00"
custom:color="green"
custom:layout_row="0"
custom:layout_column="0" />
<TimeTableKeeper.Tile
android:layout_width="75dp"
android:gravity="top|left"
android:layout_height="75dp"
android:background="#00FF00"
custom:color="blue"
custom:layout_row="0"
custom:layout_column="1" />
</GridLayout>
And it looks like:
And I would like to have this buttons in the center and perfectly with spacing between them.
Is it possible?
--EDIT:
I have also tried putting it into LinearLayout, without results:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<GridLayout xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
custom:rowCount="4"
custom:columnCount="2"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center">
<TimeTableKeeper.Tile
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#00FF00"
custom:color="green"
custom:layout_row="0"
custom:layout_column="0" />
<TimeTableKeeper.Tile
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#00FF00"
custom:color="blue"
custom:layout_row="0"
custom:layout_column="1" />
</GridLayout>
</LinearLayout>

Make the grid wrap around its content horizontally with layout_width="wrap_content" and set it's layout_gravity to center:
<GridLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
// ......
>

From GridLayout documentation:
GridLayout's distribution of excess space is based on priority rather than weight.
(...)
To make a column stretch, make sure all of the components inside it define a gravity.
So apparently you need to set the layout_gravity to android:layout_gravity="top|center" (I have not tested this, but from the documentation it should be along these lines.)

You're almost there. I think this will do the job:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:rowCount="4"
custom:columnCount="2"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<TimeTableKeeper.Tile
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#00FF00"
custom:color="green"
custom:layout_row="0"
custom:layout_column="0" />
<TimeTableKeeper.Tile
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#00FF00"
custom:color="blue"
custom:layout_row="0"
custom:layout_column="1" />
</GridLayout>
</FrameLayout>

The code in below solved my problem.
<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">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_margin="30dp"
android:columnCount="3"
android:rowCount="4"
android:useDefaultMargins="true">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</GridLayout>
</RelativeLayout>

if you are using a constraint layout use wrap content on the grid layout and create the items inside it. It will be centered to the screen
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/item"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:columnCount="2"
android:useDefaultMargins="true"
/>

copy this example for calculator layout =)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<!-- android:useDefaultMargins="true" (OPTIONAL) -->
<GridLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:useDefaultMargins="true"
>
<Button
android:layout_column="0"
android:layout_row="0"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="/"/>
<Button
android:layout_column="1"
android:layout_row="0"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="1"/>
<Button
android:layout_column="2"
android:layout_row="0"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="2"/>
<Button
android:layout_column="3"
android:layout_row="0"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="3"/>
<Button
android:layout_column="0"
android:layout_row="1"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="*"/>
<Button
android:layout_column="1"
android:layout_row="1"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="4"/>
<Button
android:layout_column="2"
android:layout_row="1"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="5"/>
<Button
android:layout_column="3"
android:layout_row="1"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="6"/>
<Button
android:layout_column="0"
android:layout_row="2"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="-"/>
<Button
android:layout_column="1"
android:layout_row="2"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="7"/>
<Button
android:layout_column="2"
android:layout_row="2"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="8"/>
<Button
android:layout_column="3"
android:layout_row="2"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="9"/>
<Button
android:layout_column="0"
android:layout_row="3"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="+"/>
<Button
android:layout_column="1"
android:layout_row="3"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="0"/>
<Button
android:layout_column="2"
android:layout_row="3"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="00"/>
<Button
android:layout_column="3"
android:layout_row="3"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="="/>
</GridLayout>
</LinearLayout>

Related

(Android) Make a layout scroll-able?

I have a layout like this
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="16dp"
android:isScrollContainer="true">
<Button
android:layout_width="180dp"
android:layout_height="300dp"
android:text="Button1"
android:layout_gravity="left"
android:layout_row="0"
android:layout_column="0"></Button>
<Button
android:layout_width="180dp"
android:layout_height="300dp"
android:text="Button2"
android:layout_gravity="right"
android:layout_row="0"
android:layout_column="1"></Button>
<Button
android:layout_width="180dp"
android:layout_height="300dp"
android:text="Button3"
android:layout_gravity="left"
android:layout_row="1"
android:layout_column="0"></Button>
<Button
android:layout_width="180dp"
android:layout_height="300dp"
android:text="Button4"
android:layout_gravity="right"
android:layout_row="1"
android:layout_column="1"></Button>
<Button
android:layout_width="180dp"
android:layout_height="300dp"
android:text="Button5"
android:layout_gravity="left"
android:layout_row="2"
android:layout_column="0"></Button>
<Button
android:layout_width="180dp"
android:layout_height="300dp"
android:text="Button6"
android:layout_gravity="right"
android:layout_row="2"
android:layout_column="1"></Button>
</GridLayout>
Normally I will fit everything into a Relative or Constraint layout. But this time I intentionally made it big so that Button5 and Button6 can't be seen. And what I want is to make the layout scrollable so that the user can scroll up to see the Button5 and Button6?
How can I do it? What layout is to be used? Is there a way to make other layouts scrollale?
You have to do this if you want to scroll your view
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:isScrollContainer="true"
android:padding="16dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="180dp"
android:layout_height="300dp"
android:layout_row="0"
android:layout_column="0"
android:layout_gravity="left"
android:text="Button1"></Button>
<Button
android:layout_width="180dp"
android:layout_height="300dp"
android:layout_row="0"
android:layout_column="1"
android:layout_gravity="right"
android:text="Button2"></Button>
<Button
android:layout_width="180dp"
android:layout_height="300dp"
android:layout_row="1"
android:layout_column="0"
android:layout_gravity="left"
android:text="Button3"></Button>
<Button
android:layout_width="180dp"
android:layout_height="300dp"
android:layout_row="1"
android:layout_column="1"
android:layout_gravity="right"
android:text="Button4"></Button>
<Button
android:layout_width="180dp"
android:layout_height="300dp"
android:layout_row="2"
android:layout_column="0"
android:layout_gravity="left"
android:text="Button5"></Button>
<Button
android:layout_width="180dp"
android:layout_height="300dp"
android:layout_row="2"
android:layout_column="1"
android:layout_gravity="right"
android:text="Button6"></Button>
</GridLayout>
</ScrollView>
</GridLayout>

Buttons going outside of the screen - Android

I need help...
I was trying to build 3 buttons aligned in 1 row and 3 columns inside of a GridLayout, and looks its working, but I think I haven't done that right, because if change the size of the text inside the button (textSize) the GridLayout and the buttons are going outside of the screen.
I think that I'm setting the layout_witdh and/or layout_height wrong, but I'm not sure.
Can anyone help me? Thanks :)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#drawable/background"
tools:context="com.markus.tssproject.MainActivity">
<RelativeLayout
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridLayout
android:layout_width="match_parent"
android:layout_height="110dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:columnCount="3"
android:rowCount="1">
<Button
android:id="#+id/id0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_marginRight="3dp"
android:layout_row="0"
android:layout_rowWeight="1"
android:padding="30dp"
android:tag="0"
android:text="test tested testing"
android:textSize="20sp" />
<Button
android:id="#+id/open1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_rowWeight="1"
android:padding="30dp"
android:tag="1"
android:text="test tested testing"
android:texSize="20sp" />
<Button
android:id="#+id/open2"
android:layout_width="wrap_content"
android:layout_column="2"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_rowWeight="1"
android:padding="30dp"
android:tag="2"
android:text="teste tested testing"/>
</GridLayout>
</RelativeLayout>
</LinearLayout>
change the following in each button
android:layout_width="0dip"
android:layout_weight ="1"
you can do this in LinearLayout itself, no need of grid layout
Use this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:orientation="horizontal"
tools:context="com.markus.tssproject.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="110dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:orientation="horizontal">
<Button
android:id="#+id/id0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
android:layout_weight="1"
android:padding="30dp"
android:tag="0"
android:text="test tested testing"
android:textSize="20sp" />
<Button
android:id="#+id/open1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
android:layout_weight="1"
android:padding="30dp"
android:tag="1"
android:texSize="20sp"
android:text="test tested testing" />
<Button
android:id="#+id/open2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="30dp"
android:tag="2"
android:text="teste tested testing" />
</LinearLayout>

Drag and Drop on a grid

I am currently developing an Android APP and I have ran into a bit of a snag. I am using the drag and drop feature to drag different items around and place them into a different layout. I want the layout where I drop the items to be setup in a grid. Where I can drop an item in the general area and it would snap into that location on the grid.
I have done this, but I feel like it is really clunky and there must be a much easier way to get this accomplished. This is how I have it now..
<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"
tools:context="com.example.david.seniorproject.Controller_LayoutFragment"
tools:showIn="#layout/activity_controller__layout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".8"
android:id="#+id/linear_layout">
<GridLayout
android:id="#+id/frame_layout"
android:layout_width="0dp"
android:layout_weight=".75"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="74dp"
android:layout_height="75dp"
android:layout_row="0"
android:id="#+id/relative1"
android:layout_column="0"></RelativeLayout>
<RelativeLayout
android:layout_width="74dp"
android:layout_height="75dp"
android:layout_row="0"
android:id="#+id/relative2"
android:layout_column="1" />
<RelativeLayout
android:layout_width="74dp"
android:layout_height="75dp"
android:layout_row="0"
android:id="#+id/relative3"
android:layout_column="2" />
<RelativeLayout
android:layout_width="74dp"
android:layout_height="75dp"
android:layout_row="0"
android:id="#+id/relative4"
android:layout_column="3" />
<RelativeLayout
android:layout_width="74dp"
android:layout_height="75dp"
android:layout_row="0"
android:id="#+id/relative5"
android:layout_column="4" />
<RelativeLayout
android:layout_width="74dp"
android:layout_height="75dp"
android:layout_row="0"
android:id="#+id/relative6"
android:layout_column="5" />
<RelativeLayout
android:layout_width="74dp"
android:layout_height="75dp"
android:layout_row="1"
android:id="#+id/relative7"
android:layout_column="0" />
<RelativeLayout
android:layout_width="74dp"
android:layout_height="75dp"
android:layout_row="1"
android:id="#+id/relative8"
android:layout_column="1" />
<RelativeLayout
android:layout_width="74dp"
android:layout_height="75dp"
android:layout_row="1"
android:id="#+id/relative9"
android:layout_column="2" />
<RelativeLayout
android:layout_width="74dp"
android:layout_height="75dp"
android:layout_row="1"
android:id="#+id/relative10"
android:layout_column="3" />
<RelativeLayout
android:layout_width="74dp"
android:layout_height="75dp"
android:layout_row="1"
android:id="#+id/relative11"
android:layout_column="4" />
<RelativeLayout
android:layout_width="74dp"
android:layout_height="75dp"
android:layout_row="1"
android:id="#+id/relative12"
android:layout_column="5" />
<RelativeLayout
android:layout_width="74dp"
android:layout_height="75dp"
android:layout_row="2"
android:id="#+id/relative13"
android:layout_column="0" />
<RelativeLayout
android:layout_width="74dp"
android:layout_height="75dp"
android:layout_row="2"
android:id="#+id/relative14"
android:layout_column="1" />
<RelativeLayout
android:layout_width="74dp"
android:layout_height="75dp"
android:layout_row="2"
android:id="#+id/relative15"
android:layout_column="2" />
<RelativeLayout
android:layout_width="74dp"
android:layout_height="75dp"
android:layout_row="2"
android:id="#+id/relative16"
android:layout_column="3" />
<RelativeLayout
android:layout_width="74dp"
android:layout_height="75dp"
android:layout_row="2"
android:id="#+id/relative17"
android:layout_column="4" />
<RelativeLayout
android:layout_width="74dp"
android:layout_height="wrap_content"
android:layout_row="2"
android:id="#+id/relative18"
android:layout_column="5" />
</GridLayout>
<ListView
android:layout_width="0dp"
android:layout_weight=".25"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:backgroundTint="#ff5500" />
</LinearLayout>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".3"
android:paddingRight="80dp"
android:id="#+id/horizontalScrollView"
android:layout_gravity="center_horizontal" >
<LinearLayout
android:id="#+id/input_items"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="80dp"
android:text="Test"
android:layout_height="match_parent"
android:background="#drawable/red_circle"
android:id="#+id/button2" /></LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</RelativeLayout>
As you can see, I placed a bunch of layouts evenly throughout the GridLayout. It works, but I feel like there is a better way. The button is just dummy data to test the drag and drop feature.
So my question is, is there a way to setup a gridLayout, or a different Layout to do the same as my setup, without all the extra layouts, or would it be better to just create a custom ViewGroup.

How do I make a scorecard type layout CardView?

I have been battling with GridLayout and TableLayout for the past couple of hours and for the life of me can't figure out a way to make a CardView that looks like the following:
Currently I have a CardView parent with this GridLayout is its child:
<GridLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:rowCount="3"
android:columnCount="3"
android:orientation="horizontal"
android:padding="20dp">
<!--FirstRow-->
<ImageView
android:id="#+id/away_logo"
android:layout_row="1"
android:layout_column="0"
android:layout_columnWeight="0.3"
android:layout_gravity="start"
android:gravity="center_vertical"
android:src="#android:drawable/btn_star_big_on"/>
<LinearLayout
android:id="#+id/scores_layout"
android:layout_row="1"
android:layout_rowWeight="0.5"
android:layout_column="1"
android:layout_columnWeight="0.4"
android:layout_gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/away_score_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="10"
android:gravity="center_vertical|start"
android:layout_weight="0.5"/>
<TextView
android:id="#+id/home_score_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|center_vertical"
android:text="11"
android:gravity="center_vertical|end"
android:layout_weight="0.5"/>
</LinearLayout>
<ImageView
android:id="#+id/home_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_rowWeight="0.33"
android:layout_column="2"
android:layout_columnWeight="0.3"
android:layout_gravity="start"
android:gravity="center_vertical"
android:src="#android:drawable/btn_star_big_on"/>
<!--SecondRow-->
<TextView
android:id="#+id/away_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_rowWeight="0.3"
android:layout_column="0"
android:layout_columnWeight="0.33"
android:layout_gravity="start"
android:text="Team"
android:gravity="center_vertical"/>
<TextView
android:id="#+id/home_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_rowWeight="0.3"
android:layout_column="2"
android:layout_columnWeight="0.33"
android:layout_gravity="end"
android:text="Team"
android:gravity="center_vertical"/>
<!--LastRow-->
<TextView
android:id="#+id/time_rink_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_rowWeight="0.3"
android:layout_column="0"
android:layout_columnSpan="3"
android:layout_columnWeight="1"
android:text="7:00P.M.atBotany"
android:layout_gravity="center_horizontal"/>
</GridLayout>
Which gives this layout where the stars are placeholders to visualise where the ImageViews are and the scores are 1 - 13:
Try this layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
tools:text="7:00 P.M. at Venue"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical"
android:padding="5dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
tools:src="#drawable/team_1_image"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="8dp"
tools:text="Team 1"/>
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
tools:text="1 - 3"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical"
android:padding="5dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
tools:src="#drawable/team_2_image"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="8dp"
tools:text="Team 2"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Why not try using relative layout to contain the inner content (also can do it for the title too), and align the views according to the parents, left, right, and center portions and it should work for you.

Android GridLayout - XML - columns not in proportion

I am trying to create a button layout that looks like this on the first row:
[ 1 ][2][3]
Button 1 should occupy 2 columns and be twice the width of buttons 2 and 3. Buttons 2 and 3 should be the same width (1 column wide).
However, what I am seeing is this:
[1][2][ 3 ]
Can somebody please tell me why button 3 completely fills the rest of the screen and why the buttons don't seem to be obeying the layout_column_span rules?
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_margin="0dp"
android:adjustViewBounds="true"
android:alignmentMode="alignMargins"
android:clipToPadding="false"
android:columnCount="4"
android:fitsSystemWindows="false"
android:layoutMode="clipBounds"
android:orientation="horizontal"
android:padding="0dp"
android:rowCount="6"
android:useDefaultMargins="true" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_weight="1"
android:layout_margin="0dp"
android:background="#ff0000"
android:layout_row="0"
android:layout_column="0"
android:layout_columnSpan="2"
android:layout_rowSpan="2"
android:text="1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_weight="1"
android:layout_margin="0dp"
android:background="#ffff00"
android:layout_row="0"
android:layout_column="2"
android:layout_columnSpan="1"
android:layout_rowSpan="2"
android:text="2" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_columnSpan="1"
android:layout_gravity="fill"
android:layout_margin="0dp"
android:layout_row="0"
android:layout_rowSpan="2"
android:layout_weight="1"
android:background="#00ff00"
android:text="3" />
</GridLayout>
Check this may help you
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/favorites_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:columnCount="1"
android:rowCount="2"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="0"
android:orientation="horizontal"
android:weightSum="4" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Cell 1"
android:textSize="14dip" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cell 2" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cell 3"
android:textSize="14dip" />
</LinearLayout>
According to this post, there are some limitations when it comes to arranging space in a non-trivial manner on a GridLayout . You can try another approach or you can use a LinearLayout for each row, so you can control weights for its children Views.
Here is an example for your case:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2"
android:singleLine="true"
android:text="Button 1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:singleLine="true"
android:text="Button 2" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:singleLine="true"
android:text="Button 3" />
</LinearLayout>
Hope to have helped you.

Categories

Resources