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.
Related
I have a video player in which I have two features:
If the auto-rotate is disabled and when the user clicks on fullscreen button I force the orientation to go in landscape view using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) and back to portrait view when fullscreen is exited.
I have overridden the onConfigurationChanged to detect the change in orientation when the auto-rotate feature is enabled by user and change the activity view according to the orientation.
Now the problem with this setup is that if user start the activity and goes in fullscreen and setRequestedOrientation get invoked once, later when auto-rotate is enabled, the activity doesn't receive the orientation changes and the onConfigurationChanged doesn't get called.
I read here (https://stackoverflow.com/a/6109206/5167868) that they wont work simultaneously.
I would like to know if there a way of implementing both as I have seen players like MX Player do this in their app.
After you fix the orientation of the screen by using setRequestedOrientation(), Auto Rotate feature won't work. So to make it work, you may do something like this:
After you set your orientation using: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE)
Call this:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
This shall make the auto rotate to work again!
I have create a custom media player.In which there is a button for landscape and portrait mode. When player opens media player,its all feature works fine.But on click fullscreen button, i set media player to landscape mode. After this button click auto screen orientation is not working, only user can change orientation by clicking button(button to change landscape/portrait). Is there any method to work both auto screen orientation with button(button to change landscape/portrait)?
Thanks.
In your manifest file, you have given permisssion for potrait and landscape, So you have avoid that, instead of that do it in your activity.
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 snap a photo, and it gets placed onto an ImageView. When the user sees the page where the Image View is, and I turn the phone sideways where the orientation is horizontal, the app immediately returns to the app's main screen. Any ideas?
Rotating the phone recreates the activity. Implementing the onSaveInstanceState() and onCreate() methods to properly save and restore the state of the activity should fix the problem.