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 ?
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've searched for a solution to this problem and have not found anything. The problem is that I have created an activity and I want this to change the orientation of the screen when the user displays a dialog. The activity has started with the portrait orientation, which is defined in the AndroidManifest.xml to not be able to change when the user rotate the screen, it only changes the orientation when the user presses a button to show the dialog. But when this happens, the activity changes the orientation, but once changed it is closed. I have checked the logcat, but does not throw any exception.
This way I have defined my activity in the AndroidManifest.xml:
<activity
android:name=".ui.BarCodeActivity"
android:label="#string/title_activity_barcode"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.MainActivity" />
</activity>
And this is the code I use to change the screen orientation at that time:
#Override
protected void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
Log.e(TAG, "onSave startDialog: " + startDialog);
state.putBoolean("startDialog", startDialog);
}
#Override
protected void onRestoreInstanceState(#NonNull Bundle state) {
startDialog = state.getBoolean("startDialog", false);
Log.e(TAG, "onRestore startDialog: " + startDialog);
if (startDialog) {
showDialog();
}
}
private void showDialog() {
Log.e(TAG, "creating dialog");
Dialog barcodeDialog = new Dialog(BarCodeActivity.this);
barcodeDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialogInterface) {
dialogInterface.dismiss();
startDialog = false;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
});
barcodeDialog.setContentView(R.layout.code_dialog);
barcodeDialog.setTitle("Scan barcode");
ImageView barcode = (ImageView) barcodeDialog.findViewById(R.id.dialog_barcode);
barcode.setImageDrawable(barcodeDrawable);
TextView codeView = (TextView) barcodeDialog.findViewById(R.id.dialog_code);
codeView.setText(code);
/*barcodeDialog.show();*/
}
When the activity will start again and then closes, I have seen that in the logcat output strange, if not you will have something to see, I've been researching but I have not had luck with the solutions. This is the output:
08-01 19:21:21.600 31521-31521/com.my.app W/ResourceType﹕ Failure getting entry for 0x7f090058 (t=8 e=88) in package 0 (error -75)
08-01 19:21:21.600 31521-31521/com.my.app W/ResourceType﹕ Failure getting entry for 0x7f090058 (t=8 e=88) in package 0 (error -75)
08-01 19:21:21.610 31521-31521/com.my.app D/not﹕ got -1
08-01 19:21:21.610 31521-31521/com.my.app D/not﹕ got -1
08-01 19:21:21.620 31521-31521/com.my.app D/not﹕ got -1
08-01 19:21:21.630 31521-31521/com.my.app D/not﹕ got -1
What can be the error?
If it helps I have updated the Android Studio to 1.3 version.
Try changing
<activity
android:name=".ui.BarCodeActivity"
android:label="#string/title_activity_barcode"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.MainActivity" />
</activity>
to
<activity
android:name=".ui.BarCodeActivity"
android:label="#string/title_activity_barcode"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.MainActivity" />
</activity>
and test with this code in your activities.
#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();
}
}
I know this is old but when you use configChanges you must define both orientation and screensize as shown below if you support all the versions of android else take a look at this documentation.
Therefore, change android:configChanges="keyboardHidden|orientation"
to
android:configChanges="orientation|screenSize|keyboardHidden"
and it should work because we use that in our app.
Hope, this helps someone.
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();
}
}
I am trying to #Override onConfigurationChanged because I want to know when the keyboard down and when it is up but nothing happened.
What can I do? This is my source:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// 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();
}
}
I also added to the manifest this line
android:configChanges="keyboard|keyboardHidden"
Try comparing newConfig.keyboardHidden==Configuration.KEYBOARDHIDDEN_YES This applies for all the keyboards whether it is attached to the device or not.
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();
}
}