How can I resize a widget on an android application by using AppWidgetHostView?

1.2k Views Asked by At

I created an application that has a widget. I would like to resize the widget but it does not work. The widget size does not change between w/ and w/o widgetView.updateAppWidgetSize(options, 100, 100, 200, 200). Why does updateAppWidgetSize not work? How can I change the widget size?

MainActivity:

class MainActivity : AppCompatActivity() {

    private lateinit var appWidgetManager: AppWidgetManager
    private lateinit var appWidegetHost: AppWidgetHost

    private val REQUEST_BIND_APPWIDGET = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        appWidgetManager = AppWidgetManager.getInstance(this)
        appWidegetHost = AppWidgetHost(this, 12345).apply {
            startListening()
        }

        val appWidgetId = appWidegetHost.allocateAppWidgetId()
        val appWidgetInfo = appWidgetManager.installedProviders.get(5)

        if (appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, appWidgetInfo.provider)) {
            addAppWidget(appWidgetId, appWidgetInfo)
        } else {
            val intent = Intent(AppWidgetManager.ACTION_APPWIDGET_BIND)
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
            startActivityForResult(intent, REQUEST_BIND_APPWIDGET)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_BIND_APPWIDGET) {
                val appWidgetId = data!!.getIntExtra(
                    AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID
                )
                if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
                    val appWidgetInfo = appWidgetManager.getAppWidgetInfo(appWidgetId)
                    addAppWidget(appWidgetId, appWidgetInfo)
                }
            }
        } else { }
    }

    override fun onStop() {
        super.onStop()
        appWidegetHost.stopListening()
    }

    private fun addAppWidget(appWidgetId: Int, appWidgetInfo: AppWidgetProviderInfo) {
        val widgetView = appWidegetHost.createView(this, appWidgetId, appWidgetInfo)
        val options = appWidgetManager.getAppWidgetOptions(appWidgetId)
        widgetView.updateAppWidgetSize(options, 100, 100, 200, 200)
        widgetView.setAppWidget(appWidgetId, appWidgetInfo)
        widgets.addView(widgetView)
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black"
        tools:context=".MainActivity" android:orientation="vertical">

        <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"     
                android:id="@+id/widgets">
        </LinearLayout>

 </LinearLayout>
1

There are 1 best solutions below

2
On

The updateAppWidgetSize() method only affects the min and max dimensions, not the current dimension.

To change the widget size, do it how you would any other View, by modifying the LayoutParams:

widgetView.layoutParams = widgetView.layoutParams.apply {
    width = /* your width */
    height = /* your height */
}