Dynamically adding a custom view to RemoteViews

Can someone help me do this? My code is similar:

public CustomClass extends View { //uses ondraw() to do something } 

To display my custom view on the main screen, I created a class to extend the broadcast receiver:

 public class customAppWidgetProvider extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) { RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main); //Here I want to create my custom view class object and I want to add this view to linear layout in main.xml CustomClass object = new CustomClass(context) ; LinearLayout layout = new LinearLayout(context) ; layout.setLayoutParameters(new LayoutParameters(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); layout.addView(object); views.addview(R.id.linearlayout, (ViewGroup) layout) ; views.setOnClickPendingIntent(R.id.analog_appwidget, PendingIntent.getActivity(context, 0, new Intent(context, AlarmClock.class), PendingIntent.FLAG_CANCEL_CURRENT)); int[] appWidgetIds = intent.getIntArrayExtra( AppWidgetManager.EXTRA_APPWIDGET_IDS); AppWidgetManager gm = AppWidgetManager.getInstance(context); gm.updateAppWidget(appWidgetIds, views); } } } 

But adding a ViewGroup to the ViewGroup link does not work ... The main.xml layout above contains only LinearLayout. I want to add a custom viewer to it. But after starting, nothing is displayed on the screen ...

Please help me do this. Thanks at Advance.

+4
source share
1 answer

Unable to add custom View to application widget. See the β€œCreating a Layout for Application Widgets” section in the Application Widgets guide for View types.

Android 3.0 adds support for some views to display collections. For more information, see "Using the application widget with collections."

Otherwise, to dynamically add the allowed View to the application widget after inflating RemoteViews and getting a link to it, you can use its addView(View) method or addView(View) method for any of the View objects already in RemoteViews .

+7
source

Source: https://habr.com/ru/post/1345215/


All Articles