My problem is simple. How to Clear Focus of all the elements in a View that may have focus (like EditText
-or- TextView
whose textIsSelectable="true"
and the text is selected in it at occurrence of the clearFocus event).
Why am asking (in case I don't get a generic reply to above) :
I have few fragments that am accessing through Navigation View. My main goal is to lose focus of the elements in the fragment on Drawer open in Navigation View. I know its possible to get onDrawerSlide
method for the NavigationDrawer and have infact set it up in my MainActivity so that on drawer open, if my EditText in one of the Fragments is open, I call hideSoftKeyboard code so that it closes the SoftKeyboard.
My Code to get the event on Opening of Drawer: (in onCreate() method of MainActivity)
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
{
//Hide SoftKeyboard on Navigation Drawer Open
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
//Hide SoftKeyboard if it is open
hideSoftKeyboard();
//And Make any view having focus loose focus
//...
//ClearFocus for the whole view is done from here (for whichever Fragment is visible right now)
super.onDrawerSlide(drawerView, slideOffset);
}
};
public void hideSoftKeyboard() //Declared outside onCreate
{
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
Here, my hideSoftKeyboard() code is generic and can close SoftKeyboard no matter what Fragment is visible at the moment from the MainActivity itself. Similarly I want to clearFocus of all my EditTexts and TextView (since Copy/Paste option shows up in middle of the drawer when it is open).
I know I can write code for each Fragment calling onDrawerSlide function and clearing Focus for each element individually, but I want to know if it is possible for a generic solution and if so, how to do it.
(If it is possible, there might be many implications of this and may help in a lot many cases)