Android Widget with Glance make around corners for SDK lower than 31

Viewed 458

Announcing Jetpack Glance Alpha for app widgets. However rounded corners do not work for SDK lower than 31 (Android 12). Ideally, you would want to change the background color with rounded corners programmatically.

Does anyone have an idea for a workaround for this issue?

2 Answers

I faced this issue. And, I solved the problem with background drawable. Give background drawable to your root.

MyWidgetClass.kt

    @Composable
    override fun Content() {
        Column(
            modifier = GlanceModifier
                .width(170.dp).height(130.dp)
                .background(ImageProvider(R.drawable.background_widget))
                .padding(8.dp)
        ) {
            //...
        }
    }

background_widget.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFFFF" />

    <corners android:radius="10dp" />
</shape>

An easy way to add rounded corners is this, but it works for Android S +.

@Composable
override fun Content() {
    Column(
        modifier = GlanceModifier
            .width(170.dp).height(130.dp)
            .background(color = Color.Gray)
            .cornerRadius(8.dp)
    ) {
        //...
    }
}
Related