Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: EditText and Button; when click Button, unfocus EditText and hide soft keyboard?

Tags:

android

<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?

like image 878
Ricket Avatar asked Jun 24 '10 13:06

Ricket


People also ask

How do I hide soft keyboard when EditText is focused?

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.

How do I hide the soft keyboard on android after clicking outside EditText Kotlin?

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.

How do I prevent the soft keyboard from pushing my view up in fragment?

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.


1 Answers

You can hide the keyboard by calling this.

InputMethodManager imm = (InputMethodManager) getSystemService(
    INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
like image 117
Robby Pond Avatar answered Sep 24 '22 22:09

Robby Pond