Question

I have a really strange issue. I have a pager with fragments. One of the fragments is the Messages fragment. In this fragment I have a FragmentTabHost with three tab-fragments. In the action bar I have a SearchView. When I am want to search for something and I start writing the query string, the focus of the search view is removed from it and is set to the first tab of the TabHost. That way I can't write the query string. This happens only when I am near the MessagesFragment. For example: if I am far enough (1 page), the search is working fine.

Can someone tell me what might the problem be?

Here is a picture: Android TabHost Issue

The MessagesFragment:

public class MessagesFragment extends Fragment {
private FragmentTabHost tabHost;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    this.tabHost = new FragmentTabHost(this.getActivity());
    this.tabHost.setup(this.getActivity(), this.getChildFragmentManager(), container.getId());

    TabSpec tabSpecForAllMessages = this.tabHost.newTabSpec("all").setIndicator("ALL");
    this.tabHost.addTab(tabSpecForAllMessages, AllMessagesFragment.class, null);

    TabSpec tabSpecForSentMessages = this.tabHost.newTabSpec("sent").setIndicator("SENT");
    this.tabHost.addTab(tabSpecForSentMessages, SentMessagesFragment.class, null);

    TabSpec tabSpecForReceivedMessages = this.tabHost.newTabSpec("received").setIndicator("RECEIVED");
    this.tabHost.addTab(tabSpecForReceivedMessages, ReceivedMessagesFragment.class, null);

    return this.tabHost;
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    this.tabHost = null;
}

}

The tab host layout:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </TabWidget>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>
</LinearLayout>

</TabHost>

The action bar menu-items:

<?xml version="1.0" encoding="utf-8"?>

<item
    android:id="@+id/menu_item_search"
    android:actionViewClass="android.widget.SearchView"
    android:icon="@drawable/ic_action_search"
    android:showAsAction="collapseActionView|ifRoom"
    android:title="@string/title_search" />

<item
    android:id="@+id/menu_item_home"
    android:icon="@drawable/ic_action_home"
    android:showAsAction="always"
    android:title="@string/title_home" />

The onCreateOptionsMenu event handler:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = this.getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =
            (SearchView) menu.findItem(R.id.menu_item_search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(this
            .getComponentName()));

    return true;
}
Was it helpful?

Solution

Try add method

@Override
public boolean onQueryTextChange(String text) {
    if(searchView!=null)
        searchView.requestFocusFromTouch();

    return true;
}

searchView must be init SearchView searchView;

My onCreateOptionsMenu method:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.only_search, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            searchView.requestFocusFromTouch();
            return true;
        }
    });
}

it`s worked for me!

OTHER TIPS

I am also faced the same problem and i resolved this by writting listener to the view like this..

searchView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            searchView.requestFocusFromTouch();
            return true;
        }
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top