Pages

Subscribe:

Labels

Wednesday, 28 September 2011

Dynamically change the ListView height - Android

This post is used to change the ListView height. This is done depends on the childviews .
 This Utility.java class is like this


 import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.widget.ListAdapter;
import android.widget.ListView;

public class Utility {
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }
}



In the main Acticity use this Utility class to change the listview height.

 phraseListView=(ListView)findViewById(R.id.phrase_listview);
 phraseAdapter=new PhraseListAdapter(this);
phraseListView.setAdapter(phraseAdapter);
Utility.setListViewHeightBasedOnChildren(phraseListView);
       

34 comments:

Abarshini said...

Thanks for providing really nice information



Android Training

Jesus Alberto said...

Thank you very much!

Vishal gupta said...

ooo guys u rock it

soumen mohar said...

thank u

Slackneo said...

OMG!!! Thank u!!!!!

John Victor said...

Wonderful blog.. Thanks for sharing informative blog.. its very useful to me..
Android Training in Chennai

isabellaJoseph said...

LEARN | GET JOB | HAPPY LIFE @ TIS Academy
Thank you so much for sharing. it’s useful for me.
Dot Net Training in Chennai
Android Training in Chennai

Dot Net Training in Chennai


Swathy Vignesh said...

hi welcome to this blog. really you have post an informative blog. it will be really helpful to many peoples. thank you for sharing this blog.
java training in chennai

Swathy Vignesh said...

hi welcome to this blog. really you have post an informative blog. it will be really helpful to many peoples. thank you for sharing this blog.
java training in chennai

Logavani G said...

hi welcome to this blog. really you have post an informative blog. it will be really helpful to many peoples. thank you for sharing this blog.
selenium training in chennai

Nandhini said...

Thanks for taking time to share this post.It is really useful.Continue sharing more like this.
Regards,
Android Training in Bangalore

john stany said...


Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..
Android App Development Company

Sharon Sandy said...

great and nice blog thanks sharing..I just want to say that all the information you have given here is awesome...Thank you very much for this one.
web design Company
web development Company
web design Company in chennai
web development Company in chennai
web design Company in India
web development Company in India

isabella jacob said...


Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing..
Mobile App development Company
Ios App development Company

Cynix Technologies said...

Good information. Thankyou for sharing Android online training

BHARGAV B.L said...

Thank you for sharing sir/madam. You saved my day.

soumya Reddy said...

Wonderful content Thanks for sharing Android Online Training

nutana meka said...

Check it once through Android Online Training Bangalore for more information on Android Courses.

Rajashri Mali said...

Very Useful information Thank you !!
#TWB_ #Blended #Learning and #Technical #Content #Training is delivered through the high impact high-intensity ILT training combined with the low-intensity sustainable intervention. This allows for thousands of trainees to be trained with the same effectiveness at the same time. TWB_ Blended Learning | Technical Content Training

soumya Reddy said...

Nice blog, check it once through Android Online Training for more information.

Pavel Co Ebele said...

Very much useful article. Kindly keep blogging

Java Training in Chennai

Java Online Training India

nutana meka said...

Great,Android Online Training

Jagjeet Kirarkot said...

Thank you for sharing. Any more information visit hereAndroid Training institute in Delhi | Android Trainer in Gurgaon

Petter Marry said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

Manasa Ala said...

Android Online course is a good blog
Android Online Training

Brn Infotech said...
This comment has been removed by the author.
Brn Infotech said...

Awesome,
Thank you so much for sharing such an awesome blog...
ios app development course

suresh H said...

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
Hadoop Training in Chennai
Big Data Training in Chennai
Python Training in Chennai
Python Training Centers in Chennai
Data Science Training in Chennai
Data Science Course in Chennai
Data Analytics Training in Chennai
Best AngularJS Training in Chennai
AngularJS Training in Chennai
QlikView Training in Chennai
Informatica Training in Chennai

Anu. SSDN said...

The blog was absolutely fantastic! Lot of great information and helpful.It is amazing and wonderful to visit your site. I have read your blog its very attractive and impressive. I like it your blog.We are provide Android Training institute in Delhi. thanks for sharing this blog..

nivedhitha reddy said...

Thanks for information Hadoop developer training

Naveen Yadav said...

amazing information in this blog . Thanks for sharing
Android Training in Delhi | Android Corporate Training Delhi

soumya Teja said...

Thanks for your useful information shared with us. check it once through Android Online Course Hyderabad

Anuj Singh said...

Nice blog. Thanks for sharing such great information.Inwizards Inc is a xamarin Development company offers quality xamarin development services best in web industries. Intrested click here - Xamarin Development Company india

Mahesh said...


Thanks, Learned a lot of new things from your post! Good creation and HATS OFF to the creativity of your mind.
Very interesting and useful blog!
Android Training in Gurgaon


Post a Comment