title: GridView Problem with row Height and Scrolling - Android Kotlin
body: <p>I have been working on an Android Project(Kotlin) and have been facing some issues.</p>
  <p>The project overview and issues:</p>
  <p><strong>Issue 1</strong> :
  I am developing an application with a navigation drawer and fragments. In one of my fragments, I am using a gridview, which has cardview elements(Refer to the first image). I have provided the code just below the image. <em>(In ProductFragment.kt only refer to the category_list and the corresponding code)</em></p>
  <p><a href="https://i.stack.imgur.com/LNI1g.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LNI1g.jpg" alt="" /></a></p>
  <p>fragment_product.xml</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  android:layout_width=&quot;match_parent&quot;
  android:layout_height=&quot;match_parent&quot;
  android:orientation=&quot;vertical&quot;
  android:fitsSystemWindows=&quot;true&quot;
  tools:context=&quot;.Fragments.ProductFragment&quot;&gt;
  
  &lt;GridView
      android:id=&quot;@+id/product_gridview&quot;
      android:columnWidth=&quot;100dp&quot;
      android:verticalSpacing=&quot;15dp&quot;
      tools:listitem=&quot;@layout/gridview_card_item&quot;
      android:horizontalSpacing=&quot;15dp&quot;
      android:numColumns=&quot;auto_fit&quot;
      android:layout_margin=&quot;10dp&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  <p>gridview_card_item.xml</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.cardview.widget.CardView
  xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  android:layout_width=&quot;match_parent&quot;
  android:layout_height=&quot;wrap_content&quot;
  app:cardCornerRadius=&quot;8dp&quot;
  app:cardPreventCornerOverlap=&quot;true&quot;
  android:layout_margin=&quot;10dp&quot;
  app:cardElevation=&quot;5dp&quot;&gt;
  
  &lt;RelativeLayout
      android:layout_width=&quot;match_parent&quot;
      android:padding=&quot;10dp&quot;
      android:layout_height=&quot;wrap_content&quot;&gt;
  
      &lt;ImageView
          android:id=&quot;@+id/gridview_card_item_logo&quot;
          android:layout_width=&quot;90dp&quot;
          android:layout_centerHorizontal=&quot;true&quot;
          android:layout_marginTop=&quot;5dp&quot;
          android:src=&quot;@drawable/paypal&quot;
          android:layout_height=&quot;90dp&quot;/&gt;
      
      &lt;TextView
          android:id=&quot;@+id/gridview_card_item_name&quot;
          android:text=&quot;Learning and Educational Toys&quot;
          android:breakStrategy=&quot;simple&quot;
          android:ellipsize=&quot;none&quot;
          android:textAlignment=&quot;center&quot;
          android:textStyle=&quot;bold&quot;
          android:layout_below=&quot;@+id/gridview_card_item_logo&quot;
          android:layout_centerHorizontal=&quot;true&quot;
          android:textColor=&quot;#000000&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;/&gt;
  &lt;/RelativeLayout&gt;
  &lt;/androidx.cardview.widget.CardView&gt;
  </code></pre>
  <p>Product_Fragment.kt</p>
  <pre><code>class ProductFragment : Fragment() {
  // TODO: Rename and change types of parameters
  private var name: String? = null
  
  val category_list = arrayListOf&lt;GridViewCardItemModel&gt;(
      GridViewCardItemModel(
          R.drawable.bitcoin,
          &quot;Musical Toys&quot;
      ),
      GridViewCardItemModel(
          R.drawable.ethereum,
          &quot;RC Toys&quot;
      ),
      GridViewCardItemModel(
          R.drawable.paypal,
          &quot;Learning &amp; Educational Toys&quot;
      ),
      GridViewCardItemModel(
          R.drawable.xrp,
          &quot;Play Gyms &amp; Play Mats&quot;
      ),
      GridViewCardItemModel(
          R.drawable.bitcoin,
          &quot;Blocks &amp; Construction Sets&quot;
      ),
      GridViewCardItemModel(
          R.drawable.ethereum,
          &quot;Baby Rattles and Teethers&quot;
      ),
      GridViewCardItemModel(
          R.drawable.paypal,
          &quot;Toy Cars Trains &amp; Vehicles&quot;
      ),
      GridViewCardItemModel(
          R.drawable.xrp,
          &quot;Push &amp; Pull Along Toys&quot;
      ),
      GridViewCardItemModel(
          R.drawable.paypal,
          &quot;Push &amp; Pull Along Toys&quot;
      ),
      GridViewCardItemModel(
          R.drawable.bitcoin,
          &quot;Push &amp; Pull Along Toys&quot;
      )
  )
  val age_list = arrayListOf&lt;GridViewCardItemModel&gt;(
      GridViewCardItemModel(
          R.drawable.bitcoin,
          &quot;0 to 6 months&quot;
      ),
      GridViewCardItemModel(
          R.drawable.ethereum,
          &quot;6 to 12 months&quot;
      ),
      GridViewCardItemModel(
          R.drawable.paypal,
          &quot;12 to 24 months&quot;
      ),
      GridViewCardItemModel(
          R.drawable.xrp,
          &quot;2 to 4 years&quot;
      ),
      GridViewCardItemModel(
          R.drawable.bitcoin,
          &quot;5 to 7 years&quot;
      ),
      GridViewCardItemModel(
          R.drawable.ethereum,
          &quot;8 to 10 years&quot;
      ),
      GridViewCardItemModel(
          R.drawable.bitcoin,
          &quot;11 to 13 years&quot;
      ),
      GridViewCardItemModel(
          R.drawable.xrp,
          &quot;14 to 15years&quot;
      ),
      GridViewCardItemModel(
          R.drawable.bitcoin,
          &quot;Above 15 years&quot;
      )
  )
  
  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      arguments?.let {
          name = it.getString(&quot;name&quot;)
      }
  }
  
  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                            savedInstanceState: Bundle?): View? {
      // Inflate the layout for this fragment
      val view = inflater.inflate(R.layout.fragment_product, container, false)
      return view
  }
  
  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
      super.onViewCreated(view, savedInstanceState)
      product_gridview.apply {
          if(name==&quot;Category&quot;){
              adapter = GridViewAdapter(
                  context,
                  category_list
              )
          }else{
              adapter = GridViewAdapter(
                  context,
                  age_list
              )
          }
      }
  }
  
  companion object {
      fun newInstance(name: String) : ProductFragment {
          val args = Bundle()
          args.putString(&quot;name&quot;,name)
          val fragment = ProductFragment()
          fragment.arguments = args
          return fragment
      }
  }
  }
  </code></pre>
  <p>GridViewAdapter.kt</p>
  <pre><code> class GridViewAdapter(var context: Context,var ArrayList: 
   ArrayList&lt;GridViewCardItemModel&gt;):BaseAdapter(){
   override fun getView(position: Int, view: View?, parent: ViewGroup?): View {
      var view = View.inflate(context,R.layout.gridview_card_item,null)
      var icon = view.gridview_card_item_logo
      var name = view.gridview_card_item_name
  
      var listItem = arrayList.get(position)
  
      icon.setImageResource(listItem.icons)
      name.text = listItem.name
      return view
  }
  
  override fun getItem(position: Int): Any  = arrayList.get(position)
  
  override fun getItemId(position: Int): Long = position.toLong()
  
  override fun getCount(): Int = arrayList.size
  }
  </code></pre>
  <p><strong>Issue 2</strong>: Refer to the second image for this. This is my home fragment. I have implemented a similar gridview under categories. Now I want the whole page to scroll instead of only the gridview section. I have tried putting the whole home_fragment layout under scrollview, but it did not help.</p>
  <p><a href="https://i.stack.imgur.com/wrqon.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wrqon.jpg" alt="" /></a></p>
  <p>Fragment_home.xml</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  android:layout_width=&quot;match_parent&quot;
  android:layout_height=&quot;match_parent&quot;
  xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  tools:context=&quot;.Fragments.HomeFragment&quot;&gt;
  
  &lt;com.denzcoskun.imageslider.ImageSlider
      android:id=&quot;@+id/image_slider&quot;
      android:layout_height=&quot;200dp&quot;
      android:layout_width=&quot;match_parent&quot;
      android:padding=&quot;20dp&quot;
      app:iss_selected_dot=&quot;@drawable/default_selected_dot&quot;
      app:iss_unselected_dot=&quot;@drawable/default_unselected_dot&quot;
      app:iss_error_image=&quot;@drawable/error&quot;
      app:iss_auto_cycle=&quot;true&quot;
      app:iss_period=&quot;1000&quot;
      app:iss_delay=&quot;0&quot; /&gt;
  
  &lt;TextView
      android:id=&quot;@+id/most_selling&quot;
      android:text=&quot;Most Selling&quot;
      android:layout_below=&quot;@+id/image_slider&quot;
      android:layout_margin=&quot;15dp&quot;
      android:textColor=&quot;#000000&quot;
      android:textSize=&quot;30sp&quot;
      android:textStyle=&quot;bold&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;wrap_content&quot;/&gt;
  
  &lt;androidx.recyclerview.widget.RecyclerView
      android:id=&quot;@+id/homeRecyclerView&quot;
      android:layout_below=&quot;@+id/most_selling&quot;
      android:orientation=&quot;horizontal&quot;
      tools:listitem=&quot;@layout/cardview_item&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:layout_margin=&quot;10dp&quot;/&gt;
  
  &lt;TextView
      android:id=&quot;@+id/categories&quot;
      android:text=&quot;Categories&quot;
      android:layout_below=&quot;@+id/homeRecyclerView&quot;
      android:layout_margin=&quot;15dp&quot;
      android:textColor=&quot;#000000&quot;
      android:textSize=&quot;30sp&quot;
      android:textStyle=&quot;bold&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;wrap_content&quot;/&gt;
  
  &lt;GridView
      android:id=&quot;@+id/home_gridview&quot;
      android:columnWidth=&quot;100dp&quot;
      android:verticalSpacing=&quot;15dp&quot;
      android:horizontalSpacing=&quot;15dp&quot;
      android:numColumns=&quot;auto_fit&quot;
      android:layout_below=&quot;@id/categories&quot;
      android:layout_margin=&quot;20dp&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;wrap_content&quot;/&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  <p>HomeFragment.kt</p>
  <pre><code>class HomeFragment : Fragment() {
  
  val view_image_list = arrayListOf&lt;Int&gt;(
      R.drawable.image_slide,
      R.drawable.image_slide_2,
      R.drawable.image_slide_3,
      R.drawable.image_slide_4,
      R.drawable.image_slide_5
  )
  val view_title_list = arrayListOf&lt;String&gt;(&quot;Bali&quot;,&quot;Thailand&quot;,&quot;Malaysia&quot;,&quot;America&quot;,&quot;France&quot;)
  val view_price_list = arrayListOf&lt;String&gt;(&quot;42,5000&quot;,&quot;34,000&quot;,&quot;56,000&quot;,&quot;78,000&quot;,&quot;67,000&quot;)
  val gridviewlist = arrayListOf&lt;GridViewCardItemModel&gt;(
      GridViewCardItemModel(
          R.drawable.bitcoin,
          &quot;Bitcoin&quot;
      ),
      GridViewCardItemModel(
          R.drawable.ethereum,
          &quot;Ethereum&quot;
      ),
      GridViewCardItemModel(
          R.drawable.paypal,
          &quot;Paypal&quot;
      ),
      GridViewCardItemModel(R.drawable.xrp, &quot;XRP&quot;)
  )
  
  
  override fun onCreateView(
      inflater: LayoutInflater, container: ViewGroup?,
      savedInstanceState: Bundle?
  ): View? {
      // Inflate the layout for this fragment
      return inflater.inflate(R.layout.fragment_home, container, false)
  }
  
  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
      super.onViewCreated(view, savedInstanceState)
      val imageList = ArrayList&lt;SlideModel&gt;()
      imageList.add(SlideModel(R.drawable.image_slide))
      imageList.add(SlideModel(R.drawable.image_slide_2))
      imageList.add(SlideModel(R.drawable.image_slide_3))
      imageList.add(SlideModel(R.drawable.image_slide_4))
      imageList.add(SlideModel(R.drawable.image_slide_5))
  
      val imageSlider = view.findViewById&lt;ImageSlider&gt;(R.id.image_slider)
      imageSlider.setImageList(imageList,ScaleTypes.FIT)
  
      homeRecyclerView.apply{
          layoutManager = LinearLayoutManager(activity,LinearLayoutManager.HORIZONTAL,false)
          adapter = HomeRecyclerViewAdapter(
              view_image_list,
              view_title_list,
              view_price_list
          )
      }
  
      home_gridview.apply {
          adapter = GridViewAdapter(
              context,
              gridviewlist
          )
      }
  }
  }
  </code></pre>