Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How edit/change View from layout in RemoteView or create RemoteView from View?

I create widget for Android application (in Java, of course). I have classic RemoteViews created from layout (using layout id)

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

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

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

But it is not supported in RemoteViews. Is possible get view using apply(), but after processView and reapply() I not see changes in view.

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

Second, worse option, is get my required view form RemoteViews, process it, delete old view and add 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 don't know how create RemoteViews from View (it is possible?). Any ideas how solve this problem?

like image 824
honzakuzel1989 Avatar asked Nov 09 '13 23:11

honzakuzel1989


1 Answers

Try this way :

        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 useful post to understand widget behaviour :

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

like image 129
Adrien Cerdan Avatar answered Oct 19 '22 19:10

Adrien Cerdan