Erfan Bagheri Erfan Bagheri - 5 months ago 56
Android Question

how to set ListView item height

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 ???

Answer

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 layout_height or 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>