jojo
jojo

Reputation: 21

How to get the Widget state/view using the widget id in android?

I have a created a simple widget, which have a text view in it, which gets updated after n time with some new string. Now when user will click on that widget i want that text which is currently displayed in that text view, to be copied to the user's clipboard.

as of now, i know how to get the widget id.

   private fun getPendingSelfIntent(context: Context, id: Int): PendingIntent {
        val intent = Intent(context, this::class.java)
        intent.action = "ROOT_CLICK"
        intent.putExtra("widget id", id)
        return PendingIntent.getBroadcast(context, 0, intent, 0)
    }

is there any view by which i can get the state of that text view using that widget id of that widget.

val appWidgetManager = AppWidgetManager.getInstance(context)
        val thisAppWidgetComponentName = ComponentName(context!!.packageName, this::class.java.name)
        val appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidgetComponentName)
        for(widgetId in appWidgetIds) {
            if(widgetId == id) {
                val temp = appWidgetManager.getAppWidgetInfo(id)
                // what should i do here, so that i can get the view of this particular widget
            }
        }

Upvotes: 2

Views: 602

Answers (1)

Style-7
Style-7

Reputation: 1209

//1 Store a string value by call 

    Bundle bundle = appWidgetManager.getAppWidgetOptions( appWidgetId);
    bundle.putString( name, value);
    appWidgetManager.updateAppWidgetOptions(appWidgetId, bundle );

   ...
//2. Get a string value:

    Bundle bundle = appWidgetManager.getAppWidgetOptions( appWidgetId);
    String value = bundle.getString( name );

Upvotes: 2

Related Questions