0

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); } 
2
  • So the purpose is hidding softkeyboard? or you just want to rid out of focus this Commented Apr 11, 2020 at 17:50
  • I want that whenever i click the empty space on the screen, the cursor should go, the color of the underline color should return to default and the keyboard should be invisible. I am able to achieve first and last but can't figure out the middle one Commented Apr 11, 2020 at 18:06

2 Answers 2

1

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; } }); 
Sign up to request clarification or add additional context in comments.

2 Comments

Adding this does help me get the desired output, but i am confused about the utility of the if else if part as they are doing nothing. Also please explain the reason for return true at the end
action move means when you click on screen and action up means when you lift your finger up. and there is one more action down you can add logs to.it and check which one will help you.
1

@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); } } 

3 Comments

you can add true and false depends on you want to consume this click or not.
can you point me to a link or explain it yourself what consuming a click means?
touch listener is set to rootview now. so true means we want this click . false means pass it to other view. if you are still confused you can check android offical documentation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.