I have a EditText control.
If I tap it the softkeyboard will popup however when I press "enter/ok/return" then the EditText control it still has focus and the keyboard up.
How do I close the softkeyboard and remove focus from it?
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editTextField.getWindowToken(), 0);
In the layout XML file, specify an imeOption on your EditText:
android:imeOptions="actionGo"
Next, add an action listener to your EditText in the Activity's java file
mYourEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_GO) {
// hide virtual keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mYourEditText.getWindowToken(), 0);
return true;
}
return false;
}
});
Where mYourEditText is an EditText object
Make sure your EditText XML has :
android:id="#+id/myEditText"
android:imeOptions="actionDone"
Then set listener to your EditText (with Kotlin, and from a fragment):
myEditText.setOnEditorActionListener({ v, actionId, event ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
myEditText.clearFocus()
val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view!!.windowToken, 0)
}
false
})
private void hideDefaultKeyboard() {
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
//you have got lot of methods here
}
You could try doing SetFocus() on another element in your layout.
If you are talking about the "enter/ok/return" button on the keyboard itself you may have to set up a KeyListener on the EditText control in order to know when to SetFocus() on another element.
You must catch the action en OnEditorActionListener
If you are in an Activity use:
editText.setOnEditorActionListener((viewAux, actionId, eventKey) -> {
if (actionId == EditorInfo.IME_ACTION_DONE){
//Remove focus
editText.clearFocus();
//Hide the keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
return true;
}
return false;
});
If you are in a Fragment add requireContext
editText.setOnEditorActionListener((viewAux, actionId, eventKey) -> {
if (actionId == EditorInfo.IME_ACTION_DONE){
editText.clearFocus();
InputMethodManager imm = (InputMethodManager)requireContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
return true;
}
return false;
});
A Kotlin solution that works for me, removes all active focus and close the soft keyboard.
Set android:focusableInTouchMode="true" on your parent layout.
In my case I have a ScrollView as well. I am setting view.clearFocus() in its touch event listener. This will clear all the focus on any touched textview. Then I proceed to close the soft keyboard on screen.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
...
android:focusableInTouchMode="true" //<--- this is important
tools:context=".SomFragment">
<ScrollView
android:id="#+id/scroll_view_container"
...
>
<TextView
android:id="#+id/sone_text_1"
...
/>
<TextView
android:id="#+id/sone_text_2"
...
/>
Then in your class
scroll_view_container.setOnTouchListener { v, event ->
view.clearFocus()
hideSoftKeyboard()
true
}
private fun hideSoftKeyboard() {
val windowToken = view?.rootView?.windowToken
windowToken?.let{
val imm = requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(it, 0)
}
}
Related
I have a numeric EditText in a fragment that shows the keyboard as normal when I select the EditText. I want to hide the keyboard when I enter OK. So I use a hide_keyboard() function which is working fine.
The issue I have is when I re-select the EditText, then the soft keyboard doesn't show up anymore. I tried many things but none worked.
Any ideas?
Here is my EditText:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="7"
android:id="#+id/kip_time"
android:hint="Reflexion time"
android:layout_below="#+id/chronometer_kipling"
android:layout_alignStart="#+id/chronometer_kipling"
android:layout_marginTop="10dp"
/>
and my hide_keyboard() function:
private void hide_keyboard(Context context, View view) {
InputMethodManager inputManager = (InputMethodManager)
context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput(0, 0);
}
and finally my onclicklistener method:
kip_time.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
reflexion_time = Integer.parseInt(kip_time.getText().toString());
reflexion_time = reflexion_time * 1000;
hide_keyboard(context, view);
}
});
If your need is to hide keyboard after entering value then simply use
android:imeOptions="actionDone"
It gives a 'done' button on soft-keyboard, which users can click when they done entering values. Add this to your EditText declaration and remove your hide_keyboard() function.
update you layout xml as follow.
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="7"
android:id="#+id/kip_time"
android:hint="Reflexion time"
android:layout_below="#+id/chronometer_kipling"
android:layout_alignStart="#+id/chronometer_kipling"
android:imeOptions="actionDone"
android:layout_marginTop="10dp"
/>
* to handle done button click event use the listeners as below *
kip_time.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// you codes gose here
//reflexion_time = Integer.parseInt(kip_time.getText().toString());
//reflexion_time = reflexion_time * 1000;
return true;
}
return false;
}
});
I fixed my problem by doing:
kip_time.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
Log.i("OK", "Enter pressed");
reflexion_time = Integer.parseInt(kip_time.getText().toString());
reflexion_time = reflexion_time * 1000;
hide_keyboard();
}
return false;
}
});
with the hide keyboard:
private void hide_keyboard() {
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(kip_time.getWindowToken(), 0);
}
no need for a show_keyboard()
Use the below method for hiding keypad and check whether it works to show the keypad again when you click on editText:
private void hideKeypad() {
View view = context.getCurrentFocus();
InputMethodManager inputManager = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (view instanceof EditText) {
inputManager.hideSoftInputFromWindow(context
.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
If it is not a multi-line input, just add this to the EditText
android:singleLine="true"
The user just tabs Enter/OK on soft-key and that is it...
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.
From the android tutorial :
pass_text.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
return true;
}
return false;
}
});
}
when click at EditText, it has a keyboard appear on the frame. I want to know after Enter.
How to make keyboard out from frame except click Back.
Thank you
Try the following
For Activity:
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(curEditText.getWindowToken(), 0);
In case of Fragment :
InputMethodManager mgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
give the EditText box you have the attribute android:imeOptions="actionDone"
this will change the Enter button to a Done button that will close the keyboard.
A working approach to get rid of the soft keyboard is to disable and then enable the TextEdit field in the Return-key event, button-press event or whatever. For example:
....
pass_text.setEnabled(false);
pass_text.setEnabled(true);
....
I think we can simply add this attribute to our EditText:
android:inputType="text"
This will automatically force the text to be on a single line and therefore when we click Enter, the keyboard disappears.
I'm struggling with the done button on the soft keyboard. I can't get the soft keyboard Done key press to hide the keyboard. From another button, it works perfectly with
imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);
but the onKeyListener does not function the way I want. When I hit the editText, the soft keyboard shows up and its content is cleared from characters.
Thanks for listening!
The main.xml:
<EditText
android:id="#+id/answer"
android:layout_gravity="center_horizontal" android:textSize="36px"
android:inputType="phone"
android:minWidth="60dp" android:maxWidth="60dp"
/>
The Java file:
private EditText editText;
//...
editText = (EditText)findViewById(R.id.answer);
editText.setOnClickListener(onKeyboard);
editText.setOnKeyListener(onSoftKeyboardDonePress);
//...
// method not working:
private View.OnKeyListener onSoftKeyboardDonePress=new View.OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
{
// code to hide the soft keyboard
imm = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);
}
return false;
}
};
private View.OnClickListener onKeyboard=new View.OnClickListener()
{
public void onClick(View v)
{
editText.setText("");
}
};
The working method using a button (in the same java file):
private View.OnClickListener onDone=new View.OnClickListener()
{
public void onClick(View v)
{
//....
// code to hide the soft keyboard
imm = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);
}
};
Edit: When I press key no "9" the keyboard hides. That's odd.
Use android:imeOptions="actionDone", like that:
<EditText
...
android:imeOptions="actionDone" />
InputMethodManager inputManager = (InputMethodManager)
context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput(0, 0);
with context being your activity.
Changed the if-statement to if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) made it working with the xml-attribute android:inputType="phone".
You should have a look at setOnEditorActionListener() for the EditText:
Set a special listener to be called when an action is performed on the
text view. This will be called when the enter key is pressed, or when
an action supplied to the IME is selected by the user.
SoftKeyboard can be hide by following way
In Java class we can write following code to hide keyboard when user press done or enter
etBid.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_DONE ||
event != null &&
event.getAction() == KeyEvent.ACTION_DOWN &&
event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
{
if (event == null || !event.isShiftPressed())
{
// the user is done typing.
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
return true; // consume.
}
}
return false; // pass on to other listeners.
}
Use below code with android:imeOptions="actionDone" its work for me.
<EditText
android:id="#+id/et_switch_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Name"
android:imeOptions="actionDone"
android:inputType="textPersonName" />
<EditText
...
android:inputType="text"
android:imeOptions="actionDone" />
When my user press Enter on the virtual android "user validate entry!" keyboard my keyboard stay visible! (Why?)
Here my Java code...
private void initTextField() {
entryUser = (EditText) findViewById(R.id.studentEntrySalary);
entryUser.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
userValidateEntry();
return true;
}
}
return true;
}
});
}
private void userValidateEntry() {
System.out.println("user validate entry!");
}
... here my View
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content">
<EditText android:id="#+id/studentEntrySalary" android:text="Foo" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
Maybe something wrong on my virtual device?
This should do it:
yourEditTextHere.setOnEditorActionListener(new OnEditorActionListener() {
#Override
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);
// NOTE: In the author's example, he uses an identifier
// called searchBar. If setting this code on your EditText
// then use v.getWindowToken() as a reference to your
// EditText is passed into this callback as a TextView
in.hideSoftInputFromWindow(searchBar
.getApplicationWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
userValidateEntry();
// Must return true here to consume event
return true;
}
return false;
}
});
Keep the singleLine="true" and add imeOptions="actionDone" to the EditText.
Then in the OnEditorActionListener check if actionId == EditorInfo.IME_ACTION_DONE, like so (but change it to your implementation):
if (actionId == EditorInfo.IME_ACTION_DONE) {
if ((username.getText().toString().length() > 0)
&& (password.getText().toString().length() > 0)) {
// Perform action on key press
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(username.getWindowToken(),
0);
doLogin();
}
}
If you make the text box a single line (I believe the propery is called SingleLine in the layout xml files) it will exit out of the keyboard on enter.
Here you go: http://developer.android.com/reference/android/R.styleable.html#TextView_singleLine
I am create a custom component who extends AutoCompleteTextView, like in the example below:
public class PortugueseCompleteTextView extends AutoCompleteTextView {
...
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event != null && (event.getKeyCode() == KeyEvent.KEYCODE_BACK)) {
InputMethodManager inputManager =
(InputMethodManager) getContext().
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(
this.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
return super.onKeyPreIme(keyCode, event);
}
I am using this code in the AlertDialog.Builder, but is possible to be using to Activity.
just add this line in your edit text.
android:imeOptions="actionDone"'
you can specify the next edit text id to move to that edit text on click of the keyboard done button.