I try to get the screen orientation in an Android Fragment using getResources().getConfiguration().orientation but it returns the same result even though I rotated the device in portrait and landscape mode. I also used in in onConfigurationChanged() (see code below) and the configuration received as parameter reports the corect screen orientation but the generic method of getting the screen orientation always reports the same orientation mode in which the activity started(either landscape or portrait).
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
Log.e(TAG, "NEW CONFIG: ORIENTATION_LANDSCAPE");
else
Log.e(TAG, "NEW CONFIG: ORIENTATION_PORTRAIT");
Configuration config = getResources().getConfiguration();
if(config.orientation == Configuration.ORIENTATION_PORTRAIT)
Log.e(TAG, "config: PORTRAIT");
else
Log.e(TAG, "config: LANDSCAPE");
LayoutInflater inflater = LayoutInflater.from(getActivity());
populateViewForOrientation(inflater, (ViewGroup)getView());
}
For example if the activity started in landsape mode the first log output will be:
1.a "NEW CONFIG: ORIENTATION_LANDSCAPE"
1.b "config: LANDSCAPE"
after I rotate the device in portrait mode:
2.a "NEW CONFIG: ORIENTATION_PORTRAIT"
2.b "config: LANDSCAPE"
As you can notice, the config reports the same orientation mode even though the device has different orientation mode.
So what is happening here? Thank you! :)
Some times the easy solution is not to fight the system and just to accept the facts as they are. Just update the configuration as they change.
Try this:
private Configuration configuration;
private int getOrientation(){
return getConfiguration().orientation;
}
private Configuration getConfiguration(){
if (configuration == null) {
configuration = getResources().getConfiguration();
}
return configuration;
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
configuration = newConfig;
}
Related
I finished my app with multi languages , but when the device orientation is changed to landscape then app's language is changed .
can I use (onSaveInstanceState) method to keep app language without changing when rotating screen?
what is the code I have to insert ??
Try this in your manifest
<activity
android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize">
</activity>
And then
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// you can do something here
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
// orientation_landscape
Toast.makeText(this, "orientation_landscape", Toast.LENGTH_SHORT).show();
} else {
// orientation_portrait
Toast.makeText(this, "orientation_portrait", Toast.LENGTH_SHORT).show();
}
}
I am making an application multi lingual and handling orientation as well and i want to logout the user if Locale is changed i implemented this but now when user change orientation then it will logout the user.
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
boolean deviceLanguageOverridenWithDefaultLanguage = PersistentManager.isDeviceLanguageOverridenWithDefaultLanguage();
if(deviceLanguageOverridenWithDefaultLanguage)
{
AppUtil.setApplicationLanguage(AppConstants.APPLICATION_DEFAULT_LANGUAGE);
}
if(AppUtil.isSupportedLanguage(newConfig.locale))
{
UIUtil.showLogoutAlertDialog(this, getString(R.string.dialog_log_out_message_change_language_log_out), getString(R.string.dialog_log_out_title_log_out), false);
}
}
Is there any way to disable Reverse Landscape and Reverse portrait orientations in an android activity. I used the below code.but reverse landscape is coming on that.
rotation = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay().getRotation();
System.out.println("Rotation Value : " +rotation);
if(rotation==0){
System.out.println("portrait");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);}
if(rotation==1){
System.out.println("landscape");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);}
if(rotation==2 )
{
System.out.println("reverse portrait");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
}
if(rotation==3)
{
System.out.println("reverse landscape");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
}
Add android:configChanges="keyboardHidden|orientation" to your AndroidManifest.xml. This tells the system what configuration changes you are going to handle yourself, in this case by doing nothing.
<activity
android:name="MainActivity"
android:screenOrientation="portrait" //This line only if you want to lock your screen Orientation
android:configChanges="keyboardHidden|orientation|screenSize">
Check http://developer.android.com/reference/android/R.attr.html#configChanges for more details.
Then override onConfigurationChanged :
http://developer.android.com/guide/topics/resources/runtime-changes.html
#Override
public void onConfigurationChanged(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();
}
}
This question involves 3d geometry and solid understanding of Android sensors. I have spent weeks looking for a solution without success and would appreciate community help.
Is it possible to get the current device orientation (portrait or landscape) from a service in Android? I do not need continuous update from the sensors; just the current device orientation. The device orientation should report correctly when held in landscape even if the launcher is portrait (because the user has auto-rotate set to off).
Sample/example code will be highly appreciated.
Thanks.
Yes you may
WindowManager windowService = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
int currentRatation = windowService.getDefaultDisplay().getRotation();
if (Surface.ROTATION_0 == currentRatation) {
currentRatation = PORT;
} else if(Surface.ROTATION_180 == currentRatation) {
currentRatation = PORT;
} else if(Surface.ROTATION_90 == currentRatation) {
currentRatation = LAND;
} else if(Surface.ROTATION_270 == currentRatation) {
currentRatation = LAND;
}
You can check resource in service by below
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
//
} else {
//
}
#Override
public void onConfigurationChanged(Configuration _newConfig) {
if (_newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast toast = Toast.makeText(this, "test orientation_landscape",
Toast.LENGTH_LONG);
toast.show();
setContentView(R.layout.proceed);
}
if (_newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast toast = Toast.makeText(this, "test orientation_portrait",
Toast.LENGTH_LONG);
toast.show();
setContentView(R.layout.proceed);
}
super.onConfigurationChanged(_newConfig);
}
when i change the emulator orientation, the toast is not shown...?
SDK 1.6
i have set the android manifest config -> orientation
When the emulator is changed from portrait to landscape, no effect is shown,
but from landscape to portrait, works fine.
If there is any method by which i can keep my app display intact, without any orientation change, when the device is changed in orientation, please let me know that also.
Advanced Thanks for help.