diff --git a/mobile/android/base/CustomEditText.java b/mobile/android/base/CustomEditText.java index 8a351dec320..55998cecdba 100644 --- a/mobile/android/base/CustomEditText.java +++ b/mobile/android/base/CustomEditText.java @@ -12,15 +12,19 @@ import android.view.View; import android.widget.EditText; public class CustomEditText extends EditText { + public CustomEditText(Context context, AttributeSet attrs) { + super(context, attrs); + mOnKeyPreImeListener = null; + } + OnKeyPreImeListener mOnKeyPreImeListener; public interface OnKeyPreImeListener { public boolean onKeyPreIme(View v, int keyCode, KeyEvent event); } - public CustomEditText(Context context, AttributeSet attrs) { - super(context, attrs); - mOnKeyPreImeListener = null; + public void setOnKeyPreImeListener(OnKeyPreImeListener listener) { + mOnKeyPreImeListener = listener; } @Override @@ -31,7 +35,20 @@ public class CustomEditText extends EditText { return false; } - public void setOnKeyPreImeListener(OnKeyPreImeListener listener) { - mOnKeyPreImeListener = listener; + public void setOnWindowFocusChangeListener(OnWindowFocusChangeListener listener) { + mOnWindowFocusChangeListener = listener; + } + + OnWindowFocusChangeListener mOnWindowFocusChangeListener; + + public interface OnWindowFocusChangeListener { + public void onWindowFocusChanged(boolean hasFocus); + } + + @Override + public void onWindowFocusChanged(boolean hasFocus) { + super.onWindowFocusChanged(hasFocus); + if (mOnWindowFocusChangeListener != null) + mOnWindowFocusChangeListener.onWindowFocusChanged(hasFocus); } } diff --git a/mobile/android/base/FindInPageBar.java b/mobile/android/base/FindInPageBar.java index b9f8798415c..7a0024a8e35 100644 --- a/mobile/android/base/FindInPageBar.java +++ b/mobile/android/base/FindInPageBar.java @@ -13,6 +13,7 @@ import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.view.inputmethod.InputMethodManager; import android.widget.RelativeLayout; import android.widget.RelativeLayout.LayoutParams; @@ -56,15 +57,36 @@ public class FindInPageBar extends RelativeLayout implements TextWatcher, View.O if (!mInflated) inflateContent(); - setVisibility(VISIBLE); + setVisibility(VISIBLE); mFindText.requestFocus(); + + // Show the virtual keyboard. + if (mFindText.hasWindowFocus()) { + getInputMethodManager(mFindText).showSoftInput(mFindText, 0); + } else { + // showSoftInput won't work until after the window is focused. + mFindText.setOnWindowFocusChangeListener(new CustomEditText.OnWindowFocusChangeListener() { + public void onWindowFocusChanged(boolean hasFocus) { + if (!hasFocus) + return; + mFindText.setOnWindowFocusChangeListener(null); + getInputMethodManager(mFindText).showSoftInput(mFindText, 0); + } + }); + } } public void hide() { - setVisibility(GONE); + setVisibility(GONE); + getInputMethodManager(mFindText).hideSoftInputFromWindow(mFindText.getWindowToken(), 0); GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("FindInPage:Closed", null)); } + private InputMethodManager getInputMethodManager(View view) { + Context context = view.getContext(); + return (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); + } + // TextWatcher implementation public void afterTextChanged(Editable s) {