Change orientation without setRequestedOrientation - java

When I click a button in my App, the screen orientation will automatically go to landscape :
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
The screen get to landscape successfully but it caused my onConfigurationChanged not working anymore.
I tried to use: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); but it makes no difference.
What should I do, to make the screen go to landscape, when I click the button, without turning off the onConfigurationChanged?Or do you have any other ways to accomplish this?

If you are using API13 or higher as your target,
in your manifest you need to add ScreenSize to your configChanges:
android:configChanges="orientation|screenSize|keyboardHidden"
That should do the trick!

Related

Activity orientation change while in the background

The first activity(locked portrait orientation) has 2 buttons that both open the second activity but A button opens it in portrait and B button opens it in landscape orientation.
The problem is that when B button opens the second activity (which is in dialog configuration and the first activity is shown in the background) the first activity changes orientation with the second one, thus restarting.
The question is, A) Can i prevent first activity from changing orientation and B) Can i disable orientation change animation so that the screen doesn't look like turning from portrait to landscape but just becoming landscape instead?
P.S For the A) question i know how to use saved state but i want to avoid it.
Use Below Code in Manifest:
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="portrait"
Edited:
Sorry there is no way to control the rotation animation. This is done way outside of your app, deep in the window manager where it takes a screenshot of the current screen, resizes and rebuilds the UI behind it, and then runs a built-in animation to transition from the original screenshot to the new rebuilt UI. There is no way to modify this behavior when the screen rotation changes

android Change orientation removing button

I've set android:configChanges="orientation|screenSize" on an activity to stop it restarting the activity everytime the orientation changes. Now this works fine but I think it's stopping the correct layouts from being used.
E.G. I have different layout folders for different orientations and sizes of screen. So if I start the activity in portrait, when I change the orientation to landscape, it's not using my landscape layout.
Also if I start the activity in landscape, when I change the orientation to portrait, it's not using my portrait layout.
Basically what I want the app to do is not start the activity again once the orientation changes, but use the correct layout when the orientation is changed!
I was thinking I could use the onConfigurationChanged method to explicitly change the layout in code?
Thanks for any input
when you use android:configChanges="orientation|screenSize" this tells to Android that you will maintain these changes by yourself - this means you have to change your layout (setContentView) and initialize it manually (set values of controls - EditTexts, Spinners etc.)
So What I've ended up doing is keeping android:configChanges="orientation|screenSize" in the manifest file. Doing it this way stops me being able to use my correct layout folders for portrait and landscape.
To overcome this issue, I override the onConfigurationChange method to set the correct information I needed for the activity to run as expected once the orientation changes. below is the code I've used:
#Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
setContentView(R.layout.hearing_test);
//Any other information needed for the activity to work correctly
}
Thanks for the help and guidance #mihail, helped me get to the bottom of the issue.
That is exactly why the Activity is destroyed and recreated: to apply new resources.
Do not use android:configChanges="orientation" if you have different layouts for portrait and landscape mode.

How can an app temporarily display a full screen animation in an Android activity?

Similar questions have been asked before, but I couldn't find an answer to how to make one element full screen. I would like to temporarily display a full-screen animation in my Android app without moving to a new Activity. The app has an ImageView for the animation and I show the animation with:
image.setBackground(animation);
and make it "go away" with:
image.setBackground(null);
For the ImageView, both android:layout_width and android:layout_height are set to "match_parent". It works, but of course the animation doesn't include the title bar at the top or the icons at the bottom. I even tried setting the layout_marginTop and layout_marginBottom to negative values, but I know that can't be the best solution for every device or orientation which may run this app.
How can I change the size of the ImageView to full screen so I can show it as needed, or is there a better way to do what I want?
Duplicate of Fullscreen Activity in Android?.
Just set the right flags and dismiss the activity when it's finished its animation.
Your ImageView is stuck inside the bounds of your Activity. And I don't think there is a way for you to change your Activity to be full screen sometimes but not others.
If you want your Activity to be fullscreen always you can set the theme in the manifest like this:
<activity
android:name="com.your.package.name.YourActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
</activity>
Combine both answers. Create a new activity to play your animation and set the theme of your animation activity to fullscreen.

How to change orientation using a menu button?

I would like to handle the orientation change of my app with a menu button, so it doesn't change views when the device is turned, but when the user presses a menu item. Can you tell me the best way to do this? Everything that comes to my mind seems to be stupid.
Thanks a lot!
You can call setRequestedOrientation so specify an orientation.
In your xml-Manifest file you can specify a android:screenOrientation="portrait" to not change the orientation automatically.
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Android change in view - Button state gets reset

I have a button which is basically used for start/stop. So initially the text of the button is set to start. I attached a OnClickListener to it. So whenever it gets clicked i change its text. So if it was start it become stop and vice-versa.
The problem comes when i change my phone view from portrait to landscape or vice-versa the button text gets reset.
So for example I clicked the start button---it changed to stop. Now if I tilt my phone to change the view the button text gets set to start again.
Am I using the button in a wrong way?
You should save your button state. When screen orientation changes, onCreate is called and all your app variables are re-intitialized. Read more here http://developer.android.com/reference/android/app/Activity.html
No, you are using the button in the right way.
The thing what you are seeing is "configuration change". When you are tilting your device, Android recreating your activity and recreating all it's views (so, they getting default captions as them described in XML).
You need to do the
disable configuration changes for your Activity. To do so, add the following to your manifest's activity tag: android:configChanges="orientation|keyboardHidden". It is not suitable, if you have different layouts for landscape and portraint orientations, when you need to...
handle the configuration changes by overriding onSaveInsatnceState method of your Activity, save a state there and then use it in onCreate method.
See this article for further explanation

Categories

Resources