Search code examples
androidincludetoolbarandroid-gridlayout

How do I fill the width of the screen with my toolbar using an Android GridLayout?


I'm trying to fill the width of the screen UI with my toolbar, in a GridLayout. The output I get is shown below. Somehow the toolbar is getting truncated, please advise.

enter image description here

Below is code from my xml file:

<?xml version="1.0" encoding="utf-8"?>

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/GridLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:columnCount="4"
    android:orientation="horizontal"
    android:useDefaultMargins="true"
    tools:context=".CardViewActivity">

<include
    android:id="@+id/toolbar"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_columnSpan="4"
    layout="@layout/toolbar" >
</include>

<TextView
        android:id="@+id/createSkycard"
        android:layout_column="0"
        android:layout_row="0"
        android:layout_columnSpan="3"
        android:layout_marginTop="80dp"
        android:layout_gravity="center_horizontal"
        android:textColor="#FFFFFF"
        android:text="Create a skycard"
        android:textAppearance="?android:attr/textAppearanceLarge"/>
...
</GridLayout>

Solution

  • It is better if you use LinearLayout as a root, like following :

    <?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="match_parent"
       android:orientation="vertical" >
    
       <include
           android:id="@+id/toolbar"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_columnSpan="4"
           layout="@layout/toolbar" >
       </include>
    
       <GridLayout
           android:id="@+id/GridLayout1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:columnCount="4"
           android:orientation="horizontal"
           android:useDefaultMargins="true"
           tools:context=".CardViewActivity" >
    
            <TextView
                android:id="@+id/createSkycard"
               android:layout_column="0"
               android:layout_columnSpan="3"
               android:layout_gravity="center_horizontal"
               android:layout_marginTop="80dp"
               android:layout_row="0"
               android:text="Create a skycard"
               android:textAppearance="?android:attr/textAppearanceLarge"
               android:textColor="#FFFFFF" />
        </GridLayout>
    
    </LinearLayout>