6

Within my widget, i'm using the following to dynamically add items (R.layout.widget_item) to a LinearLayout defined within my main widget layout:

//--  Main widget layout
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_design);

//--  Add to main layout
RemoteViews newView = new RemoteViews(context.getPackageName(), R.layout.widget_item);
views.addView(R.id.view_container, newView);

My question is, how can I dynamically set the "android:layout_weight" attribute on the newView?

92Jacko
  • 523
  • 2
  • 10
  • 18
  • see [this][1] link plz. [1]: http://stackoverflow.com/questions/3224193/set-the-layout-weight-of-a-textview-programmatically – Android Killer Sep 10 '11 at 18:30

3 Answers3

1

See: http://developer.android.com/reference/android/widget/RemoteViews.html

newView in your example is an instance of class RemoteViews and I don't think you can easily set anything above and beyond what RemoteViews provides.

newView.setLayoutParams(layoutParams);

The above will not work because it's not part of RemoteViews.

As far as I can see... there may be some round about way of setting and finding dimensions and other layoutParams of RemoteViews, but I haven't found it yet.

aseq
  • 451
  • 1
  • 6
  • 23
  • 1
    Good point, unfortunately there does not seem to be any sort of workaround. LayoutParams isn't a Java type like an Integer to String so you cannot use the set* methods that involve reflection on it either. – Tom Jun 30 '12 at 18:43
-1

first you get the view params like this

layoutParams = newView.getParams();

then edit tham //layoutParams .setThis/setThat all kinds of manipulations you want to do

and then set the new params back to the view

newView.setLayoutParams(layoutParams);
Lukap
  • 31,523
  • 64
  • 157
  • 244
  • Thanks, but i cannot seem to get this to work, the following line displays an error: `code`LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) newView.getParams();`code` any ideas? – 92Jacko Sep 10 '11 at 18:26
  • I guess newView it is not in any LinearLayout – Lukap Sep 10 '11 at 18:30
-1

layout_weight is part of the LinearLayout.LayoutParams that you apply to a the LinearLayout, not the LinearLayout itself. There's a constructor that takes a third param representing the layout_weight.

http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#weight

Rich
  • 36,270
  • 31
  • 115
  • 154