What key gets pressed to hide soft keyboard in android - android

I'm trying to detect when the soft keyboard gets closed, I found this code snippet
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK){
//detects that keyboard was hidden
}
return super.onKeyPreIme(keyCode, event);
}
but this needs to be added to a subclass that extends editText, I'm not intrested in implementing the functionality I want to add on keyboard hidden in all my app, I want to add it for just one Activity, I tried onKeyDown, onKeyBack, onKeyUp, onBackPressed nothing seems to log the back press that closes the soft keyboard.
so my question is there a way to detect the click on the button that hides the keybaord?

Related

How to close keyboard on pushing Back button in a webview?

After click on the input field in android webview the keyboard appears. When I click on the back button I would like the keyboard to close. If the keyboard is closed and the back button is pressed, I need to go to the previous page. For example, Chrome behaves like this.
I did not find a reliable way to determine whether the keyboard is open or not. Maybe I'm going the wrong way and I can do without determining the openness of the keyboard manually?
Override onKeyDown method in your Web View Activity
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)){
hideSoftKeyboard();
}
}
Use this to hide soft keyboard
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);

Android context button blocks back button functionality

I have a problem in all my activities, when I press the back button it works properly, but when I press the context menu button before, nothing happens when I press back.
I am talking about the phone buttons, not the toolbar icon for back navigation. Has anyone ever dealt with this?
EDIT:
No menu shows, onPrepareOptionsMenu() is removed, the phone vibrates when I press back but no action.
I see that if I inflate a menu, it worsk properly after the first back that closes te popup.
I see that this happens only if I set the toolbar with setSupportActionBar(toolbar);
I have nearly the same problem, this is my case:
AppCompatActivity with toolbar set with setSupportActionBar(toolbar);
I'm using a navigation Drawer (dont know if this can affect)
onBackPressed() is overriden to prompt logout dialog
All works fine until i press the device menu button, then the app begins to ignore the back button.
This is what i have seen after pressing the device menu button
The method onBackPressed() is not called after this
If you set an onKeyUp(int keyCode, KeyEvent event), it receives the event (and you can check that de keyCode is the same as KeyEvent.KEYCODE_BACK)
I don't know why the onBackPressed stops from being called, tried without overriding onBackPressed and onKeyUp and the problem is still the same.
I know this is not the best solution, but to fix this problem i'm using the next code:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_BACK){
onBackPressed();
return true;
}
return super.onKeyUp(keyCode, event);
}
Hope this helps.

Android: how to simulate back button to hide a keyboard?

This problem may seem trivial but I wasn't able to find any nice and simple solution.
I've got an activity with a EditText and a software 'back' Button which simply calls finish() method of activity.
When I click on the EditText, there is a soft keyboard shown to input the text.
I want to achieve the following functionality when clicking the 'back' button (exactly the same as it is with the hardware back button):
- when the Keyboard is hidden, the onClick method should call finish() to end the activity
- when the Keyboard is shown, the onClick methond should hide the keyboard.
Is there any simple way to do that?
Keyboard Pasition
finding if keyboard is hidden or not?
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Selection of Back Button
1)First you have to detect the back key in functionality :
here is code:
start changing the ‘Back’ button, behavior, you need to override the onKeyDown()
method and than check if the desired button has been pressed:
//Override the onKeyDown method
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//replaces the default 'Back' button action
if(keyCode==KeyEvent.KEYCODE_BACK)
{
//do whatever you want the 'Back' button to do
//as an example the 'Back' button is set to start a new Activity named 'NewActivity'
this.startActivity(new Intent(YourActivity.this,NewActivity.class));
}
return true;
}
at least for Android 2.0.
#Override
public void onBackPressed()
{
//do whatever you want the 'Back' button to do
//as an example the 'Back' button is set to start a new Activity named 'NewActivity'
this.startActivity(new Intent(YourActivity.this,NewActivity.class));
return;
}

How to handle backpressed (keyDown - in previous version) in order to show a confirm dialog for exit the application?

I've made the logic for showing a confirm dialog when the user press back button by overriding backPressed method, but this implies an unusual behaviour. If soft input keyboard is shown, on back key event, it must be hidden and other back key event must launch the confirmation dialog. There is a way to achieve this? Maybe by detecting if soft input keyboard is up and bypass the confirmation dialog?
Here is a code sample to make this clear:
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
//here is the mystery
if (soft keyboard is visible)
{
return super.onKeyUp(keyCode, event);
} else
{
//method which shows the close dialog and close the application
onBackPressed();
return true;
}
}
return super.onKeyUp(keyCode, event);
}
if soft keyboard is shown ,I don't think Activity can receive back key event. the default behaviour is back key make soft keyboard disappear.
View also can block key event, the simple way is setOnKeyListener. make sure that your View is focusable and when it receive back key event just return true .
Code to hide the softkey pad :
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
you can put this code onBackKeyPressed no matter keyboard is showing or not

How to prevent the soft keyboard from ever appearing in my Activity?

I'm writing an Android game that runs in fullscreen landscape mode, and has buttons placed at the bottom left and bottom right of the window. The problem is that one of these buttons is (on many phones) right next to the Menu button, so the player might accidentally press Menu instead.
If it is pressed briefly, I simply pause the game and show the in-game menu. No problem there.
But if the button is held down longer, Android opens up the soft keyboard on the bottom half of the screen. Since it gets in the way, and is completely useless in this Activity, I would like to disable it.
I tried the following approaches.
Via InputMethodManager
From: Hide soft keyboard on activity without any keyboard operations
Since I have only one view (a GLSurfaceView) I tried this in my Activity.onCreate():
InputMethodManager imm = ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE));
imm.hideSoftInputFromInputMethod(glSurfaceView.getApplicationWindowToken(), 0);
It doesn't work: the soft keyboard still comes up on Menu long-press.
Via the AndroidManifest.xml
From: How to stop the android soft keyboard from ever coming up in my entire application
I added this to my manifest:
<activity
android:windowSoftInputMode="stateAlwaysHidden"
>
Does a great deal of nothing as well.
So... is there even a way? How?
Here is, at least, a solution to my immediate problem. It shows the in-game menu, no matter how long the button was pressed.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
event.startTracking();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
// From the docs:
// "Note that in order to receive this callback, someone in the event [chain]
// must return true from onKeyDown(int, KeyEvent) and call startTracking() on the event."
if (keyCode == KeyEvent.KEYCODE_MENU) {
// Override default handling, and don't pop up the soft keyboard.
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
openOptionsMenu();
return true;
}
return super.onKeyUp(keyCode, event);
}
But it feels like a hack, so I'm hoping that someone comes up with a better solution.
But if the button is held down longer,
Android opens up the soft keyboard on
the bottom half of the screen.
What phone do you have? Are you sure? I've never once seen that happen and I just tried it and it doesn't work on my phone.
Also, that sounds like a user problem. Don't try to subvert the user. If the user REALLY wants to open a keyboard in your app, you should let them and if it's useless, they'll hit back and it will go away.
A more concerning issue should be that your buttons are so close to the menu buttons.
Try using hideSoftInputFromWindow() instead. According to the documentation:
request to hide the soft input window from the context of the window that is currently accepting input.
use android:windowSoftInputMode="adjustPan" in android manifest.
I think this is best choice to prevent the view goes up.

Categories

Resources