WASEEM
WASEEM

Reputation: 187

Gridview inside scrollview not working properly

I am trying to design a layout as shown in below image. It contains an ImageView for cover image, GridView in which each cell will act as a button for performing various actions and below that TextView as footer.

Layout Image

I am not able to achieve this thing. Below is my xml code:

home.xml

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="7">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_weight="3"
        android:scaleType="centerCrop"
        android:src="@drawable/xyz" />

    <GridView
        android:id="@+id/gridview"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="3.5"
        android:background="#000000"
        android:columnWidth="100dp"
        android:horizontalSpacing="1dp"
        android:numColumns="2"
        android:stretchMode="columnWidth"
        android:verticalSpacing="1dp"></GridView>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="OSKAR" />
</LinearLayout>

singlegrid.xml

<?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="200dp"
     android:background="#ffffff"
     android:orientation="vertical">

  <ImageView
    android:id="@+id/grid_img"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

  <TextView
    android:id="@+id/grid_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="1" />
</LinearLayout>

I am struggling with this issue and searched a lot with various solutions but not able to find out the correct one. Thanks in advance.

Upvotes: 0

Views: 239

Answers (1)

patel sejal
patel sejal

Reputation: 21

I think Why should you use scrollView? Without scrollview your requirement satisfied.

you can set height of grid's child dyanamically like below:

Child_item_height= 
(screenheight - (toolbar_height+imageview_height+footer_height+textview_height))/2

Note: you can set textview height fix like 20dp. Using above login you can get height of grid so that you can display 4 grid in screen and another grid in scroll.

Upvotes: 1

Related Questions