How to disable only Reverse screen orientations in android activity - android

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();
}
}

Related

how to keep my app language without changing when changing screen orientation?

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();
}
}

getConfiguration().orientation doesn't work in Android Fragment

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;
}

Android Message when I try to rotate Camera

When I try and rotate the screen from landscape to portrait or vice versa on my phone when the camera preview is active,it displays the following message in the Logcat:
Error while configuring rotation 0x80001005
ERROR: failed check:(eError == OMX_ErrorNone) || (eError == OMX_ErrorNoMore) - returning
error: 0x80001005 - Error returned from OMX API in ducati
You can write your own toast when you rotate your screen:
#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();
}
}
Just a wild guess: is it possible that you set the rotation to something which is not a multiple of 90°?
try this:
if (portrait) {
params.set("orientation", "portrait");
camera.setDisplayOrientation(90);
} else {
params.set("orientation", "landscape");
camera.setDisplayOrientation(flipped ? 180 : 0);
}
camera.setParameters(params);

Android - onConfigurationChanged is not called when keyboard show/hidden

I need to detect hidden key board when hidden keyboard is pressed
My source code
<activity
android:name="com.teamios.info.activity.MainScreenActivity"
android:screenOrientation="landscape"
android:theme="#style/Theme.MyScreenTranNorman"
android:configChanges="orientation|keyboardHidden"
android:windowSoftInputMode="stateUnchanged|adjustPan" />
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
}
I tested in samsung galaxy nexus phone android os 4.2.1, but Toast didn't show when keyboard hidden
Please help me.
<activity
android:name="com.teamios.info.activity.MainScreenActivity"
android:theme="#style/Theme.MyScreenTranNorman"
android:configChanges="orientation|keyboardHidden"
android:windowSoftInputMode="stateUnchanged|adjustPan" />
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
} else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
}
}
And add
<activity android:name=".MyActivity" android:screenOrientation="landscape " > </activity>
in menifest class.
Did you add android:configChanges="keyboardHidden" in your manifest file ?

Toast not displaying when orientation is changed why..?

I am showing two different layouts on different orientations.
In both I am Toasting a message that orientation changed.
It showing in toast in portrait but not in landscape why..?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
System.out.println("Initial Oriatation is "+display.getOrientation());
int oriatation = display.getOrientation();
if(oriatation == 0){
Toast.makeText(getApplicationContext(), "Changed", Toast.LENGTH_SHORT)
.show();
createPort();
resumePort();
}else if(oriatation == 1){
Toast.makeText(getApplicationContext(), "Changed", Toast.LENGTH_SHORT)
.show();
createLand();
resumeLand();
}
}
Thanks in advance...!
You need to implement the onConfigurationChanged method which will detect when orientation has been changed.
To make sure that your activity calls this you need to specify android:configChanges="orientation|screenSize" in your manifest.
Then in your onConfigurationChanged Method you can have something like:
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();
}
See http://developer.android.com/guide/topics/resources/runtime-changes.html for further information.
Acording to doc, Display.getOrientation() is deprecated, suggesting to use getRotation() instead. However, looking at doc for getRotation() returned values, I assume that getOrientation() might return similar results, so try checking not only for 0 and 1, but for 2 and 3 as well and see what happens.
edit instead of else if(oriatation == 1) use
else if(oriatation % 2 == 1) // or `else if(oriatation == 1 || oriatation == 3)`
One strange thing was happening with me when i run this program in android2.3.1(API-9) at first time i get the toast msg that "Changed to Portrait"(i changed the "Changed" to "Changed to Portrait" and "Changed to Landscape".) and when i changes the orientation of screen from Portrait to Landscape i get "Changed to Landscape" but when i again changes the orientation to Portrait the emulator toast the msg "Changed to Landscape" once again.And Now all the time it shows "Changed to Landscape".I googled about the problem and find that when we change the orientation of screen activity loose its initial state and changed to the next state,So we have to hold the initial state of the screen to again see the Toast msg "Changed to Portrait".
There is one class SharedPrefrences who helps us to maintain the state.So try it hope It'll solve ur problem.

Categories

Resources