Search code examples
androidkotlinandroid-jetpack-composeandroid-widgetglance-appwidget

Jetpack Compse Glance Widget API 33 is not resizable on API 30 works


I'm creating app widget with glance. And on my personal phone API 30 resizable works on emulator API 33 or 32 resizable doesn't work.

Is that emulator issue? But Google Chrome widget resizable works.

xml/widget_info.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:description="@string/app_name"
    android:initialLayout="@layout/loader"
    android:minWidth="80dp"
    android:minHeight="80dp"
    android:minResizeWidth="40dp"
    android:minResizeHeight="40dp"
    android:previewImage="@drawable/widget"
    android:resizeMode="horizontal|vertical"
    android:widgetCategory="home_screen" />

xml-v31/widget_info.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:previewLayout="@layout/widget_preview"
    android:targetCellWidth="3"
    android:targetCellHeight="2"
    android:description="@string/widget_description"/>

glance app widget

import androidx.compose.runtime.Composable
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.glance.GlanceModifier
import androidx.glance.ImageProvider
import androidx.glance.LocalContext
import androidx.glance.LocalSize
import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.GlanceAppWidgetReceiver
import androidx.glance.appwidget.SizeMode
import androidx.glance.appwidget.cornerRadius
import androidx.glance.background
import androidx.glance.layout.fillMaxSize
import androidx.glance.layout.padding
import androidx.glance.state.GlanceStateDefinition
import androidx.glance.state.PreferencesGlanceStateDefinition

class MyWidget : GlanceAppWidget() {
    override var stateDefinition: GlanceStateDefinition<*> = PreferencesGlanceStateDefinition

    override val sizeMode: SizeMode = SizeMode.Responsive(
        setOf(SMALL_SQUARE, HORIZONTAL_RECTANGLE, BIG_SQUARE)
    )

    @Composable
    override fun Content() {
        val size = LocalSize.current
        val context = LocalContext.current
        MyContent(
            modifier = GlanceModifier
                .fillMaxSize()
                .background(ImageProvider(R.drawable.bg_widget))
                .cornerRadius(16.dp)
                .padding(8.dp)
        )
    }

    companion object {
        const val WATER_WIDGET_PREFS_KEY = "WATER_WIDGET_PREFS_KEY"

        private val SMALL_SQUARE = DpSize(100.dp, 100.dp)
        private val HORIZONTAL_RECTANGLE = DpSize(250.dp, 100.dp)
        private val BIG_SQUARE = DpSize(250.dp, 250.dp)
    }
}

class MyWidgetReceiver : GlanceAppWidgetReceiver() {
    override val glanceAppWidget: GlanceAppWidget = MyWidget ()
}

AndroidMAnifest.xml

        <receiver
            android:name=".ui.widget.MyWidgetReceiver"
            android:enabled="@bool/glance_appwidget_available"
            android:exported="false">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_info" />
        </receiver>

API 33 | my widget | Google Chrome widget | | -------- | -------- | | API 33 my non resiazable | API 33 Google Chrome resizable widget |

API 30 on this phone I have resizable indicators and is also resizable.

enter image description here


Solution

  • There is no hierarchy between files inside the XML folder. It's not like styles.

    You can either duplicate all fields in xml-v31 or just add everything in the base XML and ignore the warnings of higher API.