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

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.