Reputation: 152
I have two EditText views on my page. I want to rid them of the focus when I am not clicking on them. I can't get it to work. I tried this. I set this method to each view except EditTextView But this gets rid of only the cursor and the keyboard The view is still highlighted. Is there any lighter way to do this. Any suggestions.
public static void hideKeyboard(Activity activity) {
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
EditText editText = (EditText) (activity.getCurrentFocus());
editText.setCursorVisible(false);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
Upvotes: 0
Views: 112
Reputation: 152
@AtifAbbAsi, thanks for the answer.This is how I fixed it.
rootView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(getCurrentFocus() instanceof EditText){
EditText editText = (EditText) getCurrentFocus();
hideKeyboard(LoginActivity.this);
editText.clearFocus();
}
return false;
}
});
this is the code i modified for hideKeyboard function
public void hideKeyboard(Activity activity) {
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
if(getCurrentFocus() instanceof EditText){
inputMethodManager.hideSoftInputFromWindow(
activity.getCurrentFocus().getWindowToken(), 0);
}
}
Upvotes: 1
Reputation: 6035
try removing focus, simply add touch listener it will detect intraction on screen and will remove focus.
rootView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_MOVE){
}else if(event.getAction()==MotionEvent.ACTION_UP){
}
editText.setFocusableInTouchMode(false);
editText.setFocusable(false);
editText.clearFocus();
return true;
}
});
Upvotes: 1