0

So, I have a EditText and I have a function that is hiding keyboard when you click outside EditText. What happens is next:

  1. First time EditText is selected, keyboard comes up and it pushes whole View up.
  2. I deselect EditText (click anywheere otuside of EditText) and keyboard goes to hidden
  3. When I click again on EditText, keyboard comes up, but View isn't pulled up and I cannot see my EditText

How to push that View up again when EditText is selected?

Here is code for hiding SoftKeyboard:

 @Override public boolean dispatchTouchEvent(MotionEvent ev) { View v = getCurrentFocus(); if (v != null && (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_MOVE) && v instanceof EditText && !v.getClass().getName().startsWith("android.webkit.")) { int scrcoords[] = new int[2]; v.getLocationOnScreen(scrcoords); float x = ev.getRawX() + v.getLeft() - scrcoords[0]; float y = ev.getRawY() + v.getTop() - scrcoords[1]; if (x < v.getLeft() || x > v.getRight() || y < v.getTop() || y > v.getBottom()) hideKeyboard(this); } return super.dispatchTouchEvent(ev); } 

and hideKeyboard() method

public static void hideKeyboard(Activity activity) { if (activity != null && activity.getWindow() != null && activity.getWindow().getDecorView() != null) { InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0); } } 
4
  • Try use the built-in methods .Here is the solution for you stackoverflow.com/questions/43042489/… Commented Mar 27, 2017 at 10:54
  • Already tried adjust_resize and the rest from that bundle.. Commented Mar 27, 2017 at 10:55
  • Have you tried these stackoverflow.com/a/23381583/4239410 Commented Mar 27, 2017 at 10:55
  • Yeah, tried everything from built in methods for the activity in manifest and in onCreate code. Commented Mar 27, 2017 at 10:57

2 Answers 2

2

use this method:

@Override public boolean onTouchEvent(MotionEvent event) { InputMethodManager imm = (InputMethodManager)getSystemService(Context. INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); return true; } 
Sign up to request clarification or add additional context in comments.

2 Comments

@Miljan It's worked, i have checked.can you try this.
EditText EditHello= (EditText) findViewById(R.id.edt_hello); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(EditHello, InputMethodManager.SHOW_IMPLICIT);
1

Found a solution...

I had to use dummy layout(in my case LinearLayout):

public static void hideKeyboard(Activity activity) { if (activity != null && activity.getWindow() != null && activity.getWindow().getDecorView() != null) { InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0); } activity.findViewById(R.id.dummy).requestFocus(); } 

Now, when I reselect my EditText, its working.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.