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.
Related
I have a chat feature in my application. everything is working fine. The problem i am facing is that i have an edittext and a button for sending the text. Now when i press the send button the keyboard comes down which i don't want.
Because it is very annoying for the user to open the keyboard after sending every message. Does anyone have any solution for this. it is a very silly issue but it is quite important for me. and is there any change in xml or manifest which we can make which will help solve this problem
Try the below code:
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// User has pressed Back key. So hide the keyboard
InputMethodManager mgr = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(this.getWindowToken(), 0);
// TODO: Hide your view as you do it in your activity
} else if (keyCode == KeyEvent.KEYCODE_MENU) {
// Eat the event
return true;
}
return false;
}
I was faced with the same thing in one of my project's. Try doing this to show keyboard
private void showKeyboard(){
InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
yourEditText.requestFocus();
}
This makes the keyboard go down only when back is pressed.
According to this answer.
EditText txtEdit = (EditText) findViewById(R.id.txtEdit);
txtEdit.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// your additional processing...
return true;
} else {
return false;
}
}
});
Let me know if it solves your problem.
if you return true from your onEditorAction method, action is not going to be handled again. In this case you can return true to not hide keyboard when action is EditorInfo.IME_ACTION_DONE.
Hope it will help !
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...
Hello
I've got a searched EditText and search Button. When I type the searched text, I'd like to use ENTER key on softkeyboard instead of search Button to activate search function.
Thanks for help in advance.
You do it by setting a OnKeyListener on your EditText.
Here is a sample from my own code. I have an EditText named addCourseText, which will call the function addCourseFromTextBox when either the enter key or the d-pad is clicked.
addCourseText = (EditText) findViewById(R.id.clEtAddCourse);
addCourseText.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:
addCourseFromTextBox();
return true;
default:
break;
}
}
return false;
}
});
<EditText
android:id="#+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/search_hint"
android:inputType="text"
android:imeOptions="actionSend" />
You can then listen for presses on the action button by defining a TextView.OnEditorActionListener for the EditText element. In your listener, respond to the appropriate IME action ID defined in the EditorInfo class, such as IME_ACTION_SEND. For example:
EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendMessage();
handled = true;
}
return handled;
}
});
Source: https://developer.android.com/training/keyboard-input/style.html
may be you could add a attribute to your EditText like this:
android:imeOptions="actionSearch"
add an attribute to the EditText like
android:imeOptions="actionSearch"
this is the best way to do the function
and the imeOptions also have some other values like "go" 、"next"、"done" etc.
Most updated way to achieve this is:
Add this to your EditText in XML:
android:imeOptions="actionSearch"
Then in your Activity/Fragment:
EditText.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
// Do what you want here
return#setOnEditorActionListener true
}
return#setOnEditorActionListener false
}
We can also use Kotlin lambda
editText.setOnKeyListener { _, keyCode, keyEvent ->
if (keyEvent.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
Log.d("Android view component", "Enter button was pressed")
return#setOnKeyListener true
}
return#setOnKeyListener false
}
To avoid the focus advancing to the next editable field (if you have one) you might want to ignore the key-down events, but handle key-up events. I also prefer to filter first on the keyCode, assuming that it would be marginally more efficient. By the way, remember that returning true means that you have handled the event, so no other listener will. Anyway, here is my version.
ETFind.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER
|| keyCode == KeyEvent.KEYCODE_ENTER) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
// do nothing yet
} else if (event.getAction() == KeyEvent.ACTION_UP) {
findForward();
} // is there any other option here?...
// Regardless of what we did above,
// we do not want to propagate the Enter key up
// since it was our task to handle it.
return true;
} else {
// it is not an Enter key - let others handle the event
return false;
}
}
});
this is a sample of one of my app how i handle
//searching for the Edit Text in the view
final EditText myEditText =(EditText)view.findViewById(R.id.myEditText);
myEditText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||
(keyCode == KeyEvent.KEYCODE_ENTER)) {
//do something
//true because you handle the event
return true;
}
return false;
}
});
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)
}
}
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" />