Reputation: 11164
So i've got an EditText which is disabled initially. When i press a button it enables it, and automatically opens the soft keyboard.
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(etToDelete, InputMethodManager.SHOW_FORCED);
After i enter some text in it, i press the EditText which should make it disable again and hide the opened keyboard.
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(((EditText)view).getWindowToken(), 0);
BUT, what it does is very quickly close and then reopens it. MY GUESS is when you normally press an EditText it opens the keboard, so in this case even though i close it, it reopens it because of that :(
What is the solution? I've tried a couple of methods by which i stop the keyboard from showing when an EditText is pressed, but i was unsuccessfull, if someone could offer me a concrete example of how this should be made, i'd be grateful.
Upvotes: 1
Views: 1010
Reputation: 12739
Every method for hiding keyboard when starting fragment didnt work for me, but this made it, so try it out maybe
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Upvotes: 1
Reputation: 14505
I have same problem I solved so:
first create a class KeyBoardManager:
import android.content.Context;
import android.os.Handler;
import android.view.inputmethod.InputMethodManager;
public class KeyBoardManager {
public KeyBoardManager(Context context) {
final Handler handler = new Handler();
final InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
new Thread(new Runnable() {
@Override
public void run() {
while(true){
try{Thread.sleep(100);}catch (Exception e) {}
handler.post(new Runnable() {
@Override
public void run() {
if(!imm.isAcceptingText()){
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
}
}
});
}
}
}).start();
}
}
and in method onCreate of first activity you create a new instance of KeyBoardManager like:
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new KeyBoardManager(this);
}
}
and when your edittext is draw in screen for the firs time you call:]
(new Handler()).postDelayed(new Runnable() {
editText.requestFocus();
editText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
editText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
}, 200);
Upvotes: 1