Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input from Hardware Keyboard Loses Focus

I have a fragment within a TabHost that has multiple text fields in it. The virtual keyboard works just fine to enter text into using inputType set, but the hardware keyboard (on Droid, Droid 2, etc) does not work.

From my testing as soon as you start typing on the hardware keyboard, the EditText loses focus and the "typing" seems to go elsewhere in the application. I have tried both configurations below:

<EditText
     android:id="@+id/editTextPlusFat"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_weight="0.15"
     android:background="@drawable/textfield_default_holo_light"
     android:digits="0123456789."
     android:ems="10"
     android:hint="@string/str_CalcHintFat"
     android:inputType="number" >

AND

<EditText
     android:id="@+id/editTextPlusFat"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_weight="0.15"
     android:background="@drawable/textfield_default_holo_light"
     android:ems="10"
     android:hint="@string/str_CalcHintFat"
     android:inputType="numberDecimal" >

Does anyone have any ideas why this happens? Thank you.

like image 922
mattdonders Avatar asked Jan 01 '13 18:01

mattdonders


2 Answers

My solution was to add onTouchListener() to all EditTexts in each Fragment - see below.

OnTouchListener foucsHandler = new OnTouchListener() {
    @Override
    public boolean onTouch(View arg0, MotionEvent event) {
        // TODO Auto-generated method stub
        arg0.requestFocusFromTouch();
            return false;
    }
};

currentActivity.findViewById(R.id.editTextPlusServings).setOnTouchListener(foucsHandler);
currentActivity.findViewById(R.id.editTextPlusFoodName).setOnTouchListener(foucsHandler);
like image 171
mattdonders Avatar answered Nov 18 '22 13:11

mattdonders


As in the duplicate question, the better answer is to remove the focus switching by overriding onTouchModeChanged() from TabHost.

Add a new class extending TabHost:

package net.lp.collectionista.ui.views;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TabHost;

public class BugFixedTabHost extends TabHost {

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

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

    @Override
    public void onTouchModeChanged(boolean isInTouchMode) {
        // leave it empty here. It looks that when you use hard keyboard,
        // this method would have be called and the focus will be taken.
    }
}

In your Fragment (or Activity) replace the TabHost type with BugFixedTabHost.

Finally, assuming you use TabHost in layout xmls too, change it to your custom view (full package name):

<net.lp.collectionista.ui.views.BugFixedTabHost
    android:id="@android:id/tabhost" ...

I'm not sure why this did not work for @mattdonders, but this is the right way to go. And it is cheaper than attaching listeners to every EditText. By the way, have we figured out yet why mCurrentView.hasFocus() is False or so?

like image 40
pjv Avatar answered Nov 18 '22 13:11

pjv