Aimal Khan
Aimal Khan

Reputation: 1019

Cannot clear focus of Search View android while switching fragments

I am using a Layout having a Search View below the Toolbar, and right below the Search View i have placed a layout that contains a fragment and a FloatingActionButton.

enter image description here

The search view has an auto-complete functionality that suggests city name.

When i hit the search button, it submits the query, loses focus and hides the suggestions (Which is how it is supposed to work).

But when i change the Fragment (From ListView to MapView) by clicking the FloatingActionButton, the Search View again grabs focus and starts showing those suggestions i.e. city names.

enter image description here

I am using the following code to change the fragments and clear the focus

fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (flagFAB) {
                fab.setImageResource(android.R.drawable.ic_menu_sort_by_size);
                flagFAB = false;
                if (fragmentManager.findFragmentByTag("testFrag2") != null) {
                    Log.wtf(TAG, "if the fragment exists, show it.");
                    fragmentManager.beginTransaction().show(fragmentManager.findFragmentByTag("testFrag2")).commit();
                } else {
                    Log.wtf(TAG, "if the fragment does not exist, add it to fragment manager.");
                    fragmentManager.beginTransaction().add(R.id.tabListBM, testFrag2, "testFrag2").commit();
                }
                if (fragmentManager.findFragmentByTag("fragList") != null) {
                    Log.wtf(TAG, "if the other fragment is visible, hide it.");
                    fragmentManager.beginTransaction().hide(fragmentManager.findFragmentByTag("fragList")).commit();
                }
            } else {
                fab.setImageResource(android.R.drawable.ic_dialog_map);
                flagFAB = true;
                if (fragmentManager.findFragmentByTag("fragList") != null) {
                    //if the fragment exists, show it.
                    fragmentManager.beginTransaction().show(fragmentManager.findFragmentByTag("fragList")).commit();
                } else {
                    //if the fragment does not exist, add it to fragment manager.
                    fragmentManager.beginTransaction().add(R.id.tabListBM, listFrag, "fragList").commit();
                }
                if (fragmentManager.findFragmentByTag("testFrag2") != null) {
                    //if the other fragment is visible, hide it.
                    fragmentManager.beginTransaction().hide(fragmentManager.findFragmentByTag("testFrag2")).commit();
                }
            }
            clearFocusSearchView();

        }
    });

@Override
public void clearFocusSearchView() {

    if (title.contentEquals("city")) {
        Log.wtf(TAG, "CLEAR FOCUS MAP");
        searchView.requestFocus();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        searchView.clearFocus();
    }
}

What i need is that the Search View shouldn't have the focus until and unless i click on it again.

I am really stuck here, any sort of help would really be appreciated.Thank you.

Upvotes: 1

Views: 1181

Answers (1)

ABI
ABI

Reputation: 1556

To clear focus while switching fragments, you should clearFocus it in your fragment

override onCreate()

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

and onCreateOptionsMenu

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setIconified(false);
    searchView.setIconifiedByDefault(false);
    searchView.clearFocus();
    }
}

hope it helps!

Upvotes: 2

Related Questions