Vishnu sharma
Vishnu sharma

Reputation: 21

Stretching column width to fill all the available space in GridLayout

GridLayout of two rows and three columns

Above is the image of my design but it is not as desired, I want few editing in this grid and those editing are are as follow:

  1. How to stretch the TextView_1 and fill the available space on its left and right?
  2. In Row_0 * column_0 (ImageView), I used rowSpan = "2" and minHight*minWidth = "64dp", now why I have to define the height of the TextView_1 and TextView_2 & 3 externally i.e. 32dp (Half of the height of ImageView) for both rows, I want theirs height to be stretched equally according to ImageView?
  3. How to set the text alignment center_horizontally && center_vertically in the cell?

Here is My code:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout 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">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="0"
        android:layout_column="0"
        android:layout_rowSpan="2"
        app:srcCompat="@drawable/ic_home_black_24dp"
        android:id="@+id/row_image"
        android:minHeight="64dp"
        android:minWidth="64dp"/>

    <TextView
        android:text="TextView_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="0"
        android:layout_column="1"
        android:layout_columnSpan="2"
        android:id="@+id/row_name"
        android:minHeight="32dp"
        android:layout_gravity="center_horizontal"/>

    <TextView
        android:text="TextView_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="34"
        android:layout_row="1"
        android:layout_column="2"
        android:id="@+id/row_price"
        android:minHeight="32dp"/>

    <TextView
        android:text="TextView_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="66"
        android:layout_row="1"
        android:layout_column="1"
        android:id="@+id/row_disc"
        android:minHeight="32dp"/>

</GridLayout>

Upvotes: 0

Views: 7278

Answers (1)

Vinesh Chauhan
Vinesh Chauhan

Reputation: 1378

remove from textview row 0 col 1

android:layout_gravity="center_horizontal" 

and add to textview row 0 col 1 textview row 1 col 1 textview row 1 col 2

 android:gravity="center"
 android:textAlignment="center"

should be look like this

<GridLayout 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"
    >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="0"
        android:layout_column="0"
        android:layout_rowSpan="2"
        app:srcCompat="@drawable/ic_home_black_24dp"
        android:id="@+id/row_image"
        android:minHeight="64dp"
        android:minWidth="64dp"/>


    <TextView
        android:layout_columnWeight="70"
        android:gravity="center"
        android:textAlignment="center"
        android:text="TextView_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="0"
        android:layout_column="1"
        android:layout_columnSpan="2"
        android:id="@+id/row_name"
        android:minHeight="32dp"
      />



    <TextView
        android:gravity="center"
        android:textAlignment="center"
        android:text="TextView_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="34"
        android:layout_row="1"
        android:layout_column="2"
        android:id="@+id/row_price"
        android:minHeight="32dp"/>

    <TextView
        android:gravity="center"
        android:textAlignment="center"
        android:text="TextView_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="66"
        android:layout_row="1"
        android:layout_column="1"
        android:id="@+id/row_disc"
        android:minHeight="32dp"/>

</GridLayout>

columnweight work like this enter image description here

Upvotes: 4

Related Questions