0

The below TabHost, NoFocusStealTabHost, implements the OnAttachStateChangeListener interface to implement the tab-focus-steal workaround:

TabHost tabs steal focus when using Hardware Keyboard

This TabHost also implements the OnTabChangeListener interface to listen for tab change events, and calls RequestFocus() on the first focus-able view found.

Does RemoveOnTouchModeChangeListener() prevent RequestFocus() from working? Neither RequestFocus() or RequestFocusFromTouch() give the child EditText focus. It must be explicitly touched in order for (bluetooth) keyboard input to enter into it (which doesn't seem user friendly).

Layout

<Views.NoFocusStealTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    ...
</Views.NoFocusStealTabHost>

Snippet

LocalActivityManager lam = new LocalActivityManager(Activity, false);
_tabHost = (NoFocusStealTabHost)_thisView.FindViewById(Android.Resource.Id.TabHost);
lam.DispatchCreate(_bundle);
_tabHost.Setup(lam);

Custom TabHost

public class NoFocusStealTabHost : TabHost, 
    View.IOnAttachStateChangeListener,
    TabHost.IOnTabChangeListener
{
    public NoFocusStealTabHost(Context context, IAttributeSet attributes) : base(context, attributes)
    {
        // Prevent TabHost from stealing focus (legacy android bug)
        AddOnAttachStateChangeListener(this);
        SetOnTabChangedListener(this);
    }

    void View.IOnAttachStateChangeListener.OnViewDetachedFromWindow(View view) { }

    void View.IOnAttachStateChangeListener.OnViewAttachedToWindow(View view)
    {
        // Prevent TabHost from stealing focus (legacy android bug)
        ViewTreeObserver.RemoveOnTouchModeChangeListener(this);
    }

    void IOnTabChangeListener.OnTabChanged(string tabId)
    {
        IList<View> vFocusables = CurrentView.GetFocusables(FocusSearchDirection.Down);

        if (vFocusables != null && vFocusables.Count > 0)
        {
            //vFocusables[0].RequestFocus();
            vFocusables[0].RequestFocusFromTouch();
        }
    }
}
Community
  • 1
  • 1
samus
  • 6,102
  • 6
  • 31
  • 69

0 Answers0