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

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

Related

What key gets pressed to hide soft keyboard in 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?

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.

Disable SoftInput Keyboard from Closing OnBackPressed

Just wondering if it's possible to prevent the keyboard from closing when the back button is pressed.
AKA, jump to the previous activity on one tap of the back button.
You can override onBackPressed() so that if the keyboard is showing you just call finish() on your Activity:
#Override
public void onBackPressed()
{
boolean keyboardIsShowing = // determine if keyboard is showing somehow.
if (keyboardIsShowing )
{
finish();
}
else
{
super.onBackPressed();
}
}
I am not sure an exact way to know if a keyboard is showing, but this link can point you in the right way:
How to check visibility of software keyboard in Android?
On a side note, users probably don't expect the Activity to close when the back button is pressed, they probably expect the keyboard to close. I would carefully consider your use case before implementing something like this.

How to disable the cancel when a popupwindow shows

I created a popupwindow to show a menu. I found that if I press the cancel button on keyboard, the popupwindow will dismiss, and setFocusable() only disable the buttons like menu, but cancel button still works.
If there exists a method to make popupwindow invalid for popupwindow, or define the action myself when a cancel button is pressed? Thanks.
Well I mean back button when i say cancel button. And Thanks for sachy and other people who reply me.
By cancel button do you mean back button? If yes than u can simply override onKeyDown().
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.d("back", "back button pressed");
}
return true; //to prevent this event from being propagated further.
}
Please explain more to now exactly what you want to achieve. If you just want to disable the button you might try this.
Button button = (Button)findViewById(R.id.button1);
button.setEnabled(false);
Or you want to override the cancel button? Check this out then: Back button behavior

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

Categories

Resources