Skip to content Skip to sidebar Skip to footer

How To Work With Expandablelistview?

Wellcome all I have a ExpanableListView that i'm testing. I have a problem with the expand icon of the items. The icon is being painted overlaying the text title of each item. It m

Solution 1:

I remember running into a similar issue in one of my own apps.

In your getGenericView method, you can use TextView.setPadding to specify the left padding of the text so that it appears further to the right, and is not overlapped by the icon. Since the method sets the padding in pixels (which will vary depending on the screen's density), it is important to convert the pixels to density-independent pixels so that the padding and appearance is standardized from screen to screen.

Here's a full example of the method from my own code:

public TextView getGenericView() {
    // Padding values (in pixels)intleft=36; // to avoid being overlapped by the iconintright=0; // not neededinttop=15; // space out the list a bit moreintbottom= top; // space out the list a bit more// Get density of screen to convert px to dpfloatscale= m_Context.getResources().getDisplayMetrics().density;

    // Layout parameters for the ExpandableListView
    AbsListView.LayoutParamslp=newAbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    TextViewtextView=newTextView(MyActivity.this);
    textView.setLayoutParams(lp);
    // Center the text vertically
    textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
    // Set the text starting position
    textView.setPadding((int)(left * scale), (int)(top * scale), right, (int)(bottom * scale)); // left, top, right, bottom
    textView.setTextSize(20);
    return textView;
}

If you have multiple ExpandableListViews throughout your app, it may be better to specify the pixel sizes in the your app resources (res/values/dimen.xml) rather than here at the top of getGenericView, so that it is defined (and can be changed) in one place and you can reference it in all the lists.

Hope this helps!

Post a Comment for "How To Work With Expandablelistview?"