Unable to hide soft keyboard after enter text in edit text - android

I have an layout with 5 Edit texts and 3 radio buttons and two button..After 5 edit texts I have 3 radio buttons and after that 2 buttons. After enter the text in fifth edit text I am unable to see radio buttons and normal buttons due to soft keyboard. How can I disable that soft keyboard after enter the fifth edit text? Can any one please help me out of this problem...

Try the following code snippet to hide/close the soft keyboard
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
);

public void hideKeyBord(View view) {
if (view != null) {
if (keyBoardHide == null) {
keyBoardHide = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
}
if (keyBoardHide != null && keyBoardHide.isActive()) {
// to hide keyboard
keyBoardHide.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
}

InputMethodManager inputManager = (InputMethodManager)
getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

Simply use :
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
whenever you want to hide the soft keyboard.
In your case :
Let your fifth EditText be et..
Then use :
if(!et.toString().equals(null)){
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
OR
You can detect if the fifth EditText was focused or not and then acting accordingly(i.e Hiding the keyboard using the method above if the EditText was focused) by following the link below:
How can I detect focused EditText in android?
OR
Detect Done key event on soft keyboard :
The keyboard would automatically close when DONE button is pressed. But if you want to perform customs actions when DONE button is pressed, refer below :
et= (EditText) findViewById(R.id.edit_text);
et.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// do your stuff here
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
return false;
}
});
Hope this Helps!

Hiding keyboard is not a big question ,but u need to confirm "when ?"
you need to call
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edittext.getApplicationWindowToken(), 0);
for hiding softkeyboard
try to add android:imeOptions="actionNext" in your edittext xml file and
edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
boolean flag= false;
if (i == EditorInfo.IME_ACTION_NEXT) {
flag= true;
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edittext.getApplicationWindowToken(), 0);
}
return flag;
}
});
this code will hide your soft keyboard on click next in keyboard

Related

Keep android sof keyboard shown

I added "Go" button in the soft keyboard and any time that i am pressing it keyboard is hiding. How to keep it shown? Using this code to show it does not work. Thanks
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId== EditorInfo.IME_ACTION_GO){
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}
return false;
}
I have edited the answer for u, if u miss setSingleLine(true) the whole stuff will not work..I thought u have added,but u may not have been Try this bro:
et1.setHint("testing");
et1.setImeActionLabel("Go", EditorInfo.IME_ACTION_GO);
et1.setSingleLine(true);
et1.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId== EditorInfo.IME_ACTION_GO){
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(et1, InputMethodManager.SHOW_FORCED);
return true;
}
else
return false;
}
} );
If you want your keyboard to be shown on the start of your activity you can add below line to AndroidManifest.xml inside your activity tag :
android:windowSoftInputMode="stateVisible"
and to resolve your 'GO' button problem you can use following code:
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
//imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); //to hide
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED); //to show
You can use this on any event appropriate to your task like TextWatcher's onTextChanged , editor listeners.

Android- Force visible soft keyboard

i've an edittext that when I click on enter (not button) , the soft keyboard disappears. How can i do to close the soft keyboard only with BACK button, and not with other events?
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)
The following code may show the keyboard programmatically. Just catch the KeyEvent as you are doing in your question code and place this code inside that function:
yourEditText.postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
InputMethodManager keyboard = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(mUserNameEdit, 0);
}
},0);
I do not recommend using InputMethodManager.SHOW_FORCED because it can cause strange layout issues. I would rather use this snippet in that case:
InputMethodManager mgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
Put this code in the onCreate() of your activity.
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

force soft keyboard to show when EditText gets focus [duplicate]

This question already has answers here:
How to show soft-keyboard when edittext is focused
(48 answers)
Closed 9 years ago.
I have an EditText that I am passing focus to programmatically. But when I do, I want the keyboard to show up as well (and then go down when that EditText lose focus). Right now, the user has to click on the EditText to get the keyboard to show up -- even thought the EditText already has focus.
<activity android:name=".YourActivity"
android:windowSoftInputMode="stateVisible" />
Add this to manifest file...
This is how I show the ketyboard:
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
set this for your activity in your manifest to pop keyboard automatically when your screen comes containing EditText box
<activity android:windowSoftInputMode="stateAlwaysVisible" ... />
To hide keyboard on losing focus set a OnFocusChangeListener for the EditText .
In the onCreate()
EditText editText = (EditText) findViewById(R.id.textbox);
OnFocusChangeListener ofcListener = new MyFocusChangeListener();
editText.setOnFocusChangeListener(ofcListener);
Add this class
private class MyFocusChangeListener implements OnFocusChangeListener {
public void onFocusChange(View v, boolean hasFocus){
if(v.getId() == R.id.textbox && !hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
}
To show the keyboard, use the following code.
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
To hide the keyboard,, use the code below.
et is the reference to the EditText
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
In order to do it based on focus listener you should go for:
final InputMethodManager imm =(InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
}else{
imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
}
imm.toggleSoftInput(0, 0);
}
});
Hope this helps.
Regards!

hide android softkeyboard when one key on softkeyboard press

I would like to hide my softkeyboard when one key on softkeyboard press.
After review, I found only ways to hide softkeybord after some event(eg. when use is on edit text)
hi here is the detail
To show soft keyboard
EditText editText = (EditText) findViewById(R.id.myEdit);
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// only will trigger it if no physical keyboard is open
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
To Hide soft keyboard
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
and overriding onKeydownEvent hide the soft keyboard
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//hide the soft keyboard
return super.onKeyDown(keyCode, event);
}
This should work:
public class KeyBoard {
public static void toggle(Activity activity){
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm.isActive()){
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); // hide
} else {
imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY); // show
}
}//end method
}//end class
KeyBoard.toggle(activity);

Close virtual keyboard on button press

I have an Activity with an EditText, a button and a ListView. The purpose is to type a search screen in the EditText, press the button and have the search results populate this list.
This is all working perfectly, but the virtual keyboard is behaving strange.
If I click the EditText, I get the virtual keyboard. If I click the "Done" button on the virtual keyboard, it goes away. However, if I click my search button before clicking "Done" on the virtual keyboard, the virtual keyboard stays and I can't get rid of it. Clicking the "Done" button does not close the keyboard. It changes the "Done" button from "Done" to an arrow and remains visible.
Thanks for your help
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
I put this right after the onClick(View v) event.
You need to import android.view.inputmethod.InputMethodManager;
The keyboard hides when you click the button.
mMyTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
// hide virtual keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(m_txtSearchText.getWindowToken(),
InputMethodManager.RESULT_UNCHANGED_SHOWN);
return true;
}
return false;
}
});
Use Below Code
your_button_id.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
}
}
});
You should implement OnEditorActionListener for your EditView
public void performClickOnDone(EditView editView, final View button){
textView.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(EditView v, int actionId, KeyEvent event) {
hideKeyboard();
button.requestFocus();
button.performClick();
return true;
}
});
And you hide keyboard by:
public void hideKeybord(View view) {
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),
InputMethodManager.RESULT_UNCHANGED_SHOWN);
}
You should also fire keyboard hiding in your button using onClickListener
Now clicking 'Done' on virtual keyboard and button will do the same - hide keyboard and perform click action.
For Activity,
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
For Fragments, use getActivity()
getActivity().getSystemService();
getActivity().getCurrentFocus();
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
In your case, as you only have one EditText, the bellow solution is the best one, I believe. If you had more than one EditText component, then either you would need to focus on the EditText that you wanted to close, or call the bellow function for every EditText. For you though, this works brilliantly:
editText.onEditorAction(EditorInfo.IME_ACTION_DONE);
Basically, the EditText already has a function that is intended to deal with what to do after the keyboard has been used. We just pass that we want to close the keyboard.
Add the following code inside your button click event:
InputMethodManager inputManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
This solution works perfect for me:
private void showKeyboard(EditText editText) {
editText.requestFocus();
editText.setFocusableInTouchMode(true);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.RESULT_UNCHANGED_SHOWN);
editText.setSelection(editText.getText().length());
}
private void closeKeyboard() {
InputMethodManager inputManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
}
Try this...
For Showing keyboard
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
For Hide keyboard
InputMethodManager inputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm =(InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);enter code here}
Kotlin example:
val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
from Fragment:
inputMethodManager.hideSoftInputFromWindow(activity?.currentFocus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
from Activity:
inputMethodManager.hideSoftInputFromWindow(currentFocus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
You use this code in your button click event
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Crash Null Point Exception Fix:
I had a case where the keyboard might not open when the user clicks the button. You have to write an if statement to check that getCurrentFocus() isn't a null:
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(getCurrentFocus() != null) {
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
If you set android:singleLine="true", automatically the button hides the keyboard¡

Categories

Resources