Android - How to set padding for TextView in ListView or ExpandableListView

Andrea Mario Lufino picture Andrea Mario Lufino · Sep 3, 2012 · Viewed 62.7k times · Source

I need to set padding for the TextView in every row of ListView or ExpandableListView. I try to use android:padding and child (paddingLeft,...) but without any results. How can I do? Thanks

EDIT:

This is the code of the item of the ExpandableListView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="20dp" >

    <TextView 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content"
        android:id="@+id/titleScheda"
        android:text="TextView"
        android:layout_width="wrap_content" 
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:padding="20dp" />

</LinearLayout>

Setting padding on both layout and TextView doesn't work, even if the padding is set in only one of the 2 tags.

EDIT :

Solved. The problem was in the Java code, in the SimpleExpandableListAdapter declaration. Using the standard layout it's impossible to customize, but using a custom layout for the expandable content is the solution that allows us to customize anything.

Answer

se_bastiaan picture se_bastiaan · Sep 3, 2012

I assume you're adding the items in the listview using a template file (xml layout). If you're not, you have to implement that first.

In that layout file you could do this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp" >
    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text" />
</LinearLayout>

The LinearLayout will create the padding and wrap around the TextView.