how to make keyboard go away when enter is press - android

O my acer tablet the return does not get replace by done with the following xml, but does on my other 2 android devices
So I was going to use the return key to exit the keyboard as a bck up,
problem, I figured out how to do the call back when enter key is press, but don't know how to make the keyboard go away,
code
mUserName=(EditText)findViewById(R.id.viewUserName);
mUserName.setOnEditorActionListener(
new android.widget.TextView.OnEditorActionListener()
{
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
// goes here when enter is press
return false;
}
}
);

In your xml for the edit text, set the imeOptions. Basically just add this line:
android:imeOptions="actionDone"
This will change the enter button to a "done" button, and dismiss your keyboard when pressed.
-OR-
You can add this inside the code block from above
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mUserName.getWindowToken(), 0);

Related

How to hide softkeyboard in multiline edittext?

I am making small application. I use EditText to summarize all data. It shows a few times, every time has own line. User is able to write short note next to time.
I have issue with soft keyboard. I would like it to have "ok" instead of enter. I mean, it shoul not do extra line. On enter, keyboard should close.
For now, I have something like this:
EtNotes.setOnKeyListener(new View.OnKeyListener()
{
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
{
InputMethodManager imm = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
}
return false;
}
});
It closes keyboard, but it does extra line and that is the problem.
I have already tried to put:
android:imeOptions="actionDone"
in my layout xml file. Unfortunately it also didn't worked.
To sum up, I would like my keyboard don't do extra line and closes after enter button.
You have to return true in the branch where you handled the key input

Android keyboard enter button to keep keyboard visible

I'd like for my enter key to complete an action without hiding the keyboard afterwards. Currently every time I enter something and press the enter button the EditText input is cleared to accept the next input, but as the keyboard disappears I have to click the EditText again so that the keyboard would appear again...
Right now this is on my layout:
<EditText
android:id="#+id/etCommand"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/commandHint"
android:inputType="textNoSuggestions"
android:imeOptions="actionSend" >
</EditText>
And the code:
etCommand = (EditText) findViewById(R.id.etCommand);
etCommand.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendCommand();
}
return false;
}
});
EDIT
As swayam suggested I had to return true inside onEditorAction so that the OS would think the action was handled and leave it alone.
Have a go at this code snippet :
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
Add the KeyListener for EditText for Enter key press and after the task completed requestfocus on edittext again would be solution

Android: disable (or change of the function of ) the return key when AutoCompleteTextView is focused

My app has an AutoCompleteTextView used for searching. When it's in focus, I would like to disable or change the function of the return key to a specific function call. I tried in my layout xml to add the following property to the AutoCompleteTextView
android:imeOptions="actionDone"
But it works on my simulator (when you click enter, the keyboard disappears) but it doesn't work on my device (moto droidx running 2.3.3).
Can someone show me how I can link the return key to a specific function (in my case, the search function) with android:imeOptions="actionGo"?
Write your code in setOnEditorActionListener event of EditText family. like
autoEditText.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(autoEditText.getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
//Commented line is for hide keyboard. Just make above code as comment and test your requirement
//It will work for your need. I just putted that line for your understanding only
//You can use own requirement here also.
}
return false;
}
});
Happy coding :)
For me it works if you add one more line about input type:
android:inputType="text"
android:imeOptions="actionDone"

How to hide the soft keypad when leaving an editbox?

After a user inputs into an editbox with the keypad, I want the keypad to hide so I can see the results near the bottom of the screen. I've tried this in my onCreate method but it doesn't work. What am I missing?
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditTextbox.getWindowToken(), 0);
You can force hide the keyboard like so:
Window window = getWindow();
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
I would get the window in your onCreate method and in the onFocusChanged() method or OnKeyListener() hide the keyboard.
Have you set up a Key Listener?
You don't really state how you know the user is done entering the text so I'll assume they are pressing the enter button on the soft keyboard. Here is how I am handling that type of scenario. I am using this both in a Dialog and an Activity with success. Hope it helps.
this.setOnKeyListener(new OnKeyListener()
{
/**
* This listens for the user to press the enter button on
* the keyboard and then hides the virtual keyboard
*/
#Override
public boolean onKey(DialogInterface dialog, 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) )
{
// hide virtual keyboard
InputMethodManager imm =
(InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(sessionTag.getWindowToken(), 0);
return true;
}
return false;
}
});
It depends on what currently has focus...if its another editText that takes focus then this might be bringing up the keypad...try to explicitly give focus to a different element...
You can extend EditText class and use your code in onFocusChanged() method when focused argument is false.
Follow this answer. call the method after you setContentView in your activity.
https://stackoverflow.com/a/11656129/1066839

After type in EditText, how to make keyboard disappear

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.

Categories

Resources