Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: shape corners do not work when setting individual corners

Tags:

android

I need to have a background which has rounded bottom left/right coners(but not top left/right ones), below is my xml file:

<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle" android:padding="1dp">
        <solid android:color="#FFbdbebd"/>
        <corners
            android:bottomLeftRadius="12dip"
            android:bottomRightRadius="12dip"
            android:topLeftRadius="0dip"
            android:topRightRadius="0dip"/>
    </shape>
</item>
</layer-list>

But the result is a plain rectangle without any corner rounded, if I only use:

android:radius="12dip"

then all corners are rounded, I searched and found a bug related to this:

http://code.google.com/p/android/issues/detail?id=9161

but the bug states:

Left/right is switched, because android:bottomRightRadius="2dp" turned out to specify left-bottom rounded corner.

which may not be relavent to my issue, I also tried to use:

android:radius="12dip"

followed by

android:topLeftRadius="0dip"
android:topRightRadius="0dip"

without success.

Can anyone help? Thanks!

like image 700
hzxu Avatar asked May 24 '11 03:05

hzxu


2 Answers

The above solutions didn't work for me, but I found a solution online that did work: (https://medium.com/@iamsadesh/android-ui-creating-a-layout-rounded-only-in-the-top-d60514ccab77)

This is for rounding just the top corners:

val image = findViewById<ImageView>(R.id.image)
val curveRadius = 20F

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

    image.outlineProvider = object : ViewOutlineProvider() {

        @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
        override fun getOutline(view: View?, outline: Outline?) {
            outline?.setRoundRect(0, 0, view!!.width, (view.height+curveRadius).toInt(), curveRadius)
        }
    }

    image.clipToOutline = true

}
like image 173
jc12 Avatar answered Sep 16 '22 15:09

jc12


Change this:

 <corners 
        android:bottomRightRadius="12dp" 
        android:bottomLeftRadius="12dp"
        android:topLeftRadius="0dp" 
        android:topRightRadius="0dp"/>

to this:

 <corners 
        android:radius="1dp"
        android:bottomRightRadius="12dp" 
        android:bottomLeftRadius="12dp"
        android:topLeftRadius="0dp" 
        android:topRightRadius="0dp"/>

and it should be working as expected.

like image 28
Franklin Avatar answered Sep 19 '22 15:09

Franklin