Ccna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Tuesday, April 17, 2012
Android EditText, soft keyboard show/hide event?
Is it possible to catch the event that Soft Keyboard was shown or hidden for EditText?
As far as my content view is a subclass of LinearLayout (could be any other view or view group), I'd overridden onMeasure method lilke following:
Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { final int proposedheight = MeasureSpec.getSize(heightMeasureSpec); final int actualHeight = getHeight();
if (actualHeight > proposedheight){ // Keyboard is shown } else { // Keyboard is hidden }
There actually isn't such an event to catch. The IME is simply showing and hiding its window; the feedback you get from this is the window manager causing your own window's content to resize if you have put it in resize mode.
EditText editText = (EditText) findViewById(R.id.myEdit); InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); // only will trigger it if no physical keyboard is open mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
try these methods: showSoftInput(View, int, ResultReceiver) and hideSoftInputFromWindow(IBinder, int, ResultReceiver). You can override onReceiveResult(int resultCode, Bundle resultData) method of ResultReceiver class to handle show/hide event.
Hi I'd used following workaround:
ReplyDeleteAs far as my content view is a subclass of LinearLayout (could be any other view or view group), I'd overridden onMeasure method lilke following:
Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);
final int actualHeight = getHeight();
if (actualHeight > proposedheight){
// Keyboard is shown
} else {
// Keyboard is hidden
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
This workaround helped me to hide some controls when keyboard is showing and bring back otherwise.
Hope this would be useful.
There actually isn't such an event to catch. The IME is simply showing and hiding its window; the feedback you get from this is the window manager causing your own window's content to resize if you have put it in resize mode.
ReplyDeleteWe can toggle the on-screen keyboard using the code below.
ReplyDeleteInputMethodManager inputMgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMgr.toggleSoftInput(0, 0);
To show Soft Keyboard for EditText:
EditText editText = (EditText) findViewById(R.id.myEdit);
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// only will trigger it if no physical keyboard is open
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
And to hide:
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
Refer this links: http://www.androidpeople.com/android-hide-virtual-keyboard-through-code/ , http://stackoverflow.com/questions/1109022/how-to-close-hide-the-android-soft-keyboard
try these methods: showSoftInput(View, int, ResultReceiver) and hideSoftInputFromWindow(IBinder, int, ResultReceiver). You can override onReceiveResult(int resultCode, Bundle resultData) method of ResultReceiver class to handle show/hide event.
ReplyDelete