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.
Related
I have a fragment with EditText used as search bar. What I wanna I do is when I press the back button to close the keyboard, which is opened when clicked on the EditText, is to disable the blinking cursor. For some reason when I'm handling the code below, the cursor still remains visible.
editText.setOnKeyListener{ _, keyCode, event ->
if(keyCode == KeyEvent.KEYCODE_BACK) {
editText.isCursorVisible = false
true
} else {
false
}
}
I've tried different methods, but none of them gave me the desired result. Am I missing something or is there another method for handling this?
Send the focus to the other component in the page.
It should be helpful
Or remove focus from your edit text by this code
edittext.clearFocus();
Are you actually trying to clear focus when back button is pressed? From the provided code it looks like you are trying to hide it when back is pressed on keyboard only.
Yes but this implementation is not okay. You should catch the actually backpress event not like you are doing it here. What I think happens here is that the first time you click the back button keyboard hides and you dont receive the back key even. Now if you would press the back button again it would actually hide the cursor. Also the provided code disables the back button, it will only work if the keyboard is opened, otherwise it will just clear the focus and do nothing else
I think something like this would cover your case:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activity?.onBackPressedDispatcher?.addCallback(this, object : OnBackPressedCallback(false) {
override fun handleOnBackPressed() {
editText.clearFocus()
}
})
}
I have an activity with two fragments, each fragment has a back button to the previous activity/fragment. Both the back buttons on the fragments work properly. However when i run the app on my android phone and i use the built-in back button to navigate to the previous activity, it displays a blank activity and then when i press the built-in back button again only then does it navigate to the previous activity. The problem is clearly the back burton than built in.Is there a way to solve this???
There is a method in the Activity called onBackPressed() which is called when the device back button is pressed. If you want to control what happens on back press just override it. To remove default onBackPressed action you need to remove the call to super.onBackPressed() and then you control what happens when back button is pressed.
#Override
public void onBackPressed() {
//super.onBackPressed();
// do something here
// or perhaps nothing at all
}
I have an app that uses the keyboard, when the keyboard is up if the user presses back button the keyboard dissappears, however I would like to change one or two other things while it changes, so I need an event listener.
I tried
#Override
public void onBackPressed()
However this doesnt seem to catch the backbutton while the keyboard is up, if the user presses back twice, then this catches the second click only
Register this on your root view to detect keyboard appearance/disappearance :
root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
public void onGlobalLayout(){
int heightDiff = root.getRootView().getHeight()- root.getHeight();
/*If height difference is more then 150, consider keyboard as visible. -150 for disappearing */
}
});
Also, make sure to use adjustResize for 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;
}
I navigate from Activity1 to Activity2
On Activity 2 I have a keyboard and this keyboard stays on the screen after selecting the back button and going to Activity 1.
This is how I fixed this issue
// This code is in Activity 2
#Override
public void onBackPressed() {
startActivity(intentForActivity1);
finish();
}
Is this a wrong solution to my problem?
Is it a bad idea to handle the back button manually?
Since you're capturing the back button press, most probably the soft keyboard does not receive the press and thus it does not hide.
Try hiding it yourself:
#Override
public void onBackPressed() {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
startActivity(intentForActivity1);
finish();
}
See Reto Meier's answer for more details on this method to hide the keyboard: Close/hide the Android Soft Keyboard
There's nothing inherently wrong with overriding the back button. Just make sure the behavior isn't confusing to the user.
Also, if you ever just want to hide the soft keyboard (say, you're switching between tabs or something like that), you can use InputMethodManager. Here's a thread where people discussed ways to do this.