Sunday, June 10, 2012

[android-developers] Changing LinearLayout width in an appwidget

(This was posted in Stack Overflow, with 0 answers so far. Perhaps I have more luck here)

In an appwidget I have a somewhat complex layout with nested LinearLayouts that serve as vertical columns. I want to add a setting that would allow the user to choose a fixed or flexible widht for the columns (now it's fixed at 38dip).

In XML terms, users could choose:

<item name="android:layout_width">38dip</item>

or

<item name="android:layout_width">wrap_content</item>

The following code will work if the LinearLayout belonged to an Activity, but in an appwidget crashes because of the invalid cast ((Activity) context):


private
static void customizeWidget(Context context) {
   
int identifier;
   
int styleWidth = 38;
   
int newWidth = ph.autoAdjustableColumns() ? ViewGroup.LayoutParams.WRAP_CONTENT : styleWidth;
   
for (int i = 1; i <= 7; i++) {
        identifier
= context.getResources().getIdentifier("day" + i + "_ll", "id", context.getPackageName());
       
LinearLayout llDatesCol = (LinearLayout) ((Activity) context).findViewById(identifier);
       
ViewGroup.LayoutParams params = llDatesCol.getLayoutParams();
       
params.width = newWidth;
        llDatesCol
.setLayoutParams(new LinearLayout.LayoutParams(params));
   
}
}

AFAIK it crashes because of limitations of the RemoteViews API, there is no way to set parameters for layouts when they are accesed via RemoteViews.

I know I could load a different XML layout in each case, but since the layouts would differ only in this line, I feel like it's a waste. Any other solutions?

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment