<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:id="@+id/name" android:singleLine="true"
android:maxLength="12" android:capitalize="none" android:inputType="text" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/search_button"
android:text="Search" />
I have this at the top of my application. When the application starts, the EditText is orange highlighted and has a cursor in it; when the EditText is tapped, the soft keyboard pops up. The user uses it to type into the EditText.
However, when they click the Button, my onClick method fires and does everything it's supposed to, however the soft keyboard stays on screen and the EditText is still highlighted with its cursor.
I also have, at the top of the Button onclick:
findViewById(R.id.name).clearFocus();
In spite of this, the EditText does not seem to clear its focus. How do I make the button actually act as if it is submitting the form?
Also, I do not transition to a different Activity on the click of the Button. I suppose that is the typical case, and probably the reason why they don't bother hiding the keyboard. However I want to keep the search box and button at the top of the screen, so I just dynamically fill and add views to the screen when the Button is pressed.
How can I achieve my desired behavior?
Hiding the Soft Keyboard Programmatically You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your edit field. This will force the keyboard to be hidden in all situations.
This example demonstrates how to hide a soft keyboard on android after clicking outside EditText using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
Setting android:isScrollContainer = "false" inside the ScrollView worked for me. According to the documentation, settings "isScrollContainer" to true means that the scroll view can be resized to shrink its overall window so that there is space for an input method.
You can hide the keyboard by calling this.
InputMethodManager imm = (InputMethodManager) getSystemService(
INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With