How to edit / change a view from a layout in RemoteView or create a RemoteView from a view?

I am creating a widget for an Android application (in Java, of course). I have classic RemoteViews created from a layout (using the layout id)

RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.view); 

and I need to edit or change the view (identify by id). In the classic view, it is easy using the findViewById function.

 View v = ... //inflate layout R.layout.view View my = v.findViewById(R.id.myViewId); processView(my); //filling view 

But in RemoteViews, it is not supported. It is possible to get the view using apply (), but after processView and reapply () I don't see the changes in the view.

 View v = rv.apply(context, null); View my = v.findViewById(R.id.myViewId); processView(my); //this work fine rv.reapply(context,my); 

The second, worst option is to get my necessary form of the RemoteViews view, process it, delete the old view, and add the processed new view using addView ().

 RemoteViews rv = ... View my = ... //apply, find and process //remove old view RemoteViews rvMy = ... //create RemoteViews from View rv.addView(rvMy) 

But I do not know how to create RemoteViews from a view (maybe?). Any ideas how to solve this problem?

+7
java android widget
source share
2 answers

Since editing / changing (sub) a view from removeview or creating a remote view from a view is possibly impossible (based on the basic information that I based), I solved my problem using the name of the required view (most of them are textview), getting view identifiers using its names, as well as reflection and process in the cycle. For naming, you can use bash, python, or something else.

Example:

 RemoteView rv = ... /* exemplary rv layout: +-----+-----+-----+-----+-----+-----+ |tv0x0|tv0x1|tv0x2|tv0x3|tv0x4|tv0x5| +-----+-----+-----+-----+-----+-----+ |tv1x0|tv1x1|tv1x2|tv1x3|tv1x4|tv1x5| +-----+-----+-----+-----+-----+-----+ */ String prefix = "tv"; for(int i=0; i<2;i++) { for(int j=0; j<6; j++) { // use reflection, searched in stackoverflow int id = getItemIdFromName(prefix+i+"x"+j); // working with concrete id using RemoteView set functions, eg rv.setTextViewText(id, String.ValueOf(i); } } 

This method allows you to process a large number of views and use the remote viewing function for them.

+1
source share

Try as follows:

  RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget); remoteViews.setTextViewText(R.id.widget_textview, text); <---- here you can set text // Tell the widget manager appWidgetManager.updateAppWidget(appWidgetId, remoteViews); 

Here is a helpful post to understand widget behavior:

http://www.vogella.com/articles/AndroidWidgets/article.html

+3
source share

All Articles