SRG
SRG

Reputation: 75

How can i change ListView item height in android

I am trying to change height of items in my listview but unable to do that please help.

below is my activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.sagar.javainterviewquestion.MainActivity">
    <!--<include layout="@layout/header"
        android:id="@+id/include" />-->

    <ListView
        android:layout_width="wrap_content"
        android:id="@+id/listView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="#00a8a8"
        android:backgroundTintMode="src_over"
        android:clickable="false"
        android:divider="#9f9b9b"
        android:dividerHeight="3dp"
        android:drawSelectorOnTop="false"
        android:fastScrollAlwaysVisible="false"
        android:fastScrollEnabled="false"
        android:focusableInTouchMode="true"
        android:longClickable="false"
        android:nestedScrollingEnabled="false"
        android:choiceMode="singleChoice"
        android:layout_height="wrap_content" />

</RelativeLayout>

Upvotes: 0

Views: 2988

Answers (2)

Bilal Rafique
Bilal Rafique

Reputation: 294

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>

Upvotes: 0

Alexandre Martin
Alexandre Martin

Reputation: 1502

If you use a default adapter such as ArrayAdapter and not a custom adapter with his own layout, and if you use a xml layout such as simple_list_item_un, then you should create your own layout file with the desired padding around the TextView and set the adapter with your custom layout.

<TextView
    android:paddingTop="20dp"
    android:paddingBottom="20dp" />

mytextview.xml

ArrayAdapter<String> ad = new ArrayAdapter<String>(context,R.layout.mytextview, myArray);

listView.setAdapter(ad);

MainActivity.java

Upvotes: 1

Related Questions