Take the text from EditText without a Button - android

I'm avoiding to use a button to retrieve the text of an EditText. It is possible to take the confirmation when an user just confirm from the keyboard?

Here's a code snippet that takes an enter from the keyboard, and dismisses the soft keyboard from the screen when the user confirms:
txtUserWord.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode,KeyEvent event){
if((event.getAction()==KeyEvent.ACTION_DOWN)&&
(keyCode==KeyEvent.KEYCODE_ENTER)){
inputWord = txtUserWord.getText().toString();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(txtUserWord.getWindowToken(),0);
return true;
}
return false;
}
});

take a look at this previous post:
Setting the Return key on the Android keyboard

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

How can i hide/disable done button on soft keyboard android?

I am working with Amazon S3 api.
Problem: Working with TextWatcher. As soon as user enters more than 3 characters in the edittext, api is called and it shows matched results. The problem starts as soon the user hit the done/enter button on soft keyboard, the api is called again and it searches again. Can i stop this second call somehow? Thanks!
you can change it from xml:
<EditText
...
android:imeOptions="actionNone"/>
or from code:
editText.setImeOptions(EditorInfo.IME_ACTION_NONE);
It will remove the done button whenever user trigger the softkeybord
Maybe you can try to override the onKeyListener method in your control, to let it detect if the key pressed is 'enter' and add the code you want your 'enter' key to do ?
edittext.setOnKeyListener(new View.OnKeyListener() {
just handle done click and hide the soft keyboard
editText = (EditText) findViewById(R.id.edit_text);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// hide your keyboard here
try {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (inputMethodManager != null && activity.getCurrentFocus() != null) {
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
return false;
}
});
Add
android:maxLines="1"
to you xml and enter button should be disabled.
You can Do like editText.setImeOptions(EditorInfo.IME_ACTION_NONE);
but it won't hide the Enter/Next

To Hide Keyword after input on EditText

I typed phone number into edittext. then i want to go next view, because of the next view hide by keyword, so how can i hide this, during this situation, how can i handle this?
enter image description here
To hide the soft keyboard after you have finished typing and clicked on enter button.
Textview.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
//your content
}
/*This code will basically hide the keyboard*/
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
return false;
}
});
In this code after you have finished typing user will hit enter key and keyboard will hide the soft keyboard.

Hide keyboard after user searches?

I have an activity where there is an EditText and on enter key search results are shown, so what I simply want to do is to close the keyboard when search results are about to show to prevent the user from having to do it. However if the user wants to refine his search the keyboard should open back up if he taps into the EditText again.
This has been more difficult than I imagined, I've been search and tried a few things most don't even close the keyboard on my HTC, one method where the InputType is set to INPUT_NULL closes the keyboard but it doesn't open afterwards.
Any suggestions on how to do this?
#Override
public boolean onQueryTextSubmit(String query) {
// Your search methods
searchView.clearFocus();
return true;
}
Straight to the point and clean.
The right way to do this:
set imeOptions to "actionSearch"
initialize listeners for input and search button(if provided)
searchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
performSearch();
return true;
}
return false;
}
});
view.findViewById(R.id.bigSearchBar_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
performSearch();
}
});
Hide keyboard when user clicks search. To ensure that keyboard won't show when user minimizes and restores Activity you have to remove focus from EditText
private void performSearch() {
searchEditText.clearFocus();
InputMethodManager in = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
... perform search ...
}
I belive this code snippet will close the keyboard:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
if not try this one:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
let me know if these work

How to prevent keyboard to show in EditText onTouch?

What I want to do is this: I have an EditText and when the user click on it, the user can move the caret position, same as the EditText, but without showing the keyboard.
I've tried with setInputType(0); and it hides completly the keyboard but the cursor doesn't appears.
Is there any way to do this?
Thank's
The following tricks works for me. Caret and soft keyboard both active onTouch event of editText. So Call touch event and then hide the keyboard manually.
myEditText.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final boolean ret = dialerNumber.onTouchEvent(event);
final InputMethodManager imm = ((InputMethodManager) myContext
.getSystemService(Context.INPUT_METHOD_SERVICE));
try{
imm.hideSoftInputFromWindow(myEditText.getApplicationWindowToken(), 0);
}catch(Exception e){
e.printStackTrace();
}
return ret;
});
I haven't tried suppressing the keyboard altogether, but I have hidden the soft keyboard manually before using the InputMethodManager.hideSoftInputFromWindow method.
Does this work?
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)

Categories

Resources