Hi does anyone know how to implement a layout change in the application. For example, when a user sees a text box prompt and turns the phone sideways and views the application as a landscape view. I have no idea how to go forward with this?
May I add that I know I have to create a layout-land xml. I'm ok with that bit, it's just I'm not sure how to make the phone automatically detect that it is oriented sideways and needs to implement the landscape view.
Many thanks.
If the phone supports orientation changes and you've turned it on in Settings (Display > Auto-rotate screen) everything should work automatically. The Activity will be restarted and the correct layout file used.
See this post here för details: Android: alternate layout xml for landscape mode
Related
I am new in this forum and checked different threads here but did not found something similar to what I want to do...
Some Apps like MP3-Players or e.g. Spotify-App add some buttons when the lock screen is active (android nativ lock screen with PIN). In this case the displayed Date will dissapear to give more place to these "new" controls.
I have tried this without succes with a simple widget app by including android:widgetCategory="keyguard|home_screen" in the appwidget-provider xml information. The app widget is correctly operating on home screen but I see no widget when my screen locks.
So I think this uses other mechanism, may be an activity that hides the date and display controls on the lock screen.
Did anyone try something like this and can give me some tipp?
Thank you in advance!
Notes:
I dont want to implement a lock screen app, I would like to display a button and text on my lock screen like some common and existent apps do.
Used Android Version >= 4.2.2
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Android - disable landscape mode?
I can't save position of view when orientation changes.
I think it's because with orientation it also changes and I want that view stayed on the same position. For example of it was in the upper left corner of screen it stayed there and it doesn't matter what orientation of the device.
I'm using some kind of Absolute layout. Because I need to manage few screens at the same time.
So, i have x/y coordinate system. I think it's the main problem, but I can't solve it :(
I've attached images that you could see my problem.
As you can see first screen is portrait mode and View is located in upper-left corner, but when I changed to landscape mode position of view is also changed to upper-right corner.
And I need that position of view stayed always in left-upper corner.
http://imgur.com/qo19E&u2K0hl
http://imgur.com/qo19El&u2K0h
Sorry to bad screen. I'm still developing of an application and can't provide better screens :)
Ps: If you need some code, just say what part of code do you need.
Duplicate of Android - disable landscape mode?
You can force the activity to run in a particular orientation.
Inside the AndroidManifest.xml use the attribute "android:screenOrientation"
See here for an example:
http://developer.android.com/guide/topics/manifest/activity-element.html
It would be nice if you could provide some code or screenshots.
Anyway, when the orientation changes, your activity is destroyed and recreated.
If you have a well designed xml layout file (using linearlayout, relativelayout and properties like "wrap_content", "fill_parent"...) it will reload it in the onCreate() method, so all the resizing should be done automatically.
If your views can be moved by the user or are loaded from runtime values, you can save it. Look at this post for exemple : Override Orientation Change But NOT Restart The Activity AND Pass State Data
I'm currently working on an Android application that have different behaviors according to the screen orientation.
I'm using getWindowManager().getDefaultDisplay().getRotation() to get the screen orientation in the onCreate() method and manage from there.
Basically, my problem is that I need a control over the orientation. In landscape mode, when the user decides it, the application must switch to portrait mode, and stays like that, but only until the use goes to portrait mode, and then again to landscape.
I'm using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) to change the orientation when in Landscape. The problem is, using this, I can not go back to landscape.
If I use setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) in onCreate(), the problem is that is switches back to landscape mode after a few seconds (since the screen is still in landscape).
Is there any other way to manage this? Basically, is there a way to catch the screen orientation change with a listner (or anywhere else than in onCreate, since it's not called when the views is locked in portrait mode)? I can't find anything about that...
Any idea is welcome!
Thanks!
Why not try creating two layouts, one in layout-land and one in layout-port. These will automatically be applied when the device orientation changes.
Inside each of these, create two distinct child layouts one for a portrait view, and one for a landscape view and change the visibility of these to reflect the user's preference.
So, you don't need to handle the rotation change at all, let Android manage that for you, and you just manage the user override.
I'm currently creating a side-scroller style game for my final year project for my degree.
I'm just wondering how you make the menu designed for when the phone is in horizontal orientation display, even when the phone is held in it's vertical orientation?
i.e. I want the user to know that the game has to be played with the phone in it's horizontal orientation and want the menu's to only display in the horizontal orientation.
Many thanks in advance.
Add the following attribute to your activity in your AndroidManifest.xml:
android:screenOrientation="landscape"
Well, I didn't get your question right,
but if you are defining your menu via xml
create a new folder in your res folder named
for layout changes:
layout_land
for horizontal specific menu:
xml-land
for horizontal specific drawables:
drawable-land
and define your menu,layout and drawable changes there.
Further you can tell android framework that you want to handle rotation changes:
by adding to your manifest(AndroidManifest.xml) activity child
android:configChanges="keyboardHidden|orientation"
android:screenOrientation="landscape"
Comment exactly what you want.
Cheers!
Just complementing on other's answers
There are some different types of landscape and I think the best one is:
android:screenOrientation="userLandscape"
because it lets the user switch to landscape or reverse landscape using the device's sensor, or the user can block the swithing if he decides to.
I found that removing some basic controls of the user is quite annoying and should only be used if really necessary.
If you want to checkout other possible orientations just go here
I have code in an appwidget that I want to run when the phone's orientation changes on the home screen, ie like when the keyboard flips out. I have an image that I want to change in an imageview in my appwidget. I can't use different layouts linked to the orientation (ie "layout" and "layout-land") because I don't know the name of the image file until runtime, it is created at runtime. Is there anyway to trigger code to run only if the home screen is shown, my appwidget is active and the orientation just changed?
I could listen for a configuration_change broadcast but that will run everytime the phone switches to landscape or portrait and I only want it to happen when the homescreen is shown. I cannot think of any good way in android to do this. Thanks
Ryan
There isn't a good way of doing this for images generated at runtime.
One approach would be to build both versions of the image and have two ImageViews that are visible depending on the layout/layout-land being used. (So the RemoteViews would update both ImageViews, but only the correct one would be visible.)
Jeff, what is the reason for this limitation ? I would love to be able to use images generated at runtime, or construct the RemoteView on the runtime as well, rather than use the precompiled XML - this is so inflexible.
There is more info at http://groups.google.com/group/android-developers/browse_thread/thread/0eb7c016be05e0b9/d31b312e2fa8530d
Basically update the widget from onConfigurationChanged() in an Activity and add android:configChanges="orientation" for that Activity in your manifest. Bad thing is, this doesn't cover all use cases.