How to use onConfigurationChanged() and newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE in android 2.3.3
Just write the below code into onConfigurationChanged
method and test
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
Log.e("On Config Change","LANDSCAPE");
}else{
Log.e("On Config Change","PORTRAIT");
}
and write the android:configChanges="keyboardHidden|orientation"
into your manifiest file like this
<activity android:name="TestActivity"
android:configChanges="keyboardHidden|orientation">
it's working at my side, i hope it helps you.
If you're on tablet also add |screenSize
to android:configChanges
Android 2.3.3 & 4.1.2 android:configChanges for detecting Orientation change
Hi Look at mine AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10"
android:maxSdkVersion="16"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ConfigrationTask"
android:configChanges="orientation|keyboardHidden"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
orientation is not working in 2.3.3?
You might be hitting this bug "Orientation does not change from landscape to portrait on Emulator on 2.3"
More than 45 people are facing the same issue with the androidd 2.3 emulator
http://code.google.com/p/android/issues/detail?id=13189
Host OS: Windows 7 SDK tools version : revision 8 Eclipse version: 3.5
ADT plug-in version: 8.0.1 Platform targeted by your project: 2.3
Version of the platform running in the emulator:2.3STEPS TO REPRODUCE:
1. Activity 1 (no screen orientation) - Activity 2 (screen orientation = landscape)
- Going from Activity 1 to 2 works fine. Pressing back changes the orientation of Activity 1 to landscape
EXPECTED RESULTS: Activity 1 should be in portrait mode
OBSERVED RESULTS: Behavior is observed only in gingerbread, works as
expected on froyo.
How is orientation change handled in the message queue?
Yes, it is atomic operation.
in pseudo it looks like:
void setNewOrientation(int state) {
currentState = state;
runOrientationChangedEvent();
}
And then usually Activity
is re-created. Only re-creating of Activity adds to the message queue, so it is possible to see if change device orientation fast enough.
You cannot get start and end of this process, because it just changes a few int variables which can tell you about current orientation like orientation:
getActivity().getResources().getConfiguration().orientation
or rotation
getWindowManager().getDefaultDisplay().getRotation();
So you can get event only after orientation is changed.
Android onConfigurationChanged not being called
A couple of things to try:
android:configChanges="orientation|keyboardHidden|screenSize"
rather than android:configChanges="orientation"
Ensure that you are not calling setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
anywhere. This will cause onConfigurationChange() to not fire.
Check that you are not using android:screenOrientation
in your manifest.
If none of that works, read through the Android doc on handling runtime changes and make sure you are doing everything correctly. There may be something somewhere else in your code that's causing the problem. http://developer.android.com/guide/topics/resources/runtime-changes.html
EDIT: As derrik pointed out, I assumed that you were changing the configuration with the accelerometer detecting what way the device was facing. If you want the configuration to change as the keyboard is shown/hidden the configChanges in the manifest must include keyboardHidden
as well.
How to detect orientation change in layout in Android?
Use the onConfigurationChanged method of Activity.
See the following code:
@Override
public void onConfigurationChanged(@NotNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
You also have to edit the appropriate element in your manifest file to include the android:configChanges
Just see the code below:
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
NOTE: with Android 3.2 (API level 13) or higher, 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, you must declare android:configChanges="orientation|screenSize" for API level 13 or higher.
Hope this will help you... :)
Check orientation on Android phone
The current configuration, as used to determine which resources to retrieve, is available from the Resources' Configuration
object:
getResources().getConfiguration().orientation;
You can check for orientation by looking at its value:
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
// In landscape
} else {
// In portrait
}
More information can be found in the Android Developer.
Related Topics
How to Change Bitmap Image Color in Android
Android Displaymetrics Returns Incorrect Screen Size in Pixels on Ics
How to Add an Image in Email Body
Android Keyboard Hides Edittext
How to Implement a Setonitemclicklistener Firebaserecyclerviewadapter
How to Get App's Permission for Each App? How to Do It Programmatically on Android
An Android App Remembers Its Data After Uninstall and Reinstall
How to Add Multiple Widgets in the Same App
How to Set "Android:Layout_Below" at Runtime Programmatically
Session 'App': Error Launching Activity
Getting a Photo from a Contact
Add a Search Filter on Recyclerview with Cards
Theme/Style Is Not Applied When Inflater Used with Applicationcontext
Return Value from Asynctask Class Onpostexecute Method
Copy Database from Assets to Databases Folder
How to Prevent Going Back to the Previous Activity
Upload a File Through an Http Form, via Multipartentitybuilder, with a Progress Bar