Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to setLayoutParams on dynamically added elements on Widget
    primarykey
    data
    text
    <p>currently, i'm adding elements dynamically to my widget using the following:</p> <pre><code>RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_design); RemoteViews newView = new RemoteViews(context.getPackageName(), R.layout.widget_item); views.addView(R.id.view_container, newView); </code></pre> <p>but how can I dynamically add margins, weights, and other properties to the added elements?</p> <p>currently i have tried (using code I have found from different sites):</p> <pre><code>LinearLayout item = (LinearLayout) ((Activity) context).findViewById(R.id.widgetItem); MarginLayoutParams marginParams = new MarginLayoutParams(item.getLayoutParams()); marginParams.setMargins(10, 50, 4, 5); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(marginParams); item.setLayoutParams(layoutParams); </code></pre> <p>but this force-closes</p> <p>LogCat:</p> <blockquote> <p>09-10 22:01:55.283: ERROR/AndroidRuntime(30060): FATAL EXCEPTION: main 09-10 22:01:55.283: ERROR/AndroidRuntime(30060): java.lang.RuntimeException: Unable to start receiver com.example.test.HomeWidget: java.lang.ClassCastException: android.app.ReceiverRestrictedContext 09-10 22:01:55.283: ERROR/AndroidRuntime(30060): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2821) 09-10 22:01:55.283: ERROR/AndroidRuntime(30060): at android.app.ActivityThread.access$3200(ActivityThread.java:125) 09-10 22:01:55.283: ERROR/AndroidRuntime(30060): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083) 09-10 22:01:55.283: ERROR/AndroidRuntime(30060): at android.os.Handler.dispatchMessage(Handler.java:99) 09-10 22:01:55.283: ERROR/AndroidRuntime(30060): at android.os.Looper.loop(Looper.java:123) 09-10 22:01:55.283: ERROR/AndroidRuntime(30060): at android.app.ActivityThread.main(ActivityThread.java:4627) 09-10 22:01:55.283: ERROR/AndroidRuntime(30060): at java.lang.reflect.Method.invokeNative(Native Method) 09-10 22:01:55.283: ERROR/AndroidRuntime(30060): at java.lang.reflect.Method.invoke(Method.java:521) 09-10 22:01:55.283: ERROR/AndroidRuntime(30060): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 09-10 22:01:55.283: ERROR/AndroidRuntime(30060): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 09-10 22:01:55.283: ERROR/AndroidRuntime(30060): at dalvik.system.NativeStart.main(Native Method) 09-10 22:01:55.283: ERROR/AndroidRuntime(30060): Caused by: java.lang.ClassCastException: android.app.ReceiverRestrictedContext 09-10 22:01:55.283: ERROR/AndroidRuntime(30060): at com.example.test.HomeWidget.onUpdate(HomeWidget.java:37) 09-10 22:01:55.283: ERROR/AndroidRuntime(30060): at android.appwidget.AppWidgetProvider.onReceive(AppWidgetProvider.java:61) 09-10 22:01:55.283: ERROR/AndroidRuntime(30060): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810) 09-10 22:01:55.283: ERROR/AndroidRuntime(30060): ... 10 more</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload