Friday, March 23, 2012

Android: Do something after Soft Keyboard closes

Heya, I got this method from one of the stack overflow threads, but forgot to save the link. But I'm just posting the code here for reference as usual.

So this involves making a custom EditText and listener interface.

Interface code:

public interface ImeBackListener {
 public abstract void onImeBack(SearchEditText ctrl, String text);

Now apply this when making your custom EditText:

public class SearchEditText extends EditText{

 private ImeBackListener mOnImeBack;
 public SearchEditText(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub
 public boolean onKeyPreIme(int keycode, KeyEvent event) {
  if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP)
   if (mOnImeBack != null) mOnImeBack.onImeBack(this,  this.getText().toString());
  return super.dispatchKeyEvent(event);
 public void setOnImeBackListener(ImeBackListener listener) {
  mOnImeBack = listener;


So when you call this in the code (statically or by creating a new object), just call setOnImeBackListener (new ImeBackListener() { // your code });

"your code" would be whatever you want to change in the activity.

No comments:

Post a Comment