Toast not displaying when orientation is changed why..? - android

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.

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

How to disable only Reverse screen orientations in android activity

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

Android: Get current device orientation from service

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 {
//
}

Is there a way to tell if the soft-keyboard is shown?

is there a way to tell if the softkeyboard is shown in an activity or not?
I tried
InputMethodManager manager = (InputMethodManager)
getSystemService(getApplicationContext().INPUT_METHOD_SERVICE);
manager.isActive(v)
but isActive returns false only before the first time the keyboard is shown, but if the kb appears and then dismissed, isActive returns true also.
so is there any other method to check for this issue.
thanks
According to this POST
You cannot detect if soft keyboard is shown or not, but you can indirectly know that a soft key board is shown by knowing that the View of your activity is resized.
Imagine you have a ListView and at the bottom an EditText, you want to go to the bottom of the list when a soft keyboard is shown after user clicks the EditText.
You need to implement a subclass of ListView, then use it in your ListActivity or Activity or View.
public class ThreadView extends ListView {
public ThreadView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
#Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld) {
super.onSizeChanged(xNew, yNew, xOld, yOld);
if (yOld > yNew) {
setSelection(((ListAdapter) getAdapter()).getCount() - 1);
}
}
}
Hope this helps
PS. "check Configuration Changes" only works for hand keyboard.
You can detect the state AND coordinates of the software keyboard, using dumpsys shell command.
Because dumpsys requires permission.android.DUMP, which is a system application permission, you have two options: 1. use a rooted device to grant this permission. 2. override the problem using adb as described in my other answer.
Now, run the following command: dumpsys window InputMethod | grep "mHasSurface" to get the data you are looking for.
This is my idea. It is untested.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks whether a keyboard is available which is not hard keyboard
if ((newConfig.keyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO)&&(newConfig.keyboardHidden != Configuration.KEYBOARDHIDDEN_NO)) {
Toast.makeText(this, "soft keyboard visible", Toast.LENGTH_SHORT).show();
} else if (newConfig.keyboardHidden == Configuration.KEYBOARDHIDDEN_YES) {
Toast.makeText(this, "soft keyboard hidden", Toast.LENGTH_SHORT).show();
}
}
Please check Configuration Changes for your Activity
This for your AndroidManifest.xml
and this for your Activity class http://developer.android.com/reference/android/app/Activity.html#onConfigurationChanged(android.content.res.Configuration)
You will need to #Override the public method onConfigurationChanged(android.content.res.Configuration) of your Activity to be able to handle, for example, these values:
hardKeyboardHidden,
keyboard,
keyboardHidden
For all possible values check http://developer.android.com/reference/android/content/res/Configuration.html
You will see there something like this:
HARDKEYBOARDHIDDEN_NO
HARDKEYBOARDHIDDEN_UNDEFINED
HARDKEYBOARDHIDDEN_YES
KEYBOARDHIDDEN_NO
KEYBOARDHIDDEN_UNDEFINED
KEYBOARDHIDDEN_YES
KEYBOARD_12KEY
KEYBOARD_NOKEYS
KEYBOARD_QWERTY
KEYBOARD_UNDEFINED
Also there you will be able to read something like this:
public int hardKeyboardHidden // A flag indicating whether the hard keyboard
// has been hidden.
public int keyboard The kind of keyboard attached to the device.
public int keyboardHidden A flag indicating whether any keyboard is available.
UPDATE:
Here is a specific example of what I´m talking about:
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();
}
// Checks whether a hardware keyboard is available
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();
}
}

Set ORIENTATION_LANDSCAPE not working Android

#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.

Categories

Resources