Android backend with portrait orientation - android

I try to change orientation for android backend but it doesn't change
at all..
I am trying with adding :screenOrientation="portrait" to my
manifest.xml but with no resoult...
Any suggestion how to change my orientation to portrait? Becouse in
default it is landscape...
Michael Bayne helped me on google goups with:
You want to add the following to android/src/.../YourGameActivity.java:
#Override public boolean usePortraitOrientation () {
return true;
}
Thanks Michael

Try setting it in your manifest like this
android:label="#string/app_name" android:screenOrientation="portrait"

Related

How to lock screen rotation in the code on Android 4.2 and lower?

I have used ActivityInfo.SCREEN_ORIENTATION_LOCKED and ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED to control screen rotation in Android 4.4,it is good.But when I set the ActivityInfo.SCREEN_ORIENTATION_LOCKED in Android 4.2 ,it is useless.
How can I do it?
You have to declare in your manifest
<activity android:name=".MyActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
also you can use android:screenOrientation="landscape"
And in your code something like
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Also you can see this guide.
You'll have to override it programatically. Get the current orientation, then if you toggle to fix the state, set that orientation as your application's orientation.
For Example:
int rotation=getResources().getConfiguration().orientation;
//ORIENTATION_LANDSCAPE = 2, ORIENTATION_PORTRAIT=1
if(rotation==1){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}else{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
This will lock it to the current orientation.
And in order to release it, simply use ActivityInfo.SCREEN_ORIENTATION_SENSOR like this:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
That's it.
Now, the final code will look something like this:
if(allow_screen_change.equals("yes")){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}else if(allow_screen_change.equals("no")){
int rotation=getResources().getConfiguration().orientation;
//ORIENTATION_LANDSCAPE = 2, ORIENTATION_PORTRAIT=1
if(rotation==1){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}else{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
I just made this and tested on HTC One X, running Android 4.2 and it works just fine. Hope it helps!

Android orientation change even after locking it in system settings

as in the topic, ive got a trouble with locking screen orientation after its been locked in device's settings. Its a landscape app. Ive read couple threads about similar problem but havent got any revelant answer.
Thanks in advance
Mac
You can add android:screenOrientation="landscape" in your activity tag to specify the mode of screen orientation in your manifest file
<activity
android:name="com.xyz.myapp.SplashActivity"
android:label="#string/app_name"
android:screenOrientation="landscape" >
Hope it helps .
Add setRequestedOrientation in the onCreate method like below
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.homepage);
This would set to landscape if auto_rotate is off, otherwise if would rotate normally as long as it wasn't set to land/port in the manifest.
if(android.provider.Settings.System.getInt(getContentResolver(),Settings.System.ACCELEROMETER_ROTATION, 0) != 1){
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

onConfigurationChanged is not getting called any time in android

I have create sample program to test Orientation. If portrait mode and I tilt the phone the other way round I want my sample app to reversePortrait. After reading lot of questions on this - Figured we need to need use:
onConfigurationChanged(Configuration newConfig)
I was trying out the following:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
Configuration c = getResources().getConfiguration();
Log.e("Config",""+c);
if (c.orientation == Configuration.ORIENTATION_PORTRAIT)
{
// portrait
Log.e("On Config Change","portrait");
}
else if (c.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
// landscape
Log.e("On Config Change","LANDSCAPE");
}
}
manifest.xml
android:configChanges="orientation"
tried even this
android:configChanges="orientation|screensize"
Strange I am not getting logs inside OnConfigurationChange method.
Not sure. What is the problem here?
I am using minimum api 8 to target Api 18.
Can somebody help me out with this?
Thanks!
I had a same issue after changing target API to 19(4.4 kitkat)
You need to add layoutDirection attribute for targeting higher than version 17.
my attribute looks like:
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|layoutDirection"
The key is to add 'layoutDirection'
I just encountered this problem, and fixed it with this weird little trick.
Set your screenOrientation in your manifest, like so:
android:screenOrientation="sensor"
That fixed it for me.
Your code is no problem
Modify your manifest.xml
android:configChanges="orientation|keyboardHidden"
I hope it can help you

Locking orientation to portrait force close in my phone

I want my app to lock into portrait mode. For this I used this code :
<activity android:name="MyActivity"
android:label="#string/app_name" android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"
>
and in MyActivity class :
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
This code work correctly in emulator, but when I install and run in my phone, the app is force closed.
How can I solve this problem?
Yo already use in manifest then there is no need to use by pragmatically. So remove the onConfigurationChanged method from your code.
I am using
android:screenOrientation="portrait"
in the manifest file, no other code in Java side and is working.
I see two problems here:
Remove android:configChanges="orientation|keyboardHidden" from manifest
Remove onConfigurationChanged from java
and it should work just fine

Disable screen rotating on Android [duplicate]

This question already has answers here:
How do I disable orientation change on Android?
(13 answers)
Closed 10 years ago.
When I press a button, I would like to disable screen rotation on all my activities. How can I do that?
BTW, the phone can be located in landscape or portrait position when the user click the button.
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
You can change your AndroidManifest.xml to
<activity android:name="MainActivity" android:configChanges="orientation">
Which informs the OS that you will handle these config changes (by doing nothing.)
See How do I disable orientation change on Android?
I've found the solution:
in manifest.xml:
<application android:icon="#drawable/icon" android:label="#string/app_name" android:theme="#android:style/Theme.NoTitleBar" android:name="MyApplication">
in MyApplication.java:
public class UPackingListApplication extends Application {
public void onConfigurationChanged(Configuration newConfig) {
newConfig.orientation = [orientation we wanna to use (according to ActivityInfo class)];
super.onConfigurationChanged(newConfig);
}
}

Categories

Resources