3

I am displaying an EditText inside tabhost. Tabhost is inside a SherlockFragmentActivity.

Suppose I am in touch mode and I click on EditText to give it focus and start typing on it. After typing 2-3 chars, I decided to use my hard keyboard. As soon as press first hard key, I move out of touch mode and this also causes the focus to moves back to the currently selected tab. As long as I do not use hard navigation keys to bring back the focus to my EditText, I will not be able to type into EditText.

This issue is caused only when TabHost is not inside TabActivity.

A quick sample code to reproduce this problem could be - FragmentTabs activity of ActionBarSherlock sample code. Select CustomTab and try to type any text inside the search view in action bar using the hard keyboard. See focus simply moves to tab.

Problem is reproducible on android 2.2, 4.0 and using ICS emulator as well.

Does anyone have more info. about this issue?

(A workaround seems to be mentioned here: https://stackoverflow.com/a/8684025/333137 but it looks like a hack)

Thanks.

Edit: Upon more investigation, I found setup() function inside TabHost class which needs to be called only if TabHost is not used inside TabActivity. It registers a key listener which receives callback only when hard key is pressed.

void setup(){
       // KeyListener to attach to all tabs. Detects non-navigation keys
        // and relays them to the tab content.
        mTabKeyListener = new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_DPAD_CENTER:
                    case KeyEvent.KEYCODE_DPAD_LEFT:
                    case KeyEvent.KEYCODE_DPAD_RIGHT:
                    case KeyEvent.KEYCODE_DPAD_UP:
                    case KeyEvent.KEYCODE_DPAD_DOWN:
                    case KeyEvent.KEYCODE_ENTER:
                        return false;

                }
                **mTabContent.requestFocus(View.FOCUS_FORWARD);**
                return mTabContent.dispatchKeyEvent(event);
            }

I am not sure why it is calling requestFocus(View.FOCUS_FORWARD) but this does not set the focus to EditText inside it (which it should?). Also, in case of search view in action bar, it is totally outside tab.

Community
  • 1
  • 1
vivek.m
  • 3,213
  • 5
  • 33
  • 48
  • Please refer to this posts. http://stackoverflow.com/questions/6006518/hard-keyboard-fail-to-focus-edittext – Hank Jul 18 '12 at 18:22
  • Thanks true_cp, I have mentioned this post in my qn above. It seems like a hack and would not work in my case. – vivek.m Jul 19 '12 at 11:42

1 Answers1

15

Override the TabHost as below, it works for me.

public class TabHostExt extends TabHost {

    public TabHostExt(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TabHostExt(Context context) {
        super(context);
    }

    @Override
    public void onTouchModeChanged(boolean isInTouchMode) {
        // leave it empty here. It looks that when you use hard keyboard,
        // this method will be called and the focus will be token.
    }
}
Hank
  • 1,318
  • 10
  • 29
  • Hank - I know this post is old, but wanted to follow up since I was linked here for a very similar situation. I have a ViewPagerActivity inside a SherlockActivity, and I see the same behavior of the TabHost stealing Focus from an EditText. Did you override the TabHost function in the Activity or in the Fragment? Thanks - more info below. – mattdonders Jan 07 '13 at 14:25
  • As another note I tried to `getCurrentFocus()` inside of a `onFocusChangeListener()` on the EditText and I see the TabHost trying to steal the focus and there is a `NullPointerException` and in the log I see the `onTouchModeChanged()` trying to be performed on the TabHost, but extending the TabHost and Overriding the function does not seem to work. I have tried it in the Fragment & in the Activity. – mattdonders Jan 07 '13 at 14:25
  • 1
    @mattdonders, `onTouchModeChanged()` is TabHostExt method, I don't understand you question of "override in Activity or Fragment". My TabHostExt is used in Fragment. For the NPE, could you tell which object is null in your casem `mCurrentView` or `mTabWidget` – Hank Jan 08 '13 at 13:27
  • The focus problem sometimes happens also for a combination of `TabHost`, soft keyboard, HTML page with text fields opened in a `WebView`. This solution works. – Giulio Piancastelli May 08 '14 at 14:49