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);
}
Related
This is my code for onCreate():
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new GameView(this));
getSupportActionBar().hide();
setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
For some reason, some of the phones I've tested this app on cause the app to crash when it is opened in portrait mode. The app is supposed to and is set to be played in landscape mode. If, however, you open the app while holding the phone landscape style, it does open and will not crash.
I need the app not to crash when opened in portrait mode, and then it needs to go to landscape mode.
Thanks.
give screen orientation in manifest file
<activity android:name=".LoginPage"
android:screenOrientation="landscape"/>
Add this in your manifest file in your activity.
android:configChanges="keyboardHidden|orientation"
I'm making an Android video player.It has a function like that user can watch video in any orientation.I just use code as follow:
Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1);
It works, but when I add a function that user can lock the orientation, I just did it:
Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);
So I met some trouble. when I'm in landscape orientation and try to lock the orientation, the screen just turns to portrait.
Can anyone solve it or tell me another way to do with it?
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
OR
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
More info here: Developing Orientation-Aware Android Applications
Use following code Based up on your condition change if else statement
int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
or
you can set anyone landscape or portrait in your activity.its never change during the screen rotation
<activity android:name="MyActivity"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation|screenSize">
...
</activity>
In your AndroidManifest.xml, for each activity put
android:screenOrientation="landscape"
It forces the activity to landscape.
just you have ot add following property in you manifest.xml file.
android:screenOrientation="portrait"
like this way,
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
</activity>
That.sit
You can request ScreenOrinentation public void setRequestedOrientation (int requestedOrientation). You can use it like this
// For landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//OR for portrait
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//OR reverse landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
//OR for reverse portrait
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
requestedOrientation An orientation constant as used in ActivityInfo.screenOrientation.
Added in API level 1
The preferred screen orientation this activity would like to run in. From the screenOrientation attribute, one of SCREEN_ORIENTATION_UNSPECIFIED, SCREEN_ORIENTATION_LANDSCAPE, SCREEN_ORIENTATION_PORTRAIT, SCREEN_ORIENTATION_USER, SCREEN_ORIENTATION_BEHIND, SCREEN_ORIENTATION_SENSOR, SCREEN_ORIENTATION_NOSENSOR, SCREEN_ORIENTATION_SENSOR_LANDSCAPE, SCREEN_ORIENTATION_SENSOR_PORTRAIT, SCREEN_ORIENTATION_REVERSE_LANDSCAPE, SCREEN_ORIENTATION_REVERSE_PORTRAIT, SCREEN_ORIENTATION_FULL_SENSOR, SCREEN_ORIENTATION_USER_LANDSCAPE, SCREEN_ORIENTATION_USER_PORTRAIT, SCREEN_ORIENTATION_FULL_USER, SCREEN_ORIENTATION_LOCKED,
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!
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
One of my activities in my app has an android:screenOrientation="portrait" attribute in the manifest, as I don't want it to change orientation.
I'm trying to integrate Flurry like this:
in onStart:
FlurryAgent.onStartSession(this, "api_key");
FlurryAgent.initializeAds(this);
FrameLayout adsFrameLayout = (FrameLayout)findViewById(R.id.frameLayoutAdsContent);
FlurryAgent.getAd(this, "BannerTop-1", adsFrameLayout, FlurryAdSize.BANNER_BOTTOM, 0);
in inStop:
FlurryAgent.onEndSession(this);
and in the AndroidManifest.xml:
<activity
android:name="com.flurry.android.FlurryFullscreenTakeoverActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode"
android:screenOrientation="portrait" >
</activity>
What happens is that Flurry lets the activity to change orientation (when device is turned ti landscape). if I don't run the code in onStart the activity doesn't change its orientation.
thanks in advance,
Amitos80
What if you add: android:orientation="vertical" to the layout xml?
After a long research i found that this can't be done. flurry overrides my configuration and enables the screen to change orientation to landscape. if someone ever find a solution please share.