Dynamically adding custom view to RemoteViews

Posted by Naidu on Stack Overflow See other posts from Stack Overflow or by Naidu
Published on 2011-03-25T10:40:21Z Indexed on 2012/09/24 3:38 UTC
Read the original article Hit count: 138

Filed under:

Could any help me to do this?My code is like:

 public CustomClass extends View {

//uses ondraw() to do something

}

For displaying my custom view on HomeScreen i created a class to extend 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 viewgroup to RemoteView reference is not working... above main.xml contains only LinearLayout.I want to add custom view object to it.But after running this not showing anything on screen.. please help me to do this.Thanks in Advance.

© Stack Overflow or respective owner

Related posts about android-widget