how to set ListView item height
Asked Answered
W

4

11

I have a ListActivity which i want to change Item height refer to device height

public class MainActivity extends ListActivity
{
            .
            .
            .
            setListAdapter(new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_list_item_1,mainMenuItems));
}

its my ListActivity on galaxy nexus S :

enter image description here

and on galaxy nexus 7 : enter image description here

how should I define listview items height ???

Welcy answered 19/4, 2013 at 10:59 Comment(0)
G
26

The ListView items height are the height of item layout contents, eg android.R.layout.simple_list_item_1 height. If you want different height, create an xml file and put a android:layout_height or android:minHeight attribute.

This is a modified simple_list_item_1 which refers to your value

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
    android:paddingRight="?android:attr/listPreferredItemPaddingRight"
    android:minHeight="@dimen/your_defined_minumum_height"/>

and in your dimen.xml

<dimen name="your_defined_minumum_height">48dp</dimen>
Glynn answered 19/4, 2013 at 11:4 Comment(0)
S
6
ListView lvLogs = new ListView(this);
lvLogs.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, logs) {

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            TextView textView = ((TextView) view.findViewById(android.R.id.text1));
            textView.setMinHeight(0); // Min Height
            textView.setMinimumHeight(0); // Min Height 
            textView.setHeight(44); // Height
            return view;
        }
    });

Note: MinHeight and MinimumHeight is required!!!

Set the minimum height to 0~20(or higher) : setMinHeight(?) + setMinimumHeight(?)

Then the TextView in ListView will have a normal height (smaller than the ListView's default)

If you want to specific the height in pixel, then use setHeight(?)

Schoening answered 22/8, 2015 at 3:25 Comment(0)
U
3

You need to create your own custom layout file for each item in ListView. There you can edit whatever you want, and you can get a lot of benefits.

Unthoughtof answered 19/4, 2013 at 11:2 Comment(0)
B
0

Customise an adapter for your ListView with the information of screenHeight (H). Given that you want your screen to show 9 columns (items), you can specify the height of each of them as H/9. By doing so, you can precisely control the appearance of your ListView on all devices.

Burglarize answered 4/7, 2016 at 2:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.