I'm developing a widget where the portrait and landscape layouts are saved in /layout and /layout-land respectively. each layout file is named the same, but has been modified for formatting changes based on their orientation differences.
When I start a widget in one of the orientations and keep it there, it works fine...but the moment i switch orientations, the whole layout goes haywire.
Now, after searching the web I have found out that the Android system loads the latest RemoteView from cache. Aka, if i started the widget it in landscape, the landscape layout gets cached and used when changing oritentation.
I've tried everything I can think of to stop this from happening, but to no avail.
Does anyone know if it is possible to clear the Android RemoteView cache and FORCE it load the corrected layout??
Thanks
EDIT:
I have found a solution, albeit a bit hacky...
I created a "wrapper" layout that only has a LinearLayout in it with an ID, and then created sub-layouts where the landscape versions have a _land at the end of the name.
in the onUpdate method and the service that updates the widget, i check the orientation of the screen, then programatically select either the _land or non-_land version, and add it to the wrapper layout.
I can't believe Google didn't make this easier to do, and its incredibly frustrating that I have to force such programatic behaviour out of something that should seem natural to do using XML configurations.
You might need to use layout-land and layout-port as the directories (as opposed to layout and layout-land) to make it work correctly.
Related
Is there a quick and easy way to see how my application will look on different screen sizes and shapes? Currently, I'm going into the AVD manager, editing my device definition, and then launching a fresh emulator. It takes maybe ten minutes for each one.
They layout is defined entirely from resource files. I don't actually need to execute the app. Is there a faster way to do this?
Edit: I should add: there are some custom widgets in the app.
While looking at a layout open the "Design" tab or open the "Preview" tab on the side. On the top of the window you will see a list where you can change the layout and orientation.
May not solve your entire issue, but it's a good to know.
If you want to have a preview of a static page (not rendered at runtime, like recyclerview), you could use android studio to do that.
I have a complex project with many layouts (approx 40) and to reduce duplication, I am using a lot of includes.
I have an activity layout in layout-xlarge-land which includes another layout (layout B) which itself is just a set of more includes in a LinearLayout.
My emulator is set to XLARGE and landscape and sure enough, it picks up the activity layout. I've hard coded the activity title in the layout so I can confirm which one is being used.
The problem is that if I put layout B in layout-xlarge-land, the ADT will not preview my layout and gives a "cannot render" error. if I move layout B to res/layout (i.e. with no qualifiers) it works.
My understand is that Android will look for a layout in the qualified folder first then, if not found, use the one in the unqualified folder.
It's a problem because layout B should be different for the various resolutions and orientations.
Am I misunderstanding something or is this a quirk of the ADT/Android? If a quirk, any workarounds?
res
layout
layout-xlarge-land
activity_layout.xml <---- correctly loaded
layoutb.xml <---- "cannot render"
res
layout
layoutb.xml <---- renders OK
layout-xlarge-land
activity_layout.xml <---- correctly loaded
[EDIT] I'm using IDEA but don't see any relevance to the IDE.
[EDIT]
From the IDEA log
s.android.uipreview.RenderUtil - InflateException: You must specifiy a valid layout reference. The layout ID #layout/data_panel_all_views is not valid.
This confirms that ADT is looking in res/layout.
Taking a look at How Android Finds the Best-matching Resource, it tells that you can provide multiple qualifiers and how the best layout is chosen, so in the case you are sorting qualifiers well (which looks like you're doing, as the activity_layout is correctly loaded), try applying less qualifiers and seeing if those includes are working. Otherwise, you could think about the dirtiest solution: creating different layoutb's for each resolution and storing them in the same folder. That would mean each activity_layout would load it's correct file.
Good luck!
This was weird. One of the things I did was to restart IDEA which didn't resolve anything.
I've just rebooted my PC to install some updates and since starting IDEA, it's working as expected.
I've seen some issues with layouts which were fixed by restarting Eclipse. The common element is ADT so I suspect some flakiness in there.
Working with Android for the first time, I've blocked out a layout using the relative layout and laid down some buttons and text widgets how I like them. However when I go back to rename the IDs the layout goes all crazy moving elements around and in general destroying the hours of work I spent laying them out.
Does anyone know how I can rename the widgets without Android destroying the positioning for widgets in the Relative Layout? Is this some "feature" of Android? I can't imagine why it would be hard for the UI builder to handle simple renaming of a widget ID without destroying the positioning information.. Do I have to use an external text editor and modify the XML files directly? Ughh I hope not.. I'm using Eclipse IDE.
You can use find and then replace all to change the names every place that they appear. Shouldn't take anywhere near an hour if you're dealing with a small layout.
In general the graphical UI creator that is currently included with the Android SDK is not so great for creating anything but very simple layouts. In my experiences (which were a long time ago, it may have gotten better since) it was terrible with RelativeLayouts.
If you have not modified your xml directly then it is time that you jump in and start learning to do it that way. You'll find that you have a much greater level of control over your layout, and once you get the basics figured out you'll probably be able to create quicker using raw xml then with the graphical tool anyway. I do wish that there were a nice GUI creator for android out there, the best one that I've ever come across is Droid Draw which I found to be better than the one included with the SDK, but still not as good as I was hoping.
To modify the xml directly you don't need any additional text editors, you do it inside eclipse. Open up your layout file and at the bottom click on the tab that says "Source" when you want to switch back to graphical (good to see the changes that you make to the xml graphically) just click back to the tab that says "Design"
I am trying to simply alter the setlayout when I rotate my device so that I can have a layout of Views for a particular activity that is suited to the current orientation of the Android device but I am confused about the best way to achieve this.
I have referred to the following android doc:
Handling Runtime Changes
I do not need to save any data from my Activity so don't think I really need to use the onRetainNonConfigurationInstance() method. I tried handling the orientation change myself through the onConfigurationChanged() method, where I find the current orientation then set the layout as required but this results in views that no longer work. Is there something else I need to do in onConfigurationchanged()?
Thanks
To get a different view for landscape as opposed to portrait, you would place your layout XML file in both of the following resource folders:
/res/layout - Portrait
/res/layout-land - Landscape
This is, of course, if you have the same views within both, otherwise you may get some NullPointerExceptions.
You can create a new directory under res called "res\layout-land", create an .xml layout file in both "res\layout" and "res\layout-land" that have the same name. For example: "myLayout.xml". Android will automatically use the layout from the -land directory when in landsacpe orientation and the other when in portrait.
in your projects res folder you should have a layout folder. Create a new folder in the res and call it layout-land. Now create your second set of layout.xml files that are specific to landscape oriented devices. Save them in this layout-land folder. The system will handle the rest for you.
check out this page and scroll down to "Providing Alernative Resources" for more detail about different qualifiers you can use on your res folders.
Edit: What device are you using? I created a quick test project that is nothing but hello world but displays different text from a layout stored in res/layout-land folder.
I tried it once with and once without configChanges="orientation" in the manifest. When I run the app and switch orientations the layouts behave as expected. The layout from layout-land is displayed when device is landscape and layout from plain layout folder is shown when device is in portrait.
The device I tested on is Sidekick 4g. Download the test project and report back how it works on your device if you like.
Hi
M new to android.I faced a problem i.e in landscape mode i need a layout view which doesnot have some feilds which are present in the portrait mode.I have created a layout for landscape view in such a way.In the activity i have given the conditions like if portrait display some feilds some hide.its working properly actually...first when i go from portrait to landscape it is giving what i want...but when i go to portrait mode its stopping the application.please help me.Thanks in advance
You can make two different layouts and put them in layout-port and layout-land respectively.
And make sure that you have mentioned android:configuration = "orientation" in your manifest for that activity. Try it if i have understood you correctly.
I think that the advice by Kantesh may be backwards. As explained in the docs, if you include android:configChanges="orientation" in the manifest, then the correct resource (from layout-port or layout-land) won't be automatically loaded. Instead, leave out mention of orientation from the manifest. Then, you do not need to worry about onConfigurationChanged() (don't override it). The system will automatically shut down the activity and then restart it, binding the appropriate version of configuration-dependent resources. Handling configuration changes yourself is (again, according to the docs) only a last resort to deal with performance issues that cannot be handled in other ways.