Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: ListView, problem with rounded corners

I have ListView with rounded corners implemented like bellow.

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/list_bg"
    android:divider="#dbdbdb"
    android:dividerHeight="1sp"
    android:cacheColorHint="#00000000"
    android:descendantFocusability="afterDescendants"
    android:scrollbars="vertical">
</ListView>

where list_bg.xml is:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#ffffff"/>
    <corners android:bottomLeftRadius="10dp"
            android:bottomRightRadius="10dp" 
            android:topLeftRadius="10dp"
            android:topRightRadius="10dp"
            />
</shape>

This give me rounded corners. The problem is that each items are RelativeLayout with TextView on the left side and ImageButton on the right side. Both views have custom graphic on pressed, foucused and default state. Something like this.

+------------------------------------+
|      TextView        | ImageButton |
+------------------------------------+

The problem is that custom graphic cover ListView background and thus rounded corners on first and last item. Everything is okay when scrolling.

I've written custom adapter and in getView method I set proper bg for items.

@Override
public View getView(int position, View view, ViewGroup parent) {        
    ...
    if(position == 0) {
        mItemView.setBackgroundResource(R.drawable.cell_item_first_selector);
        mImgBtn.setBackgroundResource(R.drawable.cell_btn_first_selector);
    }
    else if (position == mData.size() -1) {
        mItemView.setBackgroundResource(R.drawable.cell_item_last_selector);
        mImgBtn.setBackgroundResource(R.drawable.cell_btn_last_selector);
    } 
    else {
        mItemView.setBackgroundResource(R.drawable.cell_item_def_selector);
        mImgBtn.setBackgroundResource(R.drawable.cell_btn_def_selector);
    }
    ... 
}

It works, but I'm not sure that is good solution. Is there any other way to automatically round corners in Android Views, not just by setting background?

like image 996
Adrian Avatar asked Jun 25 '26 20:06

Adrian


1 Answers

public class ClippedListView extends ListView {

/**
 * @param context
 */
public ClippedListView(Context context) {
    super(context);
}

/**
 * @param context
 * @param attrs
 */
public ClippedListView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void dispatchDraw(Canvas canvas) {
    float radius = 10.0f;
    Path clipPath = new Path();
    RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
    clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
    canvas.clipPath(clipPath);
    super.dispatchDraw(canvas);
}
}

First clipping was not working, then using

setLayerType(View.LAYER_TYPE_SOFTWARE, null)

on my clippedListView solve the issue. My items' background are not messing with my corners anymore!

like image 87
Michel-F. Portzert Avatar answered Jun 27 '26 10:06

Michel-F. Portzert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!