In my app i want to clear focus on back pressed, show something that was hidden, and hide the keyboard.
Problem : when the Keyboard is up, pressing the back button hides it but it doesn't trigger my onBackPressed event ! Meaning i need to go back twice.
First back action : keyboard goes down
Second back action : my code get's executed
This is the code that I want to execute when the back button is pressed, regardless of the keyboard visibility
override fun onBackPressed() {
val materialEditText2 = findViewById<AppCompatEditText>(R.id.material_edit_text_2)
materialEditText2.visibility = View.VISIBLE
binding.materialEditText.clearFocus()
}
I tried searching online for way too long, best thing I found is this https://code.luasoftware.com/tutorials/android/edittext-clear-focus-on-keyboard-hidden
I don't understand how to implement it
Related
I have a listener on the whole app for the back button press event in android to override the default behaviour of back button(exit the app).
Also in some component i have override it too if i want the back button to be disabled or do something else.
The problem is when i open a react-native-picker, the picker stays on the screen ignoring the back button press.
I want it to first close (on the first back button press) and only then do what the higher component ordered to do when back button is pressed.
so i can't override the the higher component back button handling
i thought maybe to add the picker as a screen in the navigator and instead of planting it as a component, navigate to it, so it will be added to the stack navigator.
then by default it will first pop it of the stack when pressing back.
how can i add the picker component as a screen to navigate to, or is there a better way to do that?
By default the back behavior collapses or closes the picker on Android (depending on the mode you have chosen). You can see a working example here.
Is there a way to set a listener on back pressed to close the keyboard ?
override the function onBackPressed() from inside your Activity
I'm doing an appication under phonegap (more specific in android). How can I disable the home, menu and back buttons in the screen, the idea is when i press any of the buttons, show a verification dialog, if the answer is correct, close the app. When i assign the function button doesn't show the dialog
For the home menu remove the onCreateOptionsMenu() function and also you wouldn't need the onOptionsItemSelected() function, so to hide the menu remove these two.
For the back buttons there are two of them:
The hard button in device that navigates in the app until it hits the main activity then pops out of the app, can be controlled by overriding this method onBackPressed()
And the one in the action bar that navigates all inside the app itself but never pops the user out of the app, can be set by setDisplayHomeAsUpEnabled() which takes a Boolean true to make it working, false to disable it.
I have an app that contains at some point a FragmentActivity. This FragmentActivity contains some Fragments (FragmentA, FragmentB, etc). On each of these fragments, there are one or more EditText.
I'm trying to handle the keyboard properly. I want to make the Keyboard appear or disappear whenever I want.
For Example, When the FragmentA is created, I want to open the keyboard on an EditText. Then, three options possible for the user :
He clicks on a button that goes to the next fragment
He clicks on the Action Send of the Keyboard(that does nothing (on purpose) but close the keyboard normally) and then he can click on the button from the view
He clicks on the back button of the phone. In this case, it closes the keyboard and then he can click on the button form the view.
My problem is, whenever the user closes the keyboard by clicking on the back button of the phone, Android thinks that he doesn't want to see the keyboard ever in the activity. So when the FragmentB is created, I can't programmatically show the keyboard (Using InputManager btw) on a EditText from this Fragment.
Then, a second problem is when i click on the edittext to get the focus, in Android 4.x, the keyboard shows again, no problems, but with Android 2.x, it's impossible to have the keyboard shown again even if the focus is on the edittext ! It's killing me.
It appears that once the user has explicitly close the keyboard with the back button in one activity (even a fragment activity), you cannot show it again.
Does anyone have a solution ? Maybe playing with the flags in InputManager ? I didn't get all of them and what they do.
Try this:
Show keyboard:
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
EditText view = getCurrentFocus();
inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_FORCED);
Hide keyboard:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Hope it helps ;)
I have this issue: in my app, when user taps on EditText bar, keyboard pops up. After that, it is impossible to get rid of keyboard. When back button is pressed, whole application just turn off.
How can I make sure, that when user taps on some other object (not EditText), keyboard will be removed? Or at least, how to make it possible to hide keyboard by tapping back button?
Thanks.
in xml for EditText this will make keyboard dismiss when press enter on keyboard
android:imeOptions="actionDone"
You can hide the keyboard simple by overriding onBackPressed in your Activity and using the following code:
InputMethodManager inputMethodManager = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(anyView.getWindowToken(), 0);
Note that anyView can be any view that is attached to your current window.
You can see it working in my app called Magic Annotator. See method hideSoftKeyboard()