Set initial screen orientation - android

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!

Related

playing video fullscreen mode in webview fragment android

I have untypical problem. I have fragment with webview and I need to play video in fullscreen. I have no problems with it. But when screen orientation changes - fragment recreated. But I need to continue playing video in fullscreen.
I can't use android:configChanges="orientation|screensize" in AndroidManifest to MainActivity, because MainActivity has two different layout for lanscape and portrait mode.
you can programatically force the screen orientation to be landscape on your fragment, so if the user rotates it do not change(you said you want the video to be fullscreen only so no reason to change a thing when it rotates), and when your activity is open but the fragment not the override on onConfigurationChanged will not be called.
So simply add this on your fragment:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
It should works... if for any reason doesn't, try to use the same logic on onAttach/onDetach or to check the config change of the activity (if the fragment is open lock the orientation), but I think the code above should solve your problem ;]

Force Android orientation to landscape

As the title suggests, I am having trouble locking the orientation.
I am using the below code:
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
This is working ok as far as it loads in landscape and when I rotate the mobile, it stays in landscape.
The problem however, is when the power button is pressed to lock the phone, upon unlocking the app is now forced portrait.
Is there a way to capture the unlock event and force it to goto landscape again or any other method?
Thanks in advance.
Just fixed it myself, if anyone else is experiencing the same issue try the following:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
Set this in the onCreate() method, as well as in the onResume(), you can also set it in the onStop() as well:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Letting user change default orientation

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 :(

Loading picture from camera into ImageView

In my app, I have it so that when the user clicks on the Camera button it takes him to the camera using an intent.
He then takes the picture and it should load it back into the ImageView I have on the same screen as the Camera button.
I think something is wrong with my screen orientation. When I load the camera view it switches to horizontal orientation, and when it closes it switches back to vertical orientation and my whole screen resets back to default.
This is the error I get in logcat:
12-03 20:14:38.440: E/SpannableStringBuilder(15134): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
Any ideas?
EDIT:
Here is what happens:
I click on the camera button.
the screen camera view switches to horizontal mode, but I am holding it vertically so it switches to vertical.
I take the picture in horizontal mode.
The camera app asks me to save my pic or cancel it in vertical mode.
I click save.
My app loads in horizontal mode and places the picture inside the imageView.
The app then switches back to vertical mode and resets the whole screen.
I put your alls code in, but it isn't helping.
Also the point to be noted is that camera is by default in android works in landscape mode only. So no matter what you screen orientation is, the camera will launch in horizontal mode only. Also as above mentioned do
android:configChanges="orientation"
And handle this in your activity be using the below callback
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
The reason that your activity gets reseted is because of the screen orientation change made by the camera intent.
You can put android:configChanges="orientation" inside your activity in the androidManifest.xml
Once you have that, it would not destroy and recreate your activity.
Please see http://developer.android.com/guide/topics/manifest/activity-element.html#config for more details
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
All I did was add that line of code and it worked perfectly.

android:configChanges="keyboardHidden|orientation" Issue Android

Am using android:configChanges="keyboardHidden|orientation"for switching between portrait and landscape modes. But when user opens softkeyboard in portrait and changes to landscape keyboard should hide or dissappera but its not happening with that.
If you are intercepting orientation without overriding public void onConfigurationChanged(Configuration newConfig) in your Activity it is normal that the keyboard remains open.
You can either
override this method to reset the display
or avoid intercepting orientation to let the Activity restart (in which case your Activity will be reinitialized and onCreate() will be called again)
I'm not sure in it but I think that you need to write keyboardHidden|orientation|portrait (or landscape).

Categories

Resources