0

I've got a simple grid layout:

<?xml version="1.0" encoding="utf-8"?>
<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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".pkgFragment.InputDataOverviewFragment">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/abToolBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/BrightYellowCrayola">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/tbMenuIconWithTitle"
            app:navigationIcon="@drawable/baseline_menu_24"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:title="@string/InputData"
            >


        </androidx.appcompat.widget.Toolbar>



    </com.google.android.material.appbar.AppBarLayout>

    <GridView
        android:id="@+id/inputDataGrid"
        android:layout_below="@id/abToolBarLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="2"
        android:verticalSpacing="1dp"
        android:background="#e5e5e5"
        android:horizontalSpacing="2dp"
        />
</RelativeLayout>

Layout for grid item:

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

    <RelativeLayout
        android:id="@+id/inputDataGridValues"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="#fff"
        >

        <TextView
            android:id="@+id/itemTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            />

        <ImageView
            android:id="@+id/itemImage"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            />


    </RelativeLayout>

</RelativeLayout>

The issue I'm facing is that the height wont adjust to the full screen so that there is no empty space left due to my android:height="wrap_content" attribute:

In order to solve this problem I tried to set the height based on the screenheight/numberofrows I have:

@SuppressLint("SetTextI18n")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    InputDataType inputDataType = getItem(position);

    if (convertView == null) { convertView = LayoutInflater.from(getContext()).inflate(R.layout.grid_item, parent, false); }

    DisplayMetrics metrics = new DisplayMetrics();
    ((MainActivity)getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics);
    convertView.setMinimumHeight(metrics.heightPixels/3); //I will have free rows

    ImageView icon = (ImageView) convertView.findViewById(R.id.itemImage);
    TextView text = (TextView) convertView.findViewById(R.id.itemTitle);

    //set icon and title bla bla bla
}

However the issue with this approach is that the height is now to big/there is a lot of emtpy spaces between each row:

gridview

How can I solve this issue?

Savan Luffy
  • 440
  • 3
  • 12

0 Answers0