• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • About
  • Projects
    • GStaticMap WP Plugin
  • Contact
  • Privacy Policy

Lorenz Blog

All About Web & Mobile Application Development

  • Featured Articles
  • Gadgets
    • Android
    • Blackberry
  • Programming
    • Android
    • PHP
    • Java Script
    • MySQL
    • Postgresql
    • Flex
    • Web
  • Software
    • Mac OS
    • Windows
    • Linux
  • Web
You are Here » Home >> Information Technology >> Programming >> Android >> How to Make Android ListView or GridView Expandable inside ScrollView

How to Make Android ListView or GridView Expandable inside ScrollView

February 17, 2014 by Lorensius Londa 14 Comments

We all know that placing a ListView or GridView inside ScrollView will make them becoming difficult to scroll and can not be expanded. Android doesn’t provide a built in API to handle this problem so we have to do a little hack by subclassing those two views.

This is hack i found on stackoverflow, from Neil Traft ‘s answer:

ExpandableHeightListView

package net.londatiga.android.widget;

import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ListView;
import android.content.Context;

public class ExpandableHeightListView extends ListView
{

boolean expanded = false;

public ExpandableHeightListView(Context context)
    {
        super(context);
    }

    public ExpandableHeightListView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public ExpandableHeightListView(Context context, AttributeSet attrs,
            int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public boolean isExpanded()
    {
        return expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        // HACK! TAKE THAT ANDROID!
        if (isExpanded())
        {
            // Calculate entire height by providing a very large height hint.
            // But do not use the highest 2 bits of this integer; those are
            // reserved for the MeasureSpec mode.
            int expandSpec = MeasureSpec.makeMeasureSpec(
                    Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);

            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        }
        else
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void setExpanded(boolean expanded)
    {
        this.expanded = expanded;
    }
}

ExpandableHeightGridView

package net.londatiga.android.widget;

import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.GridView;
import android.content.Context;

public class ExpandableHeightGridView extends GridView
{

 boolean expanded = false;

 public ExpandableHeightGridView(Context context)
 {
 super(context);
 }

 public ExpandableHeightGridView(Context context, AttributeSet attrs)
 {
 super(context, attrs);
 }

 public ExpandableHeightGridView(Context context, AttributeSet attrs,
 int defStyle)
 {
 super(context, attrs, defStyle);
 }

 public boolean isExpanded()
 {
 return expanded;
 }

 @Override
 public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
 {
 // HACK! TAKE THAT ANDROID!
 if (isExpanded())
 {
 // Calculate entire height by providing a very large height hint.
 // But do not use the highest 2 bits of this integer; those are
 // reserved for the MeasureSpec mode.
 int expandSpec = MeasureSpec.makeMeasureSpec(
 Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
 super.onMeasure(widthMeasureSpec, expandSpec);

 ViewGroup.LayoutParams params = getLayoutParams();
 params.height = getMeasuredHeight();
 }
 else
 {
 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 }
 }

 public void setExpanded(boolean expanded)
 {
 this.expanded = expanded;
 }
}

How to use:

//ListView
ExpandableHeightListView listView = new ExpandableHeightListView(this);

listView.setAdapter(adapter);
listView.setExpanded(true);

//GridView
ExpandableHeightGridView gridView = new ExpandableHeightGridView(this);

gridView.setNumColumns(4);
gridView.setAdapter(adapter);
gridView.setExpanded(true);

As you can see, to make the ListView or GridView expandable, just call the setExapanded(true).

[ad#ad-720×90]

Facebooktwitterredditpinterestlinkedinmailby feather

Related posts:

  1. How to Make Android Map Scrollable Inside a ScrollView Layout
  2. Simple JSON RPC Client for Android
  3. How to Programmatically Show and Hide Soft Keyboard in Android
  4. Sample Code to Get MD5 Checksum of a File in Android

Filed Under: Android Tagged With: expandable, gridview, listview, scrollable

About Lorensius Londa

Passionate web and mobile application developer. Co-founder of TRUSTUDIO, loves programming, Android, aviation, travelling, photography, coffee and gym mania.

Reader Interactions

Comments

  1. Igor Ganapolsky says

    June 9, 2014 at 9:11 pm

    This approach works for GridView inside a ScrollView. Thanks for the workaround!

    Reply
  2. kleon says

    July 25, 2014 at 7:52 pm

    Hello,
    very nice video!
    i want to make exact same as shown in your video.
    Can you provide some resources that can help me do it.
    I use web service to fetch images and want to display them like google play , one big in 1st row, 2 smaller in 2nd row and so..
    I bit confused with the appropriate layout files

    Thanks in advance

    Reply
  3. Yash Tripathi says

    July 26, 2014 at 1:05 am

    hello i am new in android development and i placed this code

    ExpandableHeightListView listView = new ExpandableHeightListView(this);
     
    listView.setAdapter(adapter);
    listView.setExpanded(true);

    inside oncreate method but its not work for me please help me and provide full code of this example

    Reply
  4. Nilay Sheth says

    July 29, 2014 at 2:41 pm

    Very Good tutorial Thanks a lot bro. Can you also add the source code in this tutorial too ?

    Reply
  5. billiout says

    August 8, 2014 at 8:09 pm

    Does this work also on ExpandableListView?

    Reply
  6. Mahmoud ELshamy says

    August 27, 2014 at 5:39 am

    Thank you, It helped me so much.

    Reply
  7. dharmesh says

    September 11, 2014 at 11:53 am

    I m facing the problem in this when i implement onitemclick listner. i mean its not working which u given example

    can u plz tell me how to do that?

    Thnak You Very Much.

    Reply
  8. Irwanto says

    October 15, 2014 at 4:42 am

    membantu banget 🙂

    Reply
  9. Archish Thakkar says

    December 10, 2014 at 1:53 pm

    Please provide me download link of source code. I need source code.

    Thanking You,
    Archish Thakkar

    Reply
  10. Archish Thakkar says

    December 10, 2014 at 1:54 pm

    Please provide download link of the following code.I need source code.

    Reply
  11. Giorgio Zamparelli says

    February 9, 2015 at 6:51 pm

    Hi, thanks for the post.
    When I add many items to the ExpandableHeightGridView the scrollbar is automatically scrolled down.
    Anyway to avoid this?

    Reply
  12. morteza Amzajerdi says

    February 19, 2015 at 9:46 pm

    Thank you , that was very useful.

    Reply
  13. Ravi says

    October 13, 2016 at 10:42 pm

    I have some checkbox in custom grid view , But it is not showing full content last row is not visible , could you please help?

    Reply
  14. Sarith NOB says

    August 28, 2017 at 1:20 am

    Oh my god!
    Your post save much my time, whenever I don’t know about it even a little bit.

    Thank you very much!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About Me

A husband, father of two, passionate software developer, diy lover and home baker who loves to learn new things. Read More…

  • Facebook
  • GitHub
  • Google+
  • Instagram
  • Twitter
  • YouTube

Featured Articles

How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Blue Bamboo P25 Printer Android Demo Application With Source Code

Simple JSON RPC Client for Android

How to Send Message to Google Cloud Messaging (GCM) Server Using JSON and PHP

Footer

Recent Comments

  • Aditya Dabas on About
  • Ayten Göksenin Barutçu on How to Make Android Map Scrollable Inside a ScrollView Layout
  • mang jojot on About
  • Hussain on How to Programmatically Scan or Discover Android Bluetooth Devices

Recent Posts

  • How to Fix Blank Screen on WordPress Add/Edit Post Page
  • How to Programmatically Restart the ESP32 Board
  • How to Get Hardware Info of ESP32
  • How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Latest Tweets

  • @tricahyono_bowo @infobandung @infobdg Wah kejauhan om 355 days ago
  • Wilujeng enjing sadayana..Mohon info tempat powder coating dan sandblasting yg recommended di Bandung dunk @infobandung @infobdg359 days ago

Copyright © 2023 · Magazine Pro on Genesis Framework · WordPress · Log in