GridLayout and Row/Column Span Woe
It feels pretty hacky, but I managed to get the correct look by adding an extra column and row beyond what is needed. Then I filled the extra column with a Space in each row defining a height and filled the extra row with a Space in each col defining a width. For extra flexibility, I imagine these Space sizes could be set in code to provide something similar to weights. I tried to add a screenshot, but I do not have the reputation necessary.
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnCount="9"
android:orientation="horizontal"
android:rowCount="8" >
<Button
android:layout_columnSpan="2"
android:layout_gravity="fill"
android:layout_rowSpan="2"
android:text="1" />
<Button
android:layout_columnSpan="2"
android:layout_gravity="fill_horizontal"
android:text="2" />
<Button
android:layout_gravity="fill_vertical"
android:layout_rowSpan="4"
android:text="3" />
<Button
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:layout_rowSpan="2"
android:text="4" />
<Button
android:layout_columnSpan="3"
android:layout_gravity="fill_horizontal"
android:text="5" />
<Button
android:layout_columnSpan="2"
android:layout_gravity="fill_horizontal"
android:text="6" />
<Space
android:layout_width="36dp"
android:layout_column="0"
android:layout_row="7" />
<Space
android:layout_width="36dp"
android:layout_column="1"
android:layout_row="7" />
<Space
android:layout_width="36dp"
android:layout_column="2"
android:layout_row="7" />
<Space
android:layout_width="36dp"
android:layout_column="3"
android:layout_row="7" />
<Space
android:layout_width="36dp"
android:layout_column="4"
android:layout_row="7" />
<Space
android:layout_width="36dp"
android:layout_column="5"
android:layout_row="7" />
<Space
android:layout_width="36dp"
android:layout_column="6"
android:layout_row="7" />
<Space
android:layout_width="36dp"
android:layout_column="7"
android:layout_row="7" />
<Space
android:layout_height="36dp"
android:layout_column="8"
android:layout_row="0" />
<Space
android:layout_height="36dp"
android:layout_column="8"
android:layout_row="1" />
<Space
android:layout_height="36dp"
android:layout_column="8"
android:layout_row="2" />
<Space
android:layout_height="36dp"
android:layout_column="8"
android:layout_row="3" />
<Space
android:layout_height="36dp"
android:layout_column="8"
android:layout_row="4" />
<Space
android:layout_height="36dp"
android:layout_column="8"
android:layout_row="5" />
<Space
android:layout_height="36dp"
android:layout_column="8"
android:layout_row="6" />
</GridLayout>
Grid layout column span not works
I managed to accomplish what you are looking for. Just add layout_gravity and margin_right to your two last EditTexts
, like this:
<EditText
android:layout_width="wrap_content"
android:layout_columnSpan="1"
android:layout_gravity="fill_horizontal"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="25dp"
android:inputType="number" />
This will do the job.
Column Span of GridLayout is not working in Xamarin.android
The code could fit the Button inside 2 column span. But, if there is nothing in the column or row, it does not make change.
Make the change with getView method.
private GridLayout getView()
{
GridLayout gridlayout = new GridLayout(this);
TextView textView1 = new TextView(this);
textView1.Text = "csll0";
TextView textView2 = new TextView(this);
textView2.Text = "csll1";
TextView textView3 = new TextView(this);
textView3.Text = "csll3";
TextView textView4 = new TextView(this);
textView4.Text = "csll4";
Button b = new Button(this);
b.Text = "Button1";
var p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent,
ViewGroup.LayoutParams.MatchParent);
gridlayout.LayoutParameters = p;
gridlayout.RowCount = 3;
gridlayout.ColumnCount = 2;
addViewToGridLayout(gridlayout, textView1, 0, 1, 0, 1);
addViewToGridLayout(gridlayout, textView2, 0, 1, 1, 1);
addViewToGridLayout(gridlayout, textView3, 1, 1, 0, 1);
addViewToGridLayout(gridlayout, textView4, 1, 1, 1, 1);
addViewToGridLayout(gridlayout, b, 2, 1, 0, 2);// Here the column span given as 2.
return gridlayout;
}
Set the column span to 1.
addViewToGridLayout(gridlayout, b, 2, 1, 0, 1);
Set the column span to 2.
addViewToGridLayout(gridlayout, b, 2, 1, 0, 2);// Here the column span given as 2.
Row span and column span best layout for design
android.support.v7.widget.GridLayout fits your situation.
In your build.gradle (any versions may be ok):
compile "com.android.support:gridlayout-v7:24.1.1"
In your layout xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:text="Fir 08 Jul 10:44GMT +01"/>
<android.support.v7.widget.GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
app:columnCount="8">
<!--
Header Row
-->
<TextView
style="@style/CellStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_columnSpan="7"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:padding="3dp"
android:background="@android:color/darker_gray"
android:text="ATP Singles Winmbeldon"
android:textColor="@android:color/white"/>
<!-- Spacer for green -->
<View
style="@style/CellSpacerStyle"
app:layout_columnWeight="1"
app:layout_rowWeight="1"/>
<!--
First Row SF 13:00
-->
<!-- Title -->
<TextView
style="@style/CellStyle"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
app:layout_rowSpan="2"
android:text="SF\n13:00"/>
<!-- Sore 1 -->
<TextView
style="@style/CellStyle"
android:layout_height="@dimen/row_height"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
app:layout_rowSpan="1"
android:text="Raonic M. (6)"/>
<TextView
style="@style/CellStyle"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:text="6"/>
<TextView
style="@style/CellStyle"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:gravity="center"
android:text="4"/>
<TextView
style="@style/CellStyle"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:gravity="center"
android:text="3"/>
<TextView
style="@style/CellStyle"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:gravity="center"
android:text="4"/>
<TextView
style="@style/CellStyle"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:gravity="center"
android:text="5"/>
<!-- GreenView -->
<FrameLayout
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:layout_marginLeft="@dimen/border_size"
android:paddingTop="@dimen/border_size"
android:background="@android:color/white">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/green_circle"/>
</FrameLayout>
<!-- Sore 2 -->
<TextView
style="@style/CellStyle"
android:layout_height="@dimen/row_height"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
app:layout_rowSpan="1"
android:text="Federer R. (3)"/>
<TextView
style="@style/CellStyle"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:text="4"/>
<TextView
style="@style/CellStyle"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:text="6"/>
<TextView
style="@style/CellStyle"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:gravity="center"
android:text="7"/>
<TextView
style="@style/CellStyle"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:text="7"/>
<TextView
style="@style/CellStyle"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:text="5"/>
<!-- GreenView -->
<FrameLayout
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:layout_marginLeft="@dimen/border_size"
android:paddingTop="@dimen/border_size"
android:background="@android:color/white">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/green_circle"/>
</FrameLayout>
<!--
Second Row SF 15:00
-->
<!-- Title -->
<TextView
style="@style/CellStyle"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
app:layout_rowSpan="2"
android:text="SF\n15:00"/>
<!-- Sore 1 -->
<TextView
style="@style/CellStyle"
android:layout_height="@dimen/row_height"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
app:layout_rowSpan="1"
android:text="Berdych T.(10)"/>
<TextView
style="@style/CellStyle"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:text="6"/>
<TextView
style="@style/CellStyle"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:gravity="center"
android:text="4"/>
<TextView
style="@style/CellStyle"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:gravity="center"
android:text="3"/>
<TextView
style="@style/CellStyle"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:gravity="center"
android:text="4"/>
<TextView
style="@style/CellStyle"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:gravity="center"
android:text="5"/>
<!-- GreenView -->
<FrameLayout
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:layout_marginLeft="@dimen/border_size"
android:paddingTop="@dimen/border_size"
android:background="@android:color/white">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/green_circle"/>
</FrameLayout>
<!-- Sore 2 -->
<TextView
style="@style/CellStyle"
android:layout_height="@dimen/row_height"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
app:layout_rowSpan="1"
android:text="Murray A (2)"/>
<TextView
style="@style/CellStyle"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:text="4"/>
<TextView
style="@style/CellStyle"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:text="6"/>
<TextView
style="@style/CellStyle"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:gravity="center"
android:text="7"/>
<TextView
style="@style/CellStyle"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:text="7"/>
<TextView
style="@style/CellStyle"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:text="5"/>
<!-- GreenView -->
<FrameLayout
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:layout_marginLeft="@dimen/border_size"
android:paddingTop="@dimen/border_size"
android:background="@android:color/white">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/green_circle"/>
</FrameLayout>
<!--
Bottom Brders
-->
<View
android:layout_width="0dp"
android:layout_height="@dimen/border_size"
app:layout_columnSpan="7"
app:layout_columnWeight="1"
app:layout_rowWeight="1"/>
<View
android:layout_width="0dp"
android:layout_height="@dimen/border_size"
android:layout_marginLeft="@dimen/border_size"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:background="@android:color/white"/>
</android.support.v7.widget.GridLayout>
</LinearLayout>
In your style.xml:
<resources>
...
<style name="CellStyle">
<item name="android:layout_marginTop">@dimen/border_size</item>
<item name="android:layout_marginLeft">@dimen/border_size</item>
<item name="android:background">@android:color/white</item>
<item name="android:gravity">center</item>
</style>
<style name="CellSpacerStyle">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">0dp</item>
<item name="android:layout_marginLeft">@dimen/border_size</item>
<item name="android:background">@android:color/white</item>
</style>
...
</resources>
In your dimens.xml:
<resources>
...
<dimen name="border_size">1px</dimen>
<dimen name="row_height">54dp</dimen>
...
</resources>
The result is below:
Attributes of GridLayout [document]
app:columnCount
is the number of columns of Gridlayout.app:rowCount
is the number of rows of Gridlayout.
Attributes of GridLayout' children [document]
app:layout_columnSpan
is the number of column occupied by this view.app:layout_rowSpan
is the number of span occupied by this view.
GridLayout matches parent by resizing the last column and row only (AndroidStudio)
If you have static number of rows and columns, you can override cell view's method onMeasure
this way (if you want to fill, for example, whole display):
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = getResources().getDisplayMetrics().widthPixels / COLUMN_COUNT;
int height = getResources().getDisplayMetrics().heightPixels / ROW_COUNT;
setMeasuredDimension(width, height);
}
Related Topics
Android: Volley Http Request Custom Header
Java.Lang.Noclassdeffounderror: Android.Support.V7.Appcompat.R$Styleable
Why the View Keeps Flashing When Using Jetpack Navigation with Compose
Stuck with Getting Camera Pic When Using the Tab Activity
Value of a Global Variable Is Reset After It Is Initialised in Valueeventlistener
No Adapter Attached; Skipping Layout
Get Nfc Tag with Ndef Android Application Record (Aar)
Error: Failed to Resolve: Com.Android.Support:Appcompat-V7:29.0.1'
Navigation Drawer with Backword Compatibility Android
Can't Get File Uri from Intent Onactivityresult
Save Data and Change Orientation
How to Read Contacts in Android Using Realm
Why "This App Has Been Built with an Incorrect Configuration" Error Occured in Some Phones
Play Sound Using Soundpool Example