20

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.

5
  • @Tomer the java code? in the java there isn't any reference to the padding or other. I have only the code which need to populate the ExpandableListView Sep 4, 2012 at 9:15
  • i can't think anything else, but i would try investigate by setting different background to layout and the textView, and see the results, post the results here
    – Tomer Mor
    Sep 4, 2012 at 9:18
  • @Tomer change the background color, right? Sep 4, 2012 at 9:19
  • yes like this android:background="@android:color/white"
    – Tomer Mor
    Sep 4, 2012 at 9:20
  • Done, but no effect. Looking at the java code i noticed that i used android.R.layout.simple_expandable_list_item_1 and not my custom layout. The error is there i think, right? Sep 4, 2012 at 9:27

3 Answers 3

17

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.

1
  • Doesn't work, i'm using an ExpandableListView. I even try to set android:padding in TextView tag, but without results. Sep 4, 2012 at 8:58
4
for(int i=0; i< listview.getChildCount();i++) 

  listview.getChildAt(i).setPadding(10,10,10,10);
0
1

You can add padding as follows:

android:paddingBottom="20pt"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.