Intercept Back Button when closing keyboard and disable blinking cursor - android

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()
}
})
}

Related

How to know when the user is done with text entry on an EditText control?

I want to be able to know when a user is done entering text in an EditText control. I'm thinking maybe it's best to know when the keyboard is closed or something similar. This is using Kotlin on Android app. I'm not sure why it's so hard to find basic answers like this. Maybe I'm searching with the wrong question (new to Android dev).
Using keyboard close as an indicator that the user finished entering text is a bad idea (the user might open the keyboard again to enter more text). A better solution would be to explicitly require for the user to indicate that he has finished entering the data.
You could use a "submit" button.
You can also set the android:imeOptions of EditText to actionDone and set a listener on the EditText.
editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
#Override
public boolean onEditorAction(EditText v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
//do your stuff
return true;
}
return false;
}
});
Update - Assuming keyboard close as the indicator is bad for a couple of reasons,
There is no 'proper' way to monitor the soft keyboard. You could try listening to the focus of the EditText or you could use the height difference to guesstimate whether the soft keyboard is open or closed(the option used in most keyboard listener libraries). But these aren't reliable and might break in production.
It's an 'unexpected' application behavior for the user. For example, the keyboard can be removed by pressing the back button. In general, the user would expect that the action would not proceed if the back button is pressed. But if you listen to keyboard close, then it would end up resulting in poorer UX.
There are no actual reasons why you would want to use keyboard close as the trigger. If you want to perform the action as the user types, then you should use TextWatcher, otherwise stick to explicit confirm options.
use onFocusChangeListener to know if the user has finished to add text and has leave the textInput focus.
Example
editText?.onFocusChangeListener =
View.OnFocusChangeListener { _,
hasFocus ->
if (!hasFocus) {
// code to execute when EditText loses focus
}
}

Is there any way to listen to the keyboard back button using ionic?

I need to listen to the back button to run a certain function but the usual platform.registerBackButtonAction() won't work when I've got an open keyboard.
It doesn't work because it's not a back button event when the keyboard is open, you'll need the Keyboard plugin and use the onKeyboardHide() subscribing to the event, so when you press the button to hide the keyboard a event is fired.
import { Keyboard } from '#ionic-native/keyboard'; // import it
construtor(public keyboard: Keyboard){
keybard.onKeyboardHide().subscribe(event =>{
// DO YOUR STUFF
});
}
Hope this helps.
In my case, keyboard events are not getting fired.
Need help on how to handle physical back button click when the keypad is open

Override back button on xamarin android app

I am building an application using Xamarin and it's Android Player for Android. Whenever I hit the back button it seems to back out of the application completely. I want to be able to change this default to go back to the previous page in my app. How do I override the back button behavior?
So the previous answer is correct, you can trap the hardware back button and do whatever you want with it. But I want to make sure you understand why this is happening. Android handles the hardware back button for you, and most of the time, letting Android handle it will work, it knows what to do.
But in your case, you're not actually navigating at all. Your click handler is removing one layout file and replacing it with another. This is the equivalent of showing/hiding a div in web development. You're not actually changing the screen(page).
Because of this, when you hit the back button, you're still on the first (and only) screen in your app, so the OS does the only thing it knows to do, it closes the app.
If you want to continue with the paradigm of showing/hiding layouts in lieu of actually navigating, I would trap the hardware back button and re-swap your layout files.
public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
{
if (keyCode == Keycode.Back) {
SetContentView(Resource.Layout.Login)
return false;
}
return true;
}
But the true solution that I would recommend would be to read up on how to truly navigate in Xamarin Android. Swapping your layout files and putting all the logic for your entire app in one Activity will be very hard to maintain.
You can capture the OnKeyDown and decide whether to allow the Back button to ripple up the event chain or not:
public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
{
if (keyCode == Keycode.Back) {
Toast.MakeText (this, "Back button blocked", ToastLength.Short).Show ();
return false;
}
Toast.MakeText (this, "Button press allowed", ToastLength.Short).Show ();
return true;
}
you can handle that in the OnBackPressed() event.

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.

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

Categories

Resources