7

I have several very similar layouts for my AppWidget, in fact differing only in the rotation of one of the TextView elements (I select the appropriate layout based on the screen orientation).

So rather than repeat the same stuff multiple times (I hate repetition) I thought I'd try and tidy things up by Re-using Layouts with <include/>.

The problem comes when I'm trying to reference particular elements of the layout by ID. Where the element is in the included layout xml, for non-AppWidget layouts I've used a method described here, where a two-tier approach is used...

In the "parent" layout:

<include layout="@layout/my_layout" android:id="@+id/textview_layout" />

In the code:

TextView tv = (TextView)findViewById(R.id.textview_layout).findViewById(R.id.textview);
tv.setText("test");

... where the ID of the TextView in the "child" layout is textview. So it's a two-stage approach where you first reference the View containing the TextView that you want, and then you reference the TextView off that.

But for AppWidgets, rather than dealing directly with the TextView, you have to do everything through a RemoteViews.

So instead of:

TextView textView = (TextView)findViewById(viewId);
textView.setText("text");

you would have to do:

remoteViews.setTextViewText(viewId, "text");
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);

Given that the interface into the RemoteViews is by way of passing in a single (direct) view ID, how would you reference an element in an included file?

Community
  • 1
  • 1
drmrbrewer
  • 11,491
  • 21
  • 85
  • 181

1 Answers1

-1

As you said your view only differs with a change in configuration. Taking that you are using Android Studio, in GUI Designing Tool There's a option called Go to next State, By using this you can generate layout-land file and configure it like you want it , and also you dont have change the id's as android will be deciding which layout it will be using , so you can access Your TextView's with same id. If you want access any View which is in the include tag. you could just use the id you have given to them. I hope this is helpful. ThankYou

Sumanth Jois
  • 3,146
  • 4
  • 27
  • 42
  • Thanks. I assume that this is so that the appropriate layout can be automatically selected by the OS, based on screen orientation? Unfortunately in my case it's not so straightforward because even if the screen is e.g. fixed in portrait orientation, I need to switch between different layouts based on the dimensions of the widget (if widget is tall and narrow the layout would be rotated... as a user option). – drmrbrewer Feb 18 '16 at 09:03
  • if thats the case you can set it programitically it's easy, check out some tutorial. and you can also use fragments which is better for your sutiation i think. ThankYou – Sumanth Jois Feb 18 '16 at 09:13