Views still rotate when OnConfigurationChanged is overridden - android

For my current activity I currently have the following added to the manifest:
android:configChanges="orientation|keyboardHidden|screenSize"
and in he actual activity I have overridden the OnConfigurationChanged method as follows:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
The views in the activity still rotate, but I want to specify that some of them should not rotate.
Does anyone know how to avoid the inevitable rotation of all the UI elements within an activity?
The caveat here is that I still need the OS to open the keyboard in landscape mode when the keyboard becomes activated - I just don't want the other views rotating. I believe this means that forcing the activity to be in portrait mode (which I know how to do) will not work unless someone knows how to specifically open the softkeyboard in landscape mode while the activity is in portrait mode.
Thank you in advance.

Related

How to prevent restarting from orientation change?

Here is the structure of my application.
MainActivity.java calls FragmentActivity.java,
FragmentActivity.java calls GameView.java
GameView.java calls Thread.java.
Basically all the gaming logic will be handled by GameView and its thread.
I don't know how to prevent from restarting the game when there is a orientation change.
If i paused the thread and resume it, the app crashes and also i can not use onSaveInstanceState method in Gameview.java
Any help?
It is completely possible through the Android manifest.
Just add in the activity declaration, in which you want to disable the restarting the following attribute:
android:configChanges="orientation|screenSize"
Then you can overwrite the onConfigurationChanged() in your activity and you get the callback which event just happened. In your case the orientation change. And with this approach the activity doesn't restart, when your orientation changes.
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
You are looking for onResume() and on pause() ... You store values into a bundle in pause and instantiate those values on the resume method and show a dialog as this happens to prevent the user from noticing the obvious.
You can have some control over what happens on orientation change, by defining an Application class in your manifest file, and overriding public method onConfigurationChanged().
Base class for those who need to maintain global application state.
You can provide your own implementation by specifying its name in your
AndroidManifest.xml's tag, which will cause that class
to be instantiated for you when the process for your
application/package is created.
You just need to check for newConfig == Configuration.ORIENTATION_LANDSCAPE and such. At this point you might want to reload resources to get things working.

Rotation restarts activity

I have a problem with my android app. My activities restart when I rotate my phone (onCreate is called). I have googlet and have tried
android:configChanges="keyboardHidden|orientation"> in my manifes, but with no luck. Can someone please explain to me how I can get this to work
EDIT: I find out that i nedd to ad these codes
manifest
<activity
android:name="?"
android:label="#string/?"
android:theme="#style/?"
android:configChanges="orientation|screenSize">
MainActivity
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
In your Activity, you can override the method onSaveInstanceState. You can save the information you need into a Bundle. That bundle will be passed in to the onCreate method after the orientation has changed. If you would rather just specify one orientation to be used, within your manifest file, either under the application tab or activity tag, place:
android:screenOrientation="portrait"
You can try android:configChanges="keyboard|orientation|screenSize">. However, be aware that restarting the Activity on configuration change is normal behavior. Unless you have very specific needs, you should not be using configChanges.

onConfigurationChanged() calling twice when change orientation Landscape to Portrait

i am getting strange issue in my app. The main issue that i have asked here. issue is android:configChanges="keyboardHidden|orientation" is not working in my code.
so i have found the solution to manage it by #Override onConfigurationChanged() method in my code to manage orientation. but yet the issue is not solve properly.
Currently issue is that onConfigurationChanged() is calling twice when we change orientation
landscape to portrait.
If we change phone portrait to landscape its changing and working but now when user move the phone landscape to portrait then onConfigurationChanged() will call and return same orientation state & in second call it will return portrait.
Code :
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
Log.e("On Config Change", "LANDSCAPE");
Toast.makeText(getApplicationContext(), "L", Toast.LENGTH_LONG)
.show();
} else
{
Log.e("On Config Change", "PORTRAIT");
Toast.makeText(getApplicationContext(), "P", Toast.LENGTH_LONG)
.show();
}
}
Log
first mode its port mode , so change in land mode
02-28 12:10:06.274: E/On Config Change(540): LANDSCAPE
02-28 12:10:14.154: E/On Config Change(540): LANDSCAPE
// here after changed the land mode try to chage in port mode then its calling two times as you can see as per the log
02-28 12:10:14.593: E/On Config Change(540): PORTRAIT
02-28 12:11:39.524: E/On Config Change(540): LANDSCAPE
One more query with same question >>
It will kill the current activity when we change the orientation (at time of calling onConfigurationChanged). so i have two layouts in different folder as per my previous question.So when i change the screen activity will remove all data.so how can i save that data to show user when user change the phone orientation in any case.
I am not sure how onConfigurationChanged() gets called twice, but to avoid buggy behavior in your app you can void the execution of onConfigurationChanged() if the orientation received now is same as the previous orientation.
Worked for me, hope it works for you too :)

How to handle orientation change easily

I'm working on a android application that adds every view programmaticaly.
When the user turns the screen, I just want to values that are filled in to be displayed again.
Is there an easy way to let Android do this automaticaly?
My application is completely dynamic so it doens't have a predetermined layout, which makes it a lot harder.
So, how can I easily save the state of my screen?
Everytime orientation change, android create new view and destroy the old one. You can saved your data when orientation change and re-initialize when the new view is created
Use onConfigurationChanged method of activity to detect Orientation Change
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
Don't forget to edit the appropriate element in your AndroidManifest.XML like this to include the android:configChanges
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name">
For an expanded explanation of Hein's correct answer see my previous post:
https://stackoverflow.com/a/57239493/5696328

It is possible to known when the screen orientation is changed but with portrait mode forced on manifest?

Welcome all
I have a special need for my app. I must force the app to work only in portrait mode, but i need to know when the user has moved the phone to landscape mode. Why? because i am displaying a opengl view with a texture image, and when the user changues the phone position to landscape mode i must rotate the polygon without reseting the activity. Then i must force portrait mode on manifest, because i dont want that my onCreate method gets called again.
Please, can someone tell me how to achieve this?
I know how to rotate the image, i only need to know when the user has moved the phone to landscape position, but with portrait forced on manifest.
I tryed adding android:screenOrientation="portrait" in the manifest and also adding android:configChanges="keyboardHidden|orientation" to the declaration in the manifest; and then overriding onConfigurationChanged() in my activity. But this doens't works, because portrait mode is forzed, then onConfigurationChanged method is never called......
Thanks
Why not use the SensorManager to monitor when the phone has rotated 90 degrees. Actually this may be helpful also.
use this attribute in manifest android:configChanges="orientation" this stops recreating activity then override the onConfigurationChange() method. Give the same layout both in portrait and landscape mode. when orientation change occurs, that method wil be called then change the image as u like
Do Not set the screen orientation in the manifest (android:screenOrientation="portrait"). Add the android:configChanges="keyboardHidden|orientation", and override your onConfigurationChanged(). This way your onCreate will not be called twice.
Here a example of your onConfigurationChanged:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig); // tem que ter
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
} else
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
}
}

Categories

Resources