Locking orientation to portrait force close in my phone - android

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

Related

Orientation change notification in Android

Trying to follow something. Boiling down to simplest: In my AndroidManifest.xml I have the following:
<application
android:allowBackup="true"
android:configChanges="orientation"
...
</application>
In my activity that runs I have this:
#Override
public void onConfigurationChanged(Configuration newConfig) {
Log.d("PrimesAreInP", "onConfigurationChanged called");
}
However when I change the orientation of the phone from portrait to landscape I never see the onConfigurationChanged() print. What else do I need to do to see this call?
Try adding this for activity tag:
<activity android:configChanges="orientation|screenSize|smallestScreenSize|keyboard|keyboa‌​rdHidden"
I think you are missing this dude :
super.onConfigurationChanged(newConfig);
and not forget to write this line in activity tag of your menifest :
android:configChanges="orientation|screenSize"
good luck !!!

Orientation Locking to landscape not working on emulator but on device

I have used following code in activity onCreate() to lock view only to landscape mode, it is perfectly working on device but not working on emulator, Since i dont have a device now it's hard to develop
code :
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Please help
Try this:
android:screenOrientation="sensorLandscape"
Put this Code[Attribute] in Activity tag of AndroidManifest.xml file it should work.
In your manifest file simply add
<activity
android:name="yourActivity name"
android:screenOrientation="portrait" >
</activity>
Apologies if this is too obvious.. You have to manually put your emulator in landscape mode, I think. I think it's the F9 key, but it's been a while since I've done that.
Try this way, hope this will help you.
Define this attribute "android:screenOrientation="landscape"" as your activity in AndroidManifest.xml
Try this
Put below line of code in your manifest
<activity>
android:screenOrientation="landscape"
</activity>
It works for me....

How to prevent Flurry change orientation of my activity?

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.

Get Locked Orientation

Is there a method to detect if the Auto-Rotation Lock of the device is on or off? My app needs to access that method to lock the orientation during runtime.
Add this in your Manifest file in your activity for the fixed orientation you want for your Activity
android:screenOrientation="portrait"
like
<activity android:name=".Settings" android:label="#string/app_name" android:screenOrientation="portrait">
or in java code you can use
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
In iPhone there is a method
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return YES;
}
To unlock orientation. if u want to make it lock then change its value (YES to NO) where u want to lock orientation

Android - Vertical layout only

How do I make sure my app is only for vertical layout?
I have tried android:screenOrientation="portrait" but that doesn't seem to do the trick.
You need to add to all your activity not for one only. I think you understood that setting is per application wide, but it isn't.
<activity android:name=".MyActivity"
android:label="My Activity"
android:screenOrientation="portrait">
Add the declaration to the activity tag in AndroidManifest for every Activity you want to be portrait-only.
If you want some group of your activities to be locked on PORTRAIT mode only, than you can choose the next way:
public abstract class BasePortraitActivity extends Activity {
#Override
protected final void onCreate(Bundle state) {
super.onCreate(state);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
performOnCreate(state);
}
protected abstract void performOnCreate(Bundle state);
}
And than just extend BasePortraitActivity where you need it. Or just add setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); to YourActivity.onCreate().
you have to change into AndroidManifest.xml.
for each activity you have to insert:
android:configChanges = "orientation"
android:screenOrientation = "portrait"
for e.g.:
<activity android:name=".YourActivityName"
android:label="#string/app_name"
android:configChanges = "orientation"
android:screenOrientation = "portrait">
This works for a single activity.. But there seems no application wide setting there.
In your manifest under activity add this :
<activity
android:name="com.zeus.MyProject"
android:screenOrientation="portrait"
>
Activity is to block just in case want to block all and only repeat this line of code in their Activitys.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// Here, talk to the java does not want that the user can rotate their activity.
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
or open your "AndroidManisfest.xml" and add the rows for the portrait mode as shown below.
android:configChanges="orientation"
android:screenOrientation="portrait">
Add android:configChanges="keyboardHidden|orientation" to the activity.
Put this attribute in layout root:
android:orientation="vertical"
It may help.
Just to confirm Pentium10's answer.
I was having a similar issue and adding android:screenOrientation="portrait" to the activity tag.
Did the trick for me.

Categories

Resources