I would like to allow the user to change the option of allowing Android to be displayed in portrait or landscape. What I do is allow the user to set a preference and then force it on the program.
At this point, I'm using code like so inside onCreate:
prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (prefs.getBoolean("landscapeModeEnabled", false)) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
super.onCreate(savedInstanceState);
The problem now is that it creates the Activity twice if the screen is not in the 'forced' orientation. That is, a phone in default (portrait) orientation will load the screen twice if the user wants a landscape orientation.
I know I can set this using XML, but that would not allow the user to change the preference of the orientation.
I'm using a SherlockFragmentActivity from Action Bar Sherlock, if that matters.
Have 2 Activities like this:
mActivityLand
mActivityPort
with different orientation in the Manifest.
Check the preference in previous Activity and launch respective Activity. Probably not the best solution :(
Related
I have an Activity where the 1st Fragment (whose orientation is locked) solicits input from a user. This data is used to determine how to orient the 2nd Fragment. This orientation will also be locked.
I am guessing I will have to make a call to setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) either from the Activity or the 2nd Fragment.
My question is where best to do this. Does it really make a difference? Can this action be performed w/o undergoing an Activity restart?
If changing the setRequestedOrientation state requires the screen to rotate then it will restart the activity.
For example if you request ActivityInfo.SCREEN_ORIENTATION_PORTRAIT while the screen is in landscape, it will restart.
However it will not restart if the phone is already in that orientation.
I have one activity which i am using as host for calling all my fragments.I am using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) for setting screen in landscape mode.But problem is that it is making every fragment in landscape mode because their is only one parent activity for all of them.I can not use android:screenOrientation="landscape" because this will also work the same.
I want one of my fragment to be displayed always in landscape mode and remaining fragment as according to device orientation.Is their is another ways for doing it?
If you wanna show one specific Fragment in landscape you can call
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
in the onCreate() of your Fragment.
In this Fragments onPause() f.e. you can call
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
which is the default value and enables screen rotation changes.
I am making an app where the first screen I want to view is Portrait. I then go to the next activity which is set in landscape.
The problem i'm facing is that if I return to the first screen it will be set as landscape, evidently inheriting the orientation from the previous screen.
Is there a possible way to set the initial screen orientation as portrait which is set but the user can then go ahead and change it to landscape when they want?
Thanks
EDIT: SOLVED
Thank you for your suggestions. I've overcome the issue. It's slightly difficult to explain but will try my up most.
So my first activity has a feature whereby if the phone is turned to landscape it will show an image, full screen. And again, if the phone was portrait, the portrait view, where I have a list view.
The second activity loads an editing page to let me change that image.
So now if i press back on the editor activity. The first activity would load into a list view in LANDSCAPE. which is NOT what I want.
therefore in order to get around this. I have firstly disabled the back button when the first activity is landscape so the user can not remove himself from the view without turning the phone to landscape.
I then moved the code:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//Show landscape picture
}
}
to the top of my class before the onCreate method.
This has solved my issue so that now, if I was to go back to my first activity whilst in landscape the only thing that would show is the landscape view.
I hope that was clear enough for all to understand :)
Again thanks for the help!
you can set the screen orientation programmatically in the onCreate()- method:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
just right before the call of super.onCreate();.
I hope, I could help you.
Best regards,
G_J
You can change the orientation of individual activities using this in the manifest file of your application within each of your activity's tags:
android:configChanges="orientation"
with either
android:screenOrientation="portrait"
OR
android:screenOrientation="landscape"
as per your needs
you can set the screen orientation programmatically in the onCreate() method:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
just right after the call of super.onCreate();.
OR
You can also specify the orientation in the Manifest file like this
<activity android:name="com.example.project.activity"
android:screenOrientation="portrait"/>
Thank you for your suggestions. I've overcome the issue. It's slightly difficult to explain but will try my up most.
So my first activity has a feature whereby if the phone is turned to landscape it will show an image, full screen. And again, if the phone was portrait, the portrait view, where I have a list view.
The second activity loads an editing page to let me change that image.
So now if i press back on the editor activity. The first activity would load into a list view in LANDSCAPE. which is NOT what I want.
therefore in order to get around this. I have firstly disabled the back button when the first activity is landscape so the user can not remove himself from the view without turning the phone to landscape.
I then moved the code:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//Show landscape picture
}
}
to the top of my class before the onCreate method.
This has solved my issue so that now, if I was to go back to my first activity whilst in landscape the only thing that would show is the landscape view.
I hope that was clear enough for all to understand
Again thanks for the help!
According to the Google example here I developed an app based on fragments.
My main activity contains a listfragment of titles and, if it is created in landscape mode, a details fragment. If the app is startet in portrait mode, the main activity contains only the listfragment and, if a list item is clicked, start a new activity which shows the detailsfragment.
If I stay in either the portrait or landscape mode, everything works fine. But as soon as I change the orientation multiple problems occur.
1st problem: starting in portrait mode, then changing to landscape mode, the activity is added to the activity stack twice and I have to press the back button twice to close my app. I cant image this is the way Google wants this to work, so how do I avoid this?
2nd problem: when changing from landscape mode to portait mode, the list is shown and not the detailsfragment with the currently selected item. Therefore, all the user input in my detailsfragment is lost. This is just annoying and I don't know how to handle this. Do I have to care about the orientation change programmatically in every activity?
3rd problem: When I switch between n details in landscape mode, as soon as I change to portrait mode, I have to press the back button n times to close my add as the fragments are in the back stack (although they aren't visible any more). Do I have to clean the back stack myself in orientation change?
There is one thing about Activities. That is, when you change the orientation, the Activity restarts unless you do the following:
-First, add this in your manifest (inside the activity tag), so you will be able to tell the application what to do in case you change the orientation:
android:configChanges="orientation"
-Second, implement the following method in case you need to to something in case the orientation changes. If not, with the one before the user won't loose its data.
onOrientationChanged (int orientation)
I built a search widget based on the code, Searchable Dictionary v2. Everything is working fine, but there is one problem.
If I rotate the screen from portrait to landscape or vice versa when the search dialog box is on, the text edit box and keyboard screen get disappeared and runs again.
Is there any way that I can make that it switches the edit box into appropriated mode(landscape or portrait) without exiting and re-running the dialog box?
You can check the behavior with the searchable dictionary.
I want something like the google search widget. It just switches edit box mode while it keeps the screen.
Once the screen orientation is changed, Android creates a new activity/view, so you'll may have to handle screen orientation by yourself. I used this solution in an app successfully, I assume it also applies to widgets.
See this page on how to do that:
http://www.androidpeople.com/android-how-to-handle-screen-orientation-change-issue/
Some other helpful hints (basically the same solutions) are here:
How do I handle screen orientation changes with an activity started within a tab's activity
How to handle screen orientation change when progress dialog and background thread active?
Some information concerning onPause() and onSaveInstanceState() (which may not apply as you're using a widget, but just in case :) ) is here:
How do I disable orientation change on Android?
What i did was to lock the orientation while the dialog is displayed, then unlock it when finished:
int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); //locks landscape
}
else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); //locks port
}
// do work
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);