I have an EditText in a custom xml layout which get loaded dynamically(setView) in an EditTextPreference. Everything works well. Now when the preference is clicked and the editPreference dialog shows up, so does the soft keyboard. I dont want the soft keyboard to show up by default!
This is what I have tried. Should have worked :(!
public class ReportBugPreference extends EditTextPreference {
#Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
super.onPrepareDialogBuilder(builder);
View viewBugReport = LayoutInflater.from(ctx).inflate(R.layout.preference_report_bug,null);
builder.setView(viewBugReport);
EditText edttxtBugDesc = (EditText) viewBugReport.findViewById(R.id.bug_description_edittext);
//edttxtBugDesc.clearFocus();
InputMethodManager inputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(edttxtBugDesc.getApplicationWindowToken(), 0);
}
}
Do this for your EditText to hide Soft-Keyboard
mEditText.requestFocus();
mEditText.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager keyboard = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(ettext.
getWindowToken(), 0);
}
},200);
i think more better way to do this is below Code :
mEditText.setInputType(InputType.TYPE_NULL);
mEditText.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
mEditText.setInputType(InputType.TYPE_CLASS_TEXT);
return false;
}
});
generic function that may help;
public static void hideSoftKeyboard (Context context, View view) {
try {
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
}
catch (Exception ex) {
Log.w(TAG, "hideSoftKeyboard->"+ex.toString());
}
}
Related
In my dialog fragment, I am able to show the keyboard using
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT STATE_VISIBLE);
but I am not able to hide it on dismiss.
I've tried
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
and
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
neither of which work.
I've also tried showing and hiding the keyboard using
InputMethodManager inputManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput(0, 0);
and
InputMethodManager inputManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
but these are not able to show or hide the keyboard.
public static class MyDialogFragment extends DialogFragment
{
#Nullable #Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.my_input_dialog, container, false);
}
#Override
public void onViewCreated(View v, Bundle savedInstanceState)
{
super.onViewCreated(v, savedInstanceState);
final EditText editText = (EditText)v.findViewById(R.id.input);
// this line below is able to show the keyboard
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Button add = (Button)v.findViewById(R.id.add_btn);
add.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// other code not shown
dismiss();
}
});
Button cancel = (Button)v.findViewById(R.id.cancel_btn);
cancelButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
dismiss();
}
});
}
#Override
public void onDismiss(DialogInterface dialog)
{
//this line below does NOT work, it does not hide the keyboard
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
super.onDismiss(dialog);
}
}
Note: I have read these stackoverflow posts and have tried the proposed solutions to no avail:
How to hide the onscreen keyboard when a DialogFragment is canceled by the setCanceledOnTouchOutside event
Close/hide the Android Soft Keyboard
The solution turned out to a combination of the following. To show the keyboard in a DialogFragment:
#Override
public void onResume()
{
super.onResume();
editText.post(new Runnable()
{
#Override
public void run()
{
editText.requestFocus();
InputMethodManager imm =
(InputMethodManager)editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null)
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
}
To hide it, use the solution above by #Shekhar
#Override
public void onDismiss(DialogInterface dialog)
{
InputMethodManager imm =
(InputMethodManager)editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive())
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
super.onDismiss(dialog);
}
Hiding keyboard in a View inside DialogFragment:
public static void hideKeyboardInAndroidFragment(View view){
final InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
For Hiding the Keyboard use this:
private void hideKeyboard() {
try {
InputMethodManager inputManager = (InputMethodManager) _activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(_activity.getCurrentFocus()
.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
} catch (Exception e) {
}
}
for hide soft keyboard, you can use this method:
public void hideSoftKeyboard() {
try {
View windowToken = getDialog().getWindow().getDecorView().getRootView();
InputMethodManager imm = (InputMethodManager) getDialog().getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow( windowToken.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
} catch (Exception ex) {
Log.e(ex);
}
}
Hide it in DialogFragment onDestroyView() method:
View view = getActivity().getCurrentFocus();
if (view == null) view = new View(activity);
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm == null) return;
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
maybe work.
I had extension for fragment, but didn't work with dialog fragment. This extension works for both (not tested much tho)
/**
* If no window token is found, keyboard is checked using reflection to know if keyboard visibility toggle is needed
*
* #param useReflection - whether to use reflection in case of no window token or not
*/
fun Fragment.hideKeyboard(context: Context = App.instance, useReflection: Boolean = true) {
val windowToken = view?.rootView?.windowToken
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
windowToken?.let {
imm.hideSoftInputFromWindow(windowToken, 0)
} ?: run {
if (useReflection) {
try {
if (getKeyboardHeight(imm) > 0) {
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS)
}
} catch (exception: Exception) {
Timber.e(exception)
}
}
}
}
fun getKeyboardHeight(imm: InputMethodManager): Int = InputMethodManager::class.java.getMethod("getInputMethodWindowVisibleHeight").invoke(imm) as Int
Edit: toggle opened keyboard if it was closed before, I use reflection to get keyboard's height, which is not best solution, but works
I found only one fully working approach if you want to show keyboard when dialog is shown and hide keyboard when dialog is dismissed
<style name="InputDialog" parent="ThemeOverlay.AppCompat.Dialog.Alert">
<item name="android:windowSoftInputMode">stateAlwaysVisible</item>
</style>
And then you should use the theme above inside your DialogFragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NORMAL, R.style.InputDialog)
}
Kotil Extension function for DialogFragment hide keyboard
use : hideKeyboard(view)
fun DialogFragment.hideKeyboard(view: View) {
val imm =view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
I have a problem with android´s keyboard. I have a fragment who has an EditText in the top of the screen and below has a nested fragment that is a QR scanner. When I click on the EditText to write something the keyboard doesn´t appear if I don´t do a long click to my device´s menu button. I am trying to show programatically when I focus the EditText but I´m not able. Here is my code, thank you in advance.
EditText caja_edicion =(EditText) getActivity().findViewById(R.id.textcodigo);
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(caja_edicion, InputMethodManager.SHOW_IMPLICIT);
try adding this:
caja_edicion.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(caja_edicion, InputMethodManager.SHOW_FORCED);
} else {
((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(caja_edicion.getWindowToken(), 0);
}
}
});
I have change to this because I am in fragment but it doesn´t work and I don´t know why:
final EditText caja_edicion =(EditText) getActivity().findViewById(R.id.textcodigo);
caja_edicion.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(caja_edicion, InputMethodManager.SHOW_FORCED);
} else {
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(caja_edicion.getWindowToken(), 0);
}
}
});
I am designing my own keyboard, so i dont want the default keyboard to appear.
I am able to achieve this, but the focus on selection is missing.
How can i have the focus but disable the Keyboard alone on EditText in Android?
add this listener for edittext:
edit_Text.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
hideSoftKeyboard();
}
}
});
and add this method to activity:
public static void hideSoftKeyboard() {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
}
I have an EditText that I want to control the keyboard. When the EditText has focus the keyboard should appear, then as soon as I click on any other view, I want the keyboard to disappear. I try the following code but it did work
mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
} else {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
}
}
});
Assuming your outermost layout is RelativeLayout(You can do the similar thing for others as well), you can do something like following:
private RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//....
layout = (RelativeLayout) findViewById(R.id.yourOutermostLayout);
onTapOutsideBehaviour(layout);
}
private void onTapOutsideBehaviour(View view) {
if(!(view instanceof EditText) || !(view instanceof Button)) {
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideSoftKeyboard(YourCurrentActivity.this);
return false;
}
});
}
}
\\Function to hide keyboard
private static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
In onTapOutsideBehaviour function here, other that your EditText and Button views, if user clicks anywhere else, it will hide the keyboard. If you have any complex custom layout, you can exclude other views on which if user clicks, it does not hide the keyboard.
It worked for me. Hope it helps you.
I have a EditText and button aligned to parent's bottom.
When I enter text in it and press the button to save data, the virtual keyboard does not disappear.
Can any one guide me how to hide the keyboard?
You might also want to define the imeOptions within the EditText. This way, the keyboard will go away once you press on Done:
<EditText
android:id="#+id/editText1"
android:inputType="text"
android:imeOptions="actionDone"/>
This should work.
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
Just make sure that this.getCurrentFocus() does not return null, which it would if nothing has focus.
mEtNumber.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// do something, e.g. set your TextView here via .setText()
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
return true;
}
return false;
}
});
and in xml
android:imeOptions="actionDone"
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
I did not see anyone using this method:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean focused) {
InputMethodManager keyboard = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
if (focused)
keyboard.showSoftInput(editText, 0);
else
keyboard.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
});
And then just request focus to the editText:
editText.requestFocus();
Solution included in the EditText action listenner:
public void onCreate(Bundle savedInstanceState) {
...
...
edittext = (EditText) findViewById(R.id.EditText01);
edittext.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(edittext.getApplicationWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
}
return false;
}
});
...
...
}
I found this because my EditText wasn't automatically getting dismissed on enter.
This was my original code.
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ( (actionId == EditorInfo.IME_ACTION_DONE) || ((event.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN ))) {
// Do stuff when user presses enter
return true;
}
return false;
}
});
I solved it by removing the line
return true;
after doing stuff when user presses enter.
Hope this helps someone.
Just write down these two lines of code where enter option will work.
InputMethodManager inputManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
Been struggling with this for the past days and found a solution that works really well. The soft keyboard is hidden when a touch is done anywhere outside the EditText.
Code posted here: hide default keyboard on click in android
you can create a singleton class for call easily like this:
public class KeyboardUtils {
private static KeyboardUtils instance;
private InputMethodManager inputMethodManager;
private KeyboardUtils() {
}
public static KeyboardUtils getInstance() {
if (instance == null)
instance = new KeyboardUtils();
return instance;
}
private InputMethodManager getInputMethodManager() {
if (inputMethodManager == null)
inputMethodManager = (InputMethodManager) Application.getInstance().getSystemService(Activity.INPUT_METHOD_SERVICE);
return inputMethodManager;
}
#SuppressWarnings("ConstantConditions")
public void hide(final Activity activity) {
new Handler().post(new Runnable() {
#Override
public void run() {
try {
getInputMethodManager().hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
});
}
}
so, after can call in the activity how the next form:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
KeyboardUtils.getInstance().hide(this);
}
}
You can see marked answer on top. But i used getDialog().getCurrentFocus() and working well. I post this answer cause i cant type "this" in my oncreatedialog.
So this is my answer. If you tried marked answer and not worked , you can simply try this:
InputMethodManager inputManager = (InputMethodManager) getActivity().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getDialog().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
I use this method to remove keyboard from edit text:
public static void hideKeyboard(Activity activity, IBinder binder) {
if (activity != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (binder != null && inputManager != null) {
inputManager.hideSoftInputFromWindow(binder, 0);//HIDE_NOT_ALWAYS
inputManager.showSoftInputFromInputMethod(binder, 0);
}
}
}
And this method to remove keyboard from activity (not work in some cases - for example, when edittext, to wich is binded keyboard, lost focus, it won't work. But for other situations, it works great, and you do not have to care about element that holds the keyboard).
public static void hideKeyboard(Activity activity) {
if (activity != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (activity.getCurrentFocus() != null && inputManager != null) {
inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
inputManager.showSoftInputFromInputMethod(activity.getCurrentFocus().getWindowToken(), 0);
}
}
}
int klavStat = 1; // for keyboard soft/hide button
int inType; // to remeber your default keybort Type
editor - is EditText field
/// metod for onclick button ///
public void keyboard(View view) {
if (klavStat == 1) {
klavStat = 0;
inType = editor.getInputType();
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
editor.setInputType(InputType.TYPE_NULL);
editor.setTextIsSelectable(true);
} else {
klavStat = 1;
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
editor.setInputType(inType);
}
}
If you have another EditText Field, you need to watch for focus change.
In my case, in order to hide the keyboard when pressing the "send button", I used the accepted answer but changed context to getApplication and added getWindow().
InputMethodManager inputManager = (InputMethodManager) getApplication().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
editText.setInputType(InputType.TYPE_NULL);