#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.
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 have some kind of app, where interface depends on orientation change. So I have two functions for interface portraitSet() and landscapeSet(). Here is the code how I catching up orientation change event:
$(window).on("orientationchange",function(event){
alert(event.orientation);
if (event.orientation == 'portrait')
{
$(portraitSet());
}
else if (event.orientation == 'landscape')
{
$(landscapeSet());
}
});
On launch app in portrait mode it works perfectly, but when start app in landscape mode and trying to set mobile into portrait mode my alert(event.orientation) giving me message "landscape", but mobile on portrait mode. Where is my mistake?
hi use this code.....
$(window).resize( function(){
var height = $(window).height();
var width = $(window).width();
if(width>height) {
// Landscape
alert("LANDSCAPE");
} else {
// Portrait
alert("PORTRAIT");
}
});
if you need more refer
I am looking for how we change the vibrate settings in Jelly Bean. I found that all the pre-JB vibrate settings have been deprecated, but don't see any new AudioManager.[change the vibrate settings] code anywhere. There is a setting "Vibrate when ringing" which I would like to know how to play with.
Thanks for you help.
In android4.1 you can use this to control "vibrate & ringing"
Settings.System.putInt(mContentResolver, Settings.System.VIBRATE_WHEN_RINGING, enable ? 1 : 0);
From the Documentation:
This method is deprecated.
Applications should maintain their own vibrate policy based on current ringer mode that can be queried via getRingerMode().
Seems like Google wants Vibration settings to be handled by each app for itself on an app to app basis, adjusting it's settings by querying getRingerMode().
Settings.System.VIBRATE_WHEN_RINGING is declared with #hide annotation so you may have troubles to use it in Android Studio.
So maybe you have to replace Settings.System.VIBRATE_WHEN_RINGING by its value : "vibrate_when_ringing"
It is annoying that it is declared with the #hide annotation, because it means that Google don't want external developpers using it...
This issue has plagued me for a couple of days now. Finally got it working. Make sure that you have the right permissions as well.
uses-permission android:name="android.permission.WRITE_SETTINGS"/
protected void onCreate(Bundle savedInstanceState) {
audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
}
Then in my on click listener I utilized the following:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (vibrate == 1) {
try {
Settings.System.putInt(getContentResolver(), "vibrate_when_ringing", 1);
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON); //Set it to never vibrate on ring:
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_ON); //Set it to never vibrate on notify
boolean vibrateWhenRinging;
vibrateWhenRinging = (Settings.System.getInt(getContentResolver(), "vibrate_when_ringing") == 1);
Toast.makeText(MainActivity.this, Boolean.toString(vibrateWhenRinging), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "System vibrate error", Toast.LENGTH_LONG).show();
}
} else {
try {
Settings.System.putInt(getContentResolver(), "vibrate_when_ringing", 0);
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF); //Set it to never vibrate on ring:
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF); //Set it to never vibrate on notify
boolean vibrateWhenRinging;
vibrateWhenRinging = (Settings.System.getInt(getContentResolver(), "vibrate_when_ringing") == 1);
Toast.makeText(MainActivity.this, Boolean.toString(vibrateWhenRinging), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "System vibrate error in else statement", Toast.LENGTH_LONG).show();
}
}
}
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.
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();
}
}