1

I have a Service (implementing TextWatcher) that creates a View at the top of the screen, which only contains an EditText field. This "top bar" is then displayed on top of all activities. At the moment, the EditText is displayed, and TextWatcher methods are called correctly if I set its content programmatically.

However the desired behaviour is that, when the EditText is selected, a softkeyboard should pop up from the bottom of the screen but this is not happening, i.e. no keyboard is displayed.

I can't figure out why it works with Activities but not with my Service. My gut feeling is that the keyboard wants to be displayed in the same view of the TextWatcher, not sure though.

Can anyone please help?

This is a snippet of my code:

public final class TopBarService extends Service implements TextWatcher, OnFocusChangeListener {
    protected LayoutInflater mInflater;
    protected WindowManager mWindowManager;
    protected View mTopEditBar;
    protected EditText mTopEditText;

    public void onCreate() {
        // Create top bar
        mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        mTopBar = mInflater.inflate(R.layout.top_bar, null);
        mTopEditText = (EditText) mTopBar.findViewById(R.id.top_edit_text);
        WindowManager.LayoutParams params = ...; // some "standard" params here
        mWindowManager.addView(mTopBar, params);

        mTopEditText.addTextChangedListener(this);
        mTopEditText.setOnFocusChangeListener(this);
        mTopEditText.setInputType(InputType.TYPE_CLASS_TEXT);
        mTopEditText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
    }

    // TextWatcher implementation
    public void afterTextChanged(Editable s) {
        //...
    }

    public void beforeTextChanged(Editable s) {
        //...
    }

    public void onTextChanged(Editable s) {
        //...
    }

    public void onFocusChange(View v, boolean hasFocus) {
        // see Edit below
    }

    ...
}

Where res/layout/top_bar.xml is:

<EditText
    android:id="@+id/top_edit_text"
    android:layout_centerVertical="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</EditText>

Edit: Little progress.

I've noticed that, by using the InputMethodManager, this is not active:

public void onFocusChange(View v, boolean hasFocus) {
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.restartInput(mTopEditText);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    imm.showSoftInput(mTopEditText, InputMethodManager.SHOW_IMPLICIT);

    Log.d(TAG, "active? " + imm.isActive()); // false
    Log.d(TAG, "activeOnView? " + imm.isActive(mTopEditText)); // false
    Log.v(TAG, "accepting text? " + imm.isAcceptingText()); // false
}
rippeltippel
  • 359
  • 5
  • 15
  • Have you checked same post over here like: http://stackoverflow.com/questions/2403632/android-show-soft-keyboard-automatically-when-focus-is-on-an-edittext – Balaji Khadake Jul 11 '11 at 15:15
  • Yes, but that's not helping: It refers to an AlertDialog, and I've tried that solution anyway. PS: I'm on Android 2.2 – rippeltippel Jul 11 '11 at 15:30

0 Answers0