You are currently viewing Set Max Height of ListView using coding in Xamarin.Android
Xamarin

Set Max Height of ListView using coding in Xamarin.Android

5
(3)

Set Max Height of ListView using coding in Xamarin.Android

Related to : Set Max Height of ListView Xamarin.Android. If you want to set a maximum height of ListView using coding in Xamarin.Android, you need to implement the following code.

//set list height
                               ViewGroup.LayoutParams layoutParams = (ViewGroup.LayoutParams)listview.LayoutParameters;
                               layoutParams.Height = 300;
                               listview.LayoutParameters = layoutParams;

Here listview is your ListView variable. You can change the height as per the requirement and you can also implement some condition like

if(adapter.Count>5)
                           {
                               
                               //set list height
                               ViewGroup.LayoutParams layoutParams = (ViewGroup.LayoutParams)listview.LayoutParameters;
                               layoutParams.Height = 300;
                               listview.LayoutParameters = layoutParams;
                           }

Means code is only implemented when the item count is more than 5.

Here is the another example to set ListView height depending on the items.

if (adapter.Count > 5)
                            {
                                View item = adapter.GetView(0, null, listview);
                                item.Measure(0, 0);
                                //set list height
                                ViewGroup.LayoutParams layoutParams = (ViewGroup.LayoutParams)listview.LayoutParameters;
                                layoutParams.Height = (int)(5.5 * item.MeasuredHeight);
                                deviceList.LayoutParameters = layoutParams;
                            }

Here “adapter” is a List adapter for the listview and 5.5 means numbers of items you want to display in the list without scrolling. You can change this value as per your requirements.

Follow this blog for more .Net and Xamarin related posts. You can also visit Xamarin Category for Xamarin related posts.

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 3

No votes so far! Be the first to rate this post.

As you found this post useful...

Share this post on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?