Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when click on editText show the android keyboard

Tags:

android

How to show and hide keyboard in android.For example when i click on the editText keyboard should display and when i click outside of edittext keyboard should get hidden.

like image 828
user671005 Avatar asked Mar 31 '11 05:03

user671005


People also ask

How do I show soft keyboard when EditText is focused?

android:windowSoftInputMode="stateAlwaysVisible" -> in manifest File. edittext. requestFocus(); -> in code. This will open soft keyboard on which edit-text has request focus as activity appears.

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

Ok everyone knows that to hide a keyboard you need to implement: InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm. hideSoftInputFromWindow(getCurrentFocus(). getWindowToken(), 0);


2 Answers

just try it..

     InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);

for hide keyboard

     imm.hideSoftInputFromWindow(ed.getWindowToken(), 0);  

for show keyboard

     imm.showSoftInput(ed, 0);

where ed is EditText..

like image 162
Niranj Patel Avatar answered Oct 21 '22 06:10

Niranj Patel


I've been using showSoftInput for a while but I've also found this alternative which simulates click on the EditText by using dispatchTouchEvent:

            ed.requestFocus();
            ed.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0));
            ed.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 0, 0, 0));

Didn't notice any difference though...

like image 36
vladexologija Avatar answered Oct 21 '22 07:10

vladexologija