I want to change my device screen orientation programmaticaly. For this I can use:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
But this would lock my screen for entire activity lifecycle. I just want to change the screen orientation so that I can receive onConfigurationChangedcallbacks.
This is how my manifest looks like for an Activity:
<activity
android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"/>
So, my question here is if there's a way to change screen orientation for once and clear the flags from setRequestedOrientation
Try to use onResume() .
I think this might help you.
Want to create a view on Run time. In onResume() create the view.
Related
I have a problem. I have an App, which has a fix orientation (portrait) set in the manifest.xml.
android:screenOrientation="portrait"
But I do want to change the orientation of an Dialog, that I create programmatically in one of my fragments. Problem is, that the super method onConfigurationChanged(Configuration newConfig)is never been called, because of my manifest declaration.
Is it possible to detect that the phone has been turned and change the orientation of the dialog (Without destroying / recreating everything like normally)?
EDIT:
"Activity and Fragments should stay in portrait mode, Dialog should detect changes and orient"
Hope this question is understandable ;)
Thanks!
I think that a viable solution would be to remove the android:screenOrientation tag and then override the onConfigurationChanged() method to do what you need.
In my application, most of the activities has fixed layout - portrait.
So I've mentioned in manifest:
<activity
android:name="com.example.activity5"
android:screenOrientation="portrait" />
But in one or two activities I've to show landscape layout also, in such a way that by default the activity opens in portrait mode. But if user tilts the phone to left/right it changes to landscape. (also if user rotates the phone to upside down, activity should not go to portrait mode).
That is, basically, I want orientation change in 3-way. Default(Potrait) & Left-Right(Landscape).
So, what changes do I need to do in my code & xmls?
Should I choose "sensorPortrait" OR "sensorLandscape"
Should I use android:configChanges="orientation"
I tried few steps, but they are throwing null pointer exception.
I dont know what I'm missing.
P.S. Both orientation have different layouts.
Thank You
I think you want to do this:
1) Just remove android:screenOrientation="portrait", and
2) In your code, in onCreate(), add this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
--(UNRELATED) SIDE NOTE--
To force landscape use this:
<activity android:name="com.example.activity5"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden" />
Write to Main.class(OnCreate):
setContentView(R.layout.activity_main);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
I am running something in onCreate() upon initialisation.
If a user rotates the screen, it recalls onCreate().
I want to disable screen rotation and let onCreate() run ONLY upon the initial initialisation.
Is it enough to add android:screenOrientation="portrait" to the manifest or will onCreate() still be run?
Thanks!
If you put android:screenOrientation="portrait" in your Manifest the Phone doesnt handle orientation changes and onCreate() doesn't get called again.
So: YES it is enough!
You can easily check it if you set a Debug-Marker in your onCreate() and then rotate your phone!
If you hold your Activity in Portrait or in Landscape, the rotation will no longer happens. So the onCreate() will run to the end though you will try to rotate your device
Add in your manifest:
android:configChanges="orientation"
android:screenOrientation="portrait"
I would like my Android app to instantiate it's home screen activity one time only. I am managing the back stack appropriately to achieve this but have just discovered an orientation issue when the app starts up.
Visually this orientation change only shows itself on the emulator. (probably runs too fast to be observed on a device).
Here's what happens :: -->
activity.onCreate()
activity.onDestroy()
activity.onCreate()
This sequence makes sense and is caused by the change in orientation. What does not make sense (to me) is that it happens at all because I have done the following to prevent an orientation change :: -->
AndroidManifest.xml contains
android:screenOrientation="portrait"
for all my activities and in the home screen activity onCreate() method, I'm calling
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
So why do I get an orientation change?
Add android:screenOrientation="portrait" in your manifest file where you declare your activity like this
<activity android:name=".yourActivity"
....
android:screenOrientation="portrait"/>
if you want to do using java code
try
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
before you setContentView for your activity in onCreate()
see here
Is there a way to make an application completely ignore a screen orientation change?
It is possible, quite easily, to override the default behavior and forbid a screen orientation change when the keyboard is open/closed.
Modifying the manifest
Open the manifest, switch to the Application tab and select the desired Activity you wish to override for the orientation change behavior.
Within Attributes you need to change two fields:
Screen orientation: select either portrait or landscape - whichever is desired. This will be the default layout.
Select events for Config changes you wish to override:
In this case these are keyboardHidden and orientation.
Modifying the Activity implementation
Now you need to override a single function within desired Activity.
Just add the function below to your Activity's class.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
This is the default implementation if using the Source->Override/Implement Methods
menu option.
That's it! Now your orientation will always be kept.
Remember that this setting is per Activity - so you need to repeat this step for each Activity you wish to forbid the orientation change!
(Based on SDK 1.1)
You can make the same change in code with the following line (called in an activity):
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Once you make this call, your application will stay in landscape (or portrait) mode. You can use the same call (with a different ActivityInfo enum) to make it sensitive to the orientation shifting again.
There's a full DevX article on the topic in Developing Orientation-Aware Android Applications.
(WARNING: since I've posted this link DevX has put up a registration wall.)
If you are setting either by AndroidManifest.xml or with the setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); you are going to run into issues with tablets. Their natural/default orientation is landscape.
If you truly want to completely ignore screen orientation changes I would use this setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); value. I talk more about it in Stack Overflow question Android natural sensor orientation help.
Here is the xml:
<activity
android:name=".MyActivity"
android:screenOrientation="nosensor"
android:configChanges="orientation|keyboardHidden|keyboard"/>
You can define your activity in the AndroidManifest.xml file like this:
<activity
android:name=".MyActivity"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden|keyboard"/>`
In this case you should set the property for each activity. I didn't find an inline solution for all applications.
Add this to the activity:
android:configChanges="orientation|screenSize"
<activity android:screenOrientation="portrait"></activity>
You want to read the current orientation and keep it this way throughout all the activity's lifetime, so what I did is the following, at the end of onCreate:
// choose an orientation and stay in it
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
else if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);