User specified Orientation on Activity - android

I like that the user can specify the orientation of the Activity that it wants: LANDSCAPE or PORTRAIT.
My Activities are declared in AndroidManifest.xml like this:
<activity
android:name=“.ActivityA”
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:configChanges="keyboardHidden"
android:windowSoftInputMode="adjustResize">
</activity>
And in the onCreate method of the Activity I call
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int preferedOrientation = /* Code that get the prefered orientation */
setRequestedOrientation(preferedOrientation);
...
}
But when the user specifies LANDSCAPE, the activity first appears in PORTRAIT and then recreates to LANDSCAPE.
How this can be avoided?
Thanks.

Use android:configChanges="orientation|screenSize" instead of android:configChanges="keyboardHidden"
<activity
android:name=“.ActivityA”
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:configChanges="orientation|screenSize"
android:windowSoftInputMode="adjustResize">
</activity>
See Documentation.
Caution: Beginning with Android 3.2 (API level 13), the "screen size"
also changes when the device switches between portrait and landscape
orientation. Thus, if you want to prevent runtime restarts due to
orientation change when developing for API level 13 or higher (as
declared by the minSdkVersion and targetSdkVersion attributes), you
must include the "screenSize" value in addition to the "orientation"
value. That is, you must decalare
android:configChanges="orientation|screenSize". However, if your
application targets API level 12 or lower, then your activity always
handles this configuration change itself (this configuration change
does not restart your activity, even when running on an Android 3.2 or
higher device).

Related

onConfigurationChanged does not work while change orientation

THe manaifest.xml of activity as below:
<activity
android:name="com.wd.wuc.SearchStatusActivity"
android:label="#string/app_name"
android:screenOrientation="sensor"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:configChanges="orientation|keyboard" >
</activity>
The Activity's code as below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_status);
System.out.println("onCreate!!");
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
System.out.println("onConfigurationChanged!!");
}
While I change orientation, I want to run onConfigurationChanged.
But the code only run onCreate method, but not onConfigurationChanged.
How can I modify it?
In the documentation (http://developer.android.com/guide/topics/manifest/activity-element.html), you can find:
If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it also changes when a device switches between portrait and landscape orientations.
You should try to add "|screenSize" to configChanges if you are targeting API 13 or higher:
android:configChanges="orientation|keyboard|screenSize"
android:configChanges="orientation|keyboard"
this line tells Dalvik compiler that you are handling changes of keyboard and Orientation.so kindly remove this line it will work for sure.
Replace
android:configChanges="orientation|keyboard"
with
android:configChanges="orientation|keyboardHidden"
in your manifest file.
Look this for your reference.
remove this from manifest file this tag. then it will work
android:configChanges="orientation|keyboard"

why is onCreate() being called after onConfigurationChanged?

I have created an activity and two layouts for the activity, one each for landscape and portrait mode. Both the layouts have same Views and IDs. In the manifest file, I have added
android:configChanges="orientation" in the activity.
Now, according to the documentation, the activity is not restarted if I mention configChanges and include the onConfigurationChanged method.
onCreate(Bundle savedInstanceState) {
Log.i("Message","inside oncreate()") ;
}
onConfigurationChanged(Configuration newConfig) {
Log.i("Message","inside onconfigurationchanged()") ;
}
My log is showing both messages when i change the orientation. Is there any way to stop the onCreate() method from being called when the orientation changes?
maybe you could add more configurations changes that you wish to ignore, since starting of a certain API on android, the orientation consist of other flags , as the documentation says about "orientation" :
The screen orientation has changed — the user has rotated the device.
Note: If your application targets API level 13 or higher (as declared
by the minSdkVersion and targetSdkVersion attributes), then you should
also declare the "screenSize" configuration, because it also changes
when a device switches between portrait and landscape orientations.
so, please try to use , and tell us if it helped :
android:configChanges=orientation|screenSize"
here's the documentation about "screenSize":
The current available screen size has changed. This represents a
change in the currently available size, relative to the current aspect
ratio, so will change when the user switches between landscape and
portrait. However, if your application targets API level 12 or lower,
then your activity always handles this configuration change itself
(this configuration change does not restart your activity, even when
running on an Android 3.2 or higher device). Added in API level 13.
EDIT: here's my simple code to show that it works:
public class MainActivity extends Activity {
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("Message","inside oncreate()");
}
#Override
public void onConfigurationChanged(final Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i("Message","inside onconfigurationchanged()");
}
}
manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<application android:allowBackup="true" android:icon="#drawable/ic_launcher"
android:label="#string/app_name" android:theme="#style/AppTheme">
<activity android:name="com.example.test.MainActivity"
android:label="#string/app_name" android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Official documentation said that "orientation" in manifest is able to prevent restarts when the screen orientation changes and "keyboardHidden" to prevent restarts when the keyboard availability changes.
All you need to do is declare the following codes in your manifest:
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name">
It works for my project.
If it doesn't work, try to change "keyboardHidden" to "screenSize".

How to control landscape and portrait programmatically in android?

I developed on application using android sdk 4.0 and I install that .apk file in my samsung tab. When I run that application it is working properly. If I change the tab portrait to landscape or in reverse the screen also changed.
But my requirement is irrespective of changing the mode either portrait to landscape to Landscape to portrait, my application should run in portrait mode only.
add android:screenOrientation="portrait" for each activity in your manifest.xml file.
You can do this programmatically too, for all your activities making an AbstractActivity that all your activities extends.:-
public abstract class AbstractActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
You can define mode in AndroidManifest.xml as follows,
android:screenOrientation="portrait" after the android:name=".activityName"
for example like this,
<activity android:name=".MainActivity" android:label="#string/app_name" android:screenOrientation="portrait">
and from activity class you can use following code,
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
You can use this for landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
To change to portrait mode, use the
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
In your manifest file,
<activity android:name=".MainActivity" android:label="#string/app_name" android:screenOrientation="portrait">
Then add this to your java class:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
If you search first, you'll find this question answered already. I'll post the answer again here though.
Go to your manifest file, and under the activity that you want to keep portrait only, insert the following line
android:screenOrientation="portrait"
An example is given below.
<activity android:name=".YourActivity"
android:label="Your Activoity"
android:screenOrientation="portrait">
this will make your activity forced to be in portrait mode only, even if you hold the device in landscape mode.

Screen orientation and values in manifest.xml

I want to use all activities in my form in landscape or portrait.
When user select orientation - this is valid for all activities.
Tried with "behind" option orientation. According to Google - orientation will depend on previous activity.
My first activity use setRequestedOrientation to set selected from user orientation.
Next activities have to follow same orientation.
Do I have to put setRequestedOrientation in their code too? Or really on 'behind' parameter in manifest? Putting setRequestedOrientation may be cause onCreate again?
UPDATE:
Tried "portrait" and setRequestedOrientation() - result is onCreate was called 2 times.
Problem is in next activity -> because of "portrait" in first activity - android started next activity with same orientation. It ignores "landscape" orientation which was set by me.
If you want to have fixed orientation for your activities then you can use-
android:screenOrientation="portrait"
android:screenOrientation="sensorPortrait"
as an attribute to that activity in that manifest. But if you want to set the orientation runtime depending upon what was the previous orientation while launching the application, you need to check for the previous orientation in onCreate() and then set it to that value programmatically there itself using setRequestedOrientation()
UPDATE:
As pointed by #s.co.tt use android:screenOrientation="sensorPortrait" for a better support on tablets.
For more details on the different values for android:screenOrientation and what each of them do, look at the docs:
https://developer.android.com/guide/topics/manifest/activity-element.html#screen
<activity
android:name=".Android_mobile_infoActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
To handle the changes in orientation, add the following line in AndroidManifest.xml:
android:configChanges="orientation|screenSize"
under <activity> tag as shown below:-
<application
android:allowBackup="true"
----------
---------- >
<activity android:name=".MainActivity"
android:configChanges="orientation|screenSize">
----------
</activity>
----------
</application>
Note: Whenever there is a change in the configuration such as orientation, screenLayout, screenSize, etc., the activity is restarted and its onCreate method is called. To prevent this, we must handle any changes in configuration.
Do it programmatically, for example in an Activity base class
I tryed its working mobile and tablet. use anyone portrait or landscape.
#Override
public void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//or
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
manifest.xml inside application tag
<activity
android:name=".Login_Activity"
android:label="#string/app_name"
android:screenOrientation="portrait" />
or
<activity
android:name=".Login_Activity"
android:label="#string/app_name"
android:screenOrientation="landscape" />
In Android studio 3.6.0
android:screenOrientation="portrait"
will give you error you have to either specify
android:screenOrientation="fullSensor" or android:screenOrientation="unspecified"
fullSensor Means either you have ON the "Rotate off" or not it will change the orientation based on you move the phone
unspecified Means if you have ON the Rotate off then it will stay only in that orientation and if not then it will change the orientation based on you move the phone.
From my experience I would recommend to setRequestedOrientation in every activity in onCreate method, onCreate won't be called again it's safe.
For now I put check in onCreate:
m_bSkip = (this.getRequestedOrientation() != MyApp.sInstance.GetScreenOrientation());
if (m_bSkip)
return;
When I enter oncreate and screen orientation is not desired - exit.
When I enter in onCreate and screen orientation is desired one - continue with initialization.
This fixes situation without need of keeping async task related to activity and check for new activity.
Of course all functions: onStart,onResume,onPausde,onStop... have to check this flag to avoid null pointer exception.
go to Android Manifest editor , down there you will see MainActivity.java
click on it , to the right - you will see a new window: scroll down and choose select orientation - "portrait"

Android, how to not destroy the activity when I rotate the device?

I have an app that works only in portrait mode, and I have made the changes in my manifest file for every activity the orientation to be portrait. But when I rotate the device, the activity recreates again.
How to not destroy the activity?
For API 12 and below: add
android:configChanges="orientation"
Add "screenSize" if you are targeting API 13 or above because whenever your orientation changes so does your screen size, otherwise new devices will continue to destroy your activity. See Egg's answer below for more information on using "screenSize"
android:configChanges="orientation|screenSize"
to your Activity in AndroidManifest.xml. This way your Activity wont be restarted automatically. See the documentation for more infos
From the official document flurin said,
Note: If your application targets API level 13 or higher (as declared
by the minSdkVersion and targetSdkVersion attributes), then you should
also declare the "screenSize" configuration, because it also changes
when a device switches between portrait and landscape orientations.
So if your app targets API level 13 or higher, you should set this config instead:
android:configChanges="orientation|screenSize"
The right solution is
android:configChanges="orientation|screenSize"
Android documentation:
The current available screen size has changed. This represents a change in the currently available size, relative to the current aspect ratio, so will change when the user switches between landscape and portrait. However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).*
I was messing this up for a little bit and then relized that inside the Manifest file I was putting the configChanges on the Application level and not on the Activity Level. Here is what the code looks like when it is correctly working for me.
<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:configChanges="orientation|screenSize|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
Now that Android supports split screen ("multi-window" in Android parlance), you'll probably want to add screenSize|smallestScreenSize|screenLayout|orientation as well. So to handle rotation and split screen you'll want something like this in android:configChanges
<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:configChanges="orientation|screenSize|keyboardHidden|smallestScreenSize|screenLayout">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
Look at this code in Floating Image. It has the most interesting way of handling screen rotation ever. http://code.google.com/p/floatingimage/source/browse/#svn/trunk/floatingimage/src/dk/nindroid/rss/orientation
write in manifest:
android:configChanges="orientation|screenSize|keyboardHidden"
and override this in activity that solved your problem:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}

Categories

Resources