Set android focus on touch? - android

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

Related

Android Studio Soft Keyboard Close

I am using the soft keyboard in Android Studio in order to display user input on the screen. The program has a button, that when clicked, displays whatever it is that the person typed in. As soon as I click on the EditText field, the soft keyboard pops up and I type in the input. The only problem is that I dont know how to close it after I'm done. My Textfield, which displays the input, is at the bottom of the screen so I can't see it if the keyboard remains open. Shouldn't there be some type of button? Is there a way of solving this??? Thank you.
in the OnClickListener of the button, which you are using to display the text entered when clicked, you can add some thing which closes the keyboard when clicked.
Add this inside the onClickListener
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
so when you click the button, it hides the keyboard and displays the text entered.

Cancel on SearchView causing the soft keyboard to appear

Even if my SearchView is not in focus (ie. the user has already pressed the "Search" button earlier to submit their query), when I press the cancel (the X) button on my Android SearchView, the soft keyboard comes back up into view.
My thinking is that if the user doesn't already have the keyboard on the screen, then they just want to clear the filter/search box. If they want to clear the filter and type something different they can tap it again.
However, if they are typing into the box and make a mistake I would expect the keyboard to remain in view (because the search view already has focus).
So in a nutshell I want:
If the user is typing in the search view and taps cancel/clear, then the keyboard stays in focus.
If the user is not currently typing in the search view (ie. the keyboard has disappeared from view), then tapping the clear button should just clear the query and NOT bring the keyboard back into view.
I know I can use the setOnCloseListener() event to hook into when the clear/close button is pressed, but I don't know how to stop it from showing the soft keyboard as mentioned in point #2.
EDIT:
Maybe there is a way I can have the search view "lose focus"?
How do I achieve this result? Thanks.
You can lose focus by doing the following:
searchView.clearFocus();
You can also force hiding the keyboard on any event you want with the inputManager.
For example:
InputMethodManager inputManager = (InputMethodManager) this
.getSystemService(Context.INPUT_METHOD_SERVICE);
//check if no view has focus:
View v=this.getCurrentFocus();
if(v==null)
return;
inputManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

Android Keyboard : managing Keyboard appearance / disappearance inside fragments

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

Dismissing a DialogFragment when the soft keyboard is hidden

I have a simple DialogFragment that contains an EditText. When the DialogFragment is created the soft keyboard is shown immediately and the EditText gains immediate focus by using:
mEditText.requestFocus();
getDialog().getWindow().setSoftInputMode(
LayoutParams.SOFT_INPUT_STATE_VISIBLE);
In fact, what I have is essentially like the example given in this blog:
http://android-developers.blogspot.co.uk/2012/05/using-dialogfragments.html
When the back button is pressed, I wish for the DialogFragment to be dismissed. What actually happens is that the first back button press causes the soft keyboard to be hidden. A further back press is required to dismiss the DialogFragment.
I was quite surprised that there doesn't seem to be a simple API solution for this (such as setting a flag) as I'd have thought it'd be a common requirement.
Having searched on SO the best option seems to be to detect when the soft keyboard has been hidden, and then call dismiss() on that event. Such possible solutions for detecting the soft keyboard is hidden are:
EditText with soft keyboard and "Back" button
How to check visibility of software keyboard in Android?
Before I go ahead and use one of the above solutions, is there any other means I should consider dismiss of the entire DialogFragment and soft keyboard with one hit of the back button?
Why not using a cancel button instead of exploiting the back one?

Dismiss Soft Keyboard after Hardware Search is performed

I have an Activity which performs searching within my app. I have this Activity set as the handler for the hardware search button. This all works great.
If a user navigates to my Activity, enters a search query and then clicks on the "Search" button then I dismiss the soft keyboard via:
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(etQuery.getWindowToken(), 0);
etQuery is my EditText field.
However, if a user taps the hardware search button, then the search input overlap is shown, and the user is allowed to enter text, which they do, upon clicking "Go" my Activity gets the input and performs the search successfully. However, the soft keyboard is still visible.
Even though I do call the above keyboard dismissal code, it doesnt actually work. I assume because the window [token] that initiated the soft keyboard is NOT in fact the etQuery EditText - it was initiated by the Hardware Search facility. Thus asking it to close based on the EditTexts token has no effect.
At this point, I don't really care who opened the soft keyboard, I just want it to close.
How can I force the soft keyboard to close regardless of who opened it?
I solved this by adding the following attribute to the activity in the manifest:
android:windowSoftInputMode="stateHidden"

Categories

Resources