I am in trouble with Portrait and Landscape modes of my android application. So, I want to know how to restrict my application in anyone mode only. Thanks in advance to any reply with suitable answer and problem solving solution.
You can define your screen Orientation in your manifest file, activity tag
android:screenOrientation="landscape"
for more detail refer here.
add the below code in the activity tag of your manifest file...
android:screenOrientation="landscape"
Add android:screenOrientation to your activity tag in manifest file like below code:
<activity
android:name="com.packagename.YourActivity"
android:screenOrientation="portrait" >
</activity>
Try this:
<activity
android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden|keyboard"
android:screenOrientation="portrait" />
Related
The title of this question is not exactly reflects my question I have trimmed it because it went too lengthy. Sorry for my bad English too.
Okay,
I am using a library which has an activity, I am using that activity in my app. The orientation of that activity is working fine in tablets i.e., both landscape and portrait but in phones it sticks to portrait mode.
When I have seen the Manifest file of that library its like
<activity
android:name="DrawingActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:finishOnTaskLaunch="true"
android:screenOrientation="unspecified">
In the thread Android Orentation Changes. I came to know it should like android:screenOrientation="fullSensor" to work.
Now I am trying to override the screenOrientation property of the activity as said here
using tools:replace attribute but still its not working. Please can anybody help me to solve this issue.
This is how I'm trying to achieve.
<activity
android:name="DrawingActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:finishOnTaskLaunch="true"
android:screenOrientation="fullSensor"
tools:replace="android:screenOrientation">
If my question is not clear/too broad please let me know I'll edit my question.
Regards
You can use tools:node="merge", for example:
<activity android:name="DrawingActivity"
android:screenOrientation="fullSensor"
tools:node="merge">
</activity>
this node will be merged with the existing one and overrides any duplicates.
for more details about this refer to https://developer.android.com/studio/build/manifest-merge#node_markers
<activity
android:name="DrawingActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:finishOnTaskLaunch="true"
android:screenOrientation="fullSensor"
tools:replace="screenOrientation">
Dont use android:screenOrientation instead use screenOrientation
This will work:
tools:node="replace"
In your case it'd be:
<activity
android:name="DrawingActivity"
android:screenOrientation="fullSensor"
tools:node="replace" />
It will replace any attr added on the library which in this case will be 'screenOrientation' from 'unspecified' to 'fullSensor'.
Try this one:-
<activity android:name="DrawingActivity"
tools:replace="android:screenOrientation"
android:screenOrientation="fullSensor">
</activity>
Import library as module instead of using Gradle, so you can change Code
I want to run my app in portrait mode, I'm aware this isn't best practice but there are reasons to do this. and while I have disabled the rotation it still is able to rotate on some views will it doesn't on others.
I have this part of code in my Android Manifest:
<activity
android:name="<name>.app.MainActivity"
android:label="#string/app_name"
android:configChanges="keyboardHidden|keyboard|locale|orientation"
android:screenOrientation="portrait">
I'm using fragments to show different containers depending on user input.
this is the only activity that has fragments.
I have tried a few solutions on this site. including setting portrait mode on by code
You can do it something like below.
After rootView in your java add this line
getActivity().setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // programmatically
For example:
View rootView = inflater.inflate(R.layout.activityxml, container, false);
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
And also in your manifest change it:
android:configChanges="orientation|keyboardHidden"
as
android:configChanges="keyboardHidden"
<activity
android:name="com.test.activity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden" >
try this code
<activity android:name="com.myActivity" android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation"
>
</activity>
use below code before super.onCreate.first line of code.it force activity to portrait(of course both side of portrait)
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
the code that you entered android:configChanges="screenSize|orientation" just force activity to don't miss it's stats on rotate(onCreate call just once and if rotate onPause will call)
add Android Manifest:
tools:ignore="LockedOrientationActivity"
android:screenOrientation="portrait">
i have one activity in my manifest.xml my activity is CPohonApp but in this activity i have 2 config
this is my manifest
<activity android:name=".pupuk.CPupukApp" android:configChanges="keyboardHidden|orientation"></activity>
and
<activity android:name=".pupuk.CPupukApp" android:screenOrientation="portrait"></activity>
when run this code is not work.
how to run two config in one name activity in manifest.xml?
thank you.
sorry for my bad english
Alex
Alex when you set android:screenOrientation="portrait" then your activity remains in portrait mode so no need to declare orientation configChanges. Try this.
If you have more than one configuration for particular activity then no need to declare it more than one time . Declare all those in that single activity.
<activity android:name=".pupuk.CPupukApp" android:configChanges="keyboardHidden" android:screenOrientation="portrait"></activity>
add the two configurations to single activity initialization
<activity android:name=".pupuk.CPupukApp" android:configChanges="keyboardHidden" android:screenOrientation="portrait"></activity>
I have tried to freeze orientation using:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Although the display stays in portrait orientation, the activity is still recreated. Any ideas how to solve this?
How can the orientation of the application be locked such that the activity is not recreated on orientation change?
First, don't use setRequestedOrientation() if you can avoid it. Use the android:screenOrientation attribute in your <activity> manifest element instead.
Second, you will also need android:configChanges="keyboardHidden|orientation" in your <activity> manifest element to prevent the destroy/recreate cycle.
A more specific example of the activity section of the AndroidManifest.xml for portrait orientation:
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Where android:screenOrientation sets the initial orientation and android:configChanges voids the events that triggers the corresponding lifecycle methods on screen changes.
Try this:
1.- Set the desired screen orientation in your AndroidManifest.xml
android:screenOrientation="portrait|landscape"
It should look like this:
<application
android:allowBackup="true"
android:icon="~icon path~"
android:label="~name~"
android:supportsRtl="true"
android:screenOrientation="portrait"
android:theme="#style/AppTheme">
</application>
2.- Add this to your onCreate void(or wherever you want) in your java Activity File(Example: "MainActivity.java"):
super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
It should look like this:
protected void onCreate(Bundle savedInstanceState) {
super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);}
Now the screen wont move even if the Screen Rotation is on in the Device.
The best solution is to use the saved instance.
If you are locking the the screen orientation then it means you are forcing the user to use the app according to constraints set by you. So always use onSaveInstanceState. Read this link: http://developer.android.com/training/basics/activity-lifecycle/recreating.html
I am using the Android VNC viewer on my HTC G1. But for some reason, that application is always in landscape mode despite my G1 is in portrait mode. Since the Android VNC viewer is open source, I would like know how is it possible hard code an activity to be 'landscape'. I would like to change it to respect the phone orientation.
Looking at the AndroidManifest.xml (link), on line 9:
<activity android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden" android:name="VncCanvasActivity">
This line specifies the screenOrientation as landscape, but author goes further in overriding any screen orientation changes with configChanges="orientation|keyboardHidden". This points to a overridden function in VncCanvasActivity.java.
If you look at VncCanvasActivity, on line 109 is the overrided function:
#Override
public void onConfigurationChanged(Configuration newConfig) {
// ignore orientation/keyboard change
super.onConfigurationChanged(newConfig);
}
The author specifically put a comment to ignore any keyboard or orientation changes.
If you want to change this, you can go back to the AndroidManifest.xml file shown above, and change the line to:
<activity android:screenOrientation="sensor" android:name="VncCanvasActivity">
This should change the program to switch from portrait to landscape when the user rotates the device.
This may work, but might mess up how the GUI looks, depending on how the layout were created. You will have to account for that. Also, depending on how the activities are coded, you may notice that when screen orientation is changed, the values that were filled into any input boxes disappear. This also may have to be handled.
You can set the same data in your java code as well.
myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Other values on ActivityInfo will let you set it back to sensor driven or locked portrait. Personally, I like to set it to something in the Manifest as suggested in another answer to this question and then change it later using the above call in the Android SDK if there's a need.
In my OnCreate(Bundle), I generally do the following:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
You can specify the orientation of an activity in the manifest. See here.
<activity android:allowTaskReparenting=["true" | "false"]
...
android:screenOrientation=["unspecified" | "user" | "behind" |
"landscape" | "portrait" |
"sensor" | "nosensor"]
...
"adjustResize", "adjustPan"] >
In the manifest:
<activity android:name=".YourActivity"
android:screenOrientation="portrait"
android:configChanges="orientation|screenSize">
In your activity:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.your_activity_layout);
The following is the code which I used to display all activity in landscape mode:
<activity android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden"
android:name="abcActivty"/>
A quick and simple solution is for the AndroidManifest.xml file, add the following for each activity that you wish to force to landscape mode:
android:screenOrientation="landscape"
This works for Xamarin.Android. In OnCreate()
RequestedOrientation = Android.Content.PM.ScreenOrientation.Landscape;
That's it!! Long waiting for this fix.
I've an old Android issue about double-start an activity that required (programmatically) landscape mode: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
Now Android make Landscape mode on start.
Arslan,
why do you want to force orientation pro grammatically, though there's already a way in manifest
<activity android:name=".youractivityName" android:screenOrientation="portrait" />
Add The Following Lines in Activity
You need to enter in every Activity
for landscape
android:screenOrientation="landscape"
tools:ignore="LockedOrientationActivity"
for portrait
android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity"
Here The Example of MainActivity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="org.thcb.app">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:screenOrientation="landscape"
tools:ignore="LockedOrientationActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity2"
android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Doing it in code is is IMO wrong and even more so if you put it into the onCreate. Do it in the manifest and the "system" knows the orientation from the startup of the app. And this type of meta or top level "guidance" SHOULD be in the manifest. If you want to prove it to yourself set a break in the Activity's onCreate. If you do it in code there it will be called twice : it starts up in Portrait mode then is switched to Landscape. This does not happen if you do it in the manifest.
For Android 4.0 (Ice Cream Sandwich) and later, I needed to add these, besides the landscape value.
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
Using only keyboardHidden|orientation would still result in memory leaks and recreation of my activities when pressing the power button.
Use the ActivityInfo (android.content.pm.ActivityInfo) in your onCreate method before calling setLayout method like this
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
use Only
android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity"
Press CTRL+F11 to rotate the screen.