[Android] Dynamically set the height of ListView with copy (for beginners)

How to set the height of ListView is surprisingly complicated.

With wrap_contents, it becomes one, or elements overflow and overflow depending on the device ....

Even if I look it up, I can find a lot of it, but I thought that it wouldn't be easy to do because there were many things that were unexpectedly confusing, such as deep reference, collapse without being aware of the life cycle.

Therefore, it can be easily implemented by using the function described later.

All you have to do is apply the Adapter, prepare this function for the Listview that has the data you want to display set, and pass the Listview as an argument.

If you want to change a part of the height dynamically, I think that it will be cleaner if you add control syntax etc. in the for statement.

Java

ListViewUtil.java




public static void setListViewHeightBasedOnChildren(ListView listView) {
    
    //Get ListAdapter
    ListAdapter listAdapter = listView.getAdapter();

    if (listAdapter == null) {
        //null check
        return;
    }

    //Initialization
    int totalHeight = 0;

    //Measure the height of each item and add
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    //Get LayoutParams
    ViewGroup.LayoutParams params = listView.getLayoutParams();

    //(Separator height*Number of elements)I'll just add
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));

    //I'll set height to LayoutParams
    listView.setLayoutParams(params);
}

Kotlin

ListViewUtil.kt


fun setListViewHeightBasedOnChildren(listView:ListView) {

  //Get ListAdapter
  val listAdapter = listView.getAdapter()
  if (listAdapter == null)
  {
    //null check
    return
  }

  //Initialization
  val totalHeight = 0

  //Measure the height of each item and add
  for (i in 0 until listAdapter.getCount())
  {
    val listItem = listAdapter.getView(i, null, listView)
    listItem.measure(0, 0)
    totalHeight += listItem.getMeasuredHeight()
  }

  //Get LayoutParams
  val params = listView.getLayoutParams()

  //(Separator height*Number of elements)I'll just add
  params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1))

  //I'll set height to LayoutParams
  listView.setLayoutParams(params)
}

If there is a better implementation method, please teach me.

Reference article: Android Listview Measure Height

Recommended Posts

[Android] Dynamically set the height of ListView with copy (for beginners)
[Android] [Java] Manage the state of CheckBox of ListView
The story of tuning android apps with libGDX
Android Studio development for the first time (for beginners)
[For beginners] Quickly understand the basics of Java 8 Lambda
<Android> Change the background color of the List row of ListView
Automatically set the width and height of the UI Label
Automatically adjust the height of the WebView dialog with the size of the content
I tried to make it possible to set the delay for the UDP client of Android by myself
I will explain the nesting of for statements that kill beginners
The basics of the process of making a call with an Android app
[Swift] How to dynamically change the height of the toolbar on the keyboard
[Android Studio] Set an arbitrary image for the application background [Java]
[Eclipse / github] Let's share the project created with eclipse on github For beginners
[Android Studio] [For beginners] Let's roughly explain the screen and directories
[For beginners] Personally thinking about the cause of the error-the idea of ​​the solution
About the basics of Android development
[For beginners] Summary of java constructor
Environment construction with Docker for beginners
Links for creating Android apps (for beginners)
Notes for Android application development beginners
Memorandum of Understanding for Using Appium desktop Inspector with Emulator (Android App)
Set the number of seconds for fast forward and rewind in ExoPlayer
[Ruby] Explanation for beginners of iterative processing with subscripts such as each_with_index!
The story of releasing the Android app to the Play Store for the first time.
[Rails] Articles for beginners to organize and understand the flow of form_with
(Note) Get a set of dependent library jars with the help of Gradle