Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 841
Andrea Mario Lufino Avatar asked Sep 03 '12 14:09

Andrea Mario Lufino


1 Answers

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.

like image 146
se_bastiaan Avatar answered Sep 25 '22 10:09

se_bastiaan