Goofy
Goofy

Reputation: 6128

Android List view fill the height to the contents

i have a list view defined in the xml, now i am setting the content view setContentView(R.layout.topic_layout); , i have 5 items in it,currently its filling only half the height of the list view but i want it to completely fill the height so that i dont have any space at the bottom.

i have searched for it but couldnt find any solution, please help me to acheive this :

and also i am setting the adapter like this:

adapter = new MyAdapter(this);
        if (adapter != null) {
        setListAdapter(adapter);

        }

Upvotes: 1

Views: 8902

Answers (3)

MJim
MJim

Reputation: 39

This is what I use to do:

  1. Set your xml listview height to match_parent instead of wrap_content, in order to fill all the available space.
  2. Set the minimum height of each item in the listview to match the listview height divided by the number of elements.

Activity Layout:

(...)
<ListView
android:id="@+id/myListView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
(...)

ArrayAdapter for the Listview:

private class MyAdapter extends ArrayAdapter<MyObject> {

    (...)

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyObject myobject = getItem(position);
        ObjectHolder oh;
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) myContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.myitemlayout, parent, false);
        // This is the important line:
            convertView.setMinimumHeight(parent.getHeight()/getCount());
        (...)

        return convertView;
    }
}

Upvotes: 0

yahya
yahya

Reputation: 4860

I totally agree with @Aswin, using LinearLayout with layout_weight property would be your solution. But if you insist to use listView, i can offer you a workout which is not so recommended.

You can get your screenHeight when activity created by using these codes:

Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
display.getMetrics(dm);
int height = dm.heightPixels;
int listItemHeight = height / YOUR_ITEM_COUNT;

Then you can use this listItemHeight in your listAdapter, by setting inflated view's height on getView method.

Upvotes: 0

Aswin Rajendiran
Aswin Rajendiran

Reputation: 3409

If you have a fixed number of items and want them to stretch all the way to the end of the screen, ListView is not the best choice for you. Use a LinearLayout which takes up all the space and add all the items to it. This is assuming you want the items to take up all the space every time.

Using LinearLayout, you can spread the items out evenly without doing any calculations yourself.

LinearLayout linearLayout = new LinearLayout(getSupportActivity());
linearLayout.setOrientation(android.widget.LinearLayout.VERTICAL);

RelativeLayout.LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT);

for (int i = 0; i < 5; i++) {
    View individualView = new View(getSupportActivity());
    // Create your custom view here and add it to the linear layout
    // Leave the height as 0, LinearLayout will calculate the height properly.
    params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0);
    additionalOption.setLayoutParams(params);

    // As a we are adding it to the linear layout, they will all have a weight of 1, which will make them spread out evenly.
    linearLayout.addView(additionalOption);
}
mainView.addView(linearLayout);

EDIT: If you have already implemented it with ListView and is troublesome to change it, you can do the following.

Make sure the list view width and height are set to match_parent in the xml. Then in getView() of the adapter where you create your custom view, do the following

// Get the height of the ListView
int totalHeight = listView.getHeight();
int rowHeight = totalHeight/getCount(); // Divide by number of items.

// Create custom view with the height calculated above.

Be careful about the totalHeight being 0. If you create the ListView in onCreate() and set the adapter in onCreate() as well, the ListView will most likely not have the width or height calculated yet. Try setting the adapter in onResume() instead. By this point, the dimensions of the ListView would have been calculated and laid out on the screen.

Hope this helps.

Upvotes: 4

Related Questions