Hide Soft keyboard on return key press - android

I've searched half a dozen other answers on SO, but haven't found one that works. All I'm trying to do is dismiss the soft keyboard when the user presses the enter button. (The equivalent of the insanely easy iOS 'resignKeyboard' call.) In the following code, the onEditorAction method does not get called. I've set up an EditText view in my XML file and the code in my fragment is as follows:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fragView = inflater.inflate(R.layout.fragment_encrypt2, container, false);
textField = (EditText) fragView.findViewById(R.id.textField);
textField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView arg0, int actionId,
KeyEvent arg2) {
// hide the keyboard and search the web when the enter key
// button is pressed
if (actionId == EditorInfo.IME_ACTION_GO
|| actionId == EditorInfo.IME_ACTION_DONE
|| actionId == EditorInfo.IME_ACTION_NEXT
|| actionId == EditorInfo.IME_ACTION_SEND
|| actionId == EditorInfo.IME_ACTION_SEARCH
|| (arg2.getAction() == KeyEvent.KEYCODE_ENTER)) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(textField.getWindowToken(), 0);
return true;
}
return false;
}
});
// Inflate the layout for this fragment
return fragView;//inflater.inflate(R.layout.fragment_encrypt2, container, false);
}
The following is a snippet from the XML file where I define the EditText field. I need EditText to be multiline.
<EditText
android:id="#+id/textField"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="#string/Encrypted_Text_Hint"
android:gravity="top"
android:inputType="textMultiLine"
android:imeOptions="actionDone"/>

If you don't want multiple line, for you edittext you can just specify a single line. For the edittext and also you can put imeOptions as Done like this:
<EditText
android:id="#+id/edittext_done"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
/>
I clearly don't know, whether you are trying to achieve this or not.Any way take a look.
EDIT: android:singleLine is deprecated since API 3 due to bad performance, you have to use android:maxLines instead. singleLine will be available because even now some effects are not supported by android:maxLines attribute.

Below code works for me.
IN XML:
android:imeOptions="actionDone"
android:inputType="textCapWords"
IN JAVA CLASS:
edit_text.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// TODO Auto-generated method stub
if ((actionId==EditorInfo.IME_ACTION_DONE ) )
{
//Toast.makeText(getActivity(), "call",45).show();
// hide virtual keyboard
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
//or try following:
//InputMethodManager imm = (InputMethodManager)getBaseContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(auto_data.getWindowToken(), 0);
return true;
}
return false;
}
});

I solved the issue by changing the following in the XML file from:
android:inputType="textMultiLine"
to:
android:inputType="textImeMultiLine"

Based on my tests, what is really required to dismiss the soft keyboard, when a user clicks the enter button, can be achieved simply by adding the code below to your EditText in xml.
android:imeOptions="actionDone"
android:inputType="textImeMultiLine"
No need for OnEditorActionListener or any other Java code.

Accepted answer is deprecated:
<EditText
android:id="#+id/edittext_done"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
/>
Try with this:
<EditText
android:id="#+id/edittext_done"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:maxLines="1"
/>

EditText.xml
<EditText
android:id="#+id/edit_text_searh_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:singleLine="true"
android:lines="1"
android:hint="Ingresa palabras claves"
android:imeActionLabel="Search"
android:background="#drawable/rounded_edit_text_search"
android:drawableRight="#android:drawable/ic_menu_search"/>
Fragment.java
final EditText edit_text_searh = (EditText) v.findViewById(R.id.edit_text_searh_home);
edit_text_searh.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit_text_searh.getWindowToken(), 0);
Toast.makeText(getContext(),edit_text_searh.getText(),Toast.LENGTH_SHORT).show();
return true;
}
});

This may be you could add a attribute to your EditText like this:
android:imeOptions="actionSearch"

Try this method, It may be solve your problem.
protected void showKeyboard() {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (activity.getCurrentFocus() == null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
} else {
View view = activity.getCurrentFocus();
inputMethodManager.showSoftInput(view,InputMethodManager.SHOW_FORCED);
}
}
/**
* Hide keyboard.
*/
protected void hideKeyboard() {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
View view = activity.getCurrentFocus();
if (view == null) {
if (inputMethodManager.isAcceptingText())
inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
} else {
if (view instanceof EditText)
((EditText) view).setText(((EditText) view).getText().toString()); // reset edit text bug on some keyboards bug
inputMethodManager.hideSoftInputFromInputMethod(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}

This is what worked for me. Based on this layout xml:
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/ip_override_text_input_sat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:hint="IP Address" />
</com.google.android.material.textfield.TextInputLayout>
I needed to do this for the keyboard to dismiss and the text into to lose focus and hide the cursor.
findViewById<TextView>(R.id.ip_override_text_input_sat).setOnKeyListener { v, keyCode, event ->
if (keyCode == KeyEvent.KEYCODE_ENTER && currentFocus != null) {
val inputManager =
getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.hideSoftInputFromWindow(
this.currentFocus!!.windowToken,
HIDE_NOT_ALWAYS
)
currentFocus!!.clearFocus()
return#setOnKeyListener true
}
return#setOnKeyListener false
}
Hope this helps someone doing the same

Related

How to hide EditText cursor when activity starts

When activity starts the EditText put cursor automatically without shows keyboard.
And when I finish use EditText and hide keyboard the cursor still shows.
<EditText
android:id="#+id/edit_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/m_a_0_search_shape"
android:hint="#string/m_a_0_search"
android:inputType="text"
android:maxLength="50"
android:maxLines="1"
android:paddingStart="15dp"
android:paddingTop="10dp"
android:paddingEnd="15dp"
android:paddingBottom="10dp"
android:textColor="#color/m_a_0_search"
android:textColorHint="#color/m_a_0_search"
android:textSize="20sp" />
Coped
Solution
1) Set in your xml under your EditText:
android:cursorVisible="false"
2) Set onClickListener:
iEditText.setOnClickListener(editTextClickListener);
OnClickListener editTextClickListener = new OnClickListener()
{ public void onClick(View v)
{ if (v.getId() == iEditText.getId())
{
iEditText.setCursorVisible(true);
}
}
};
3) then onCreate, capture the event when done is pressed using OnEditorActionListener to your EditText, and then setCursorVisible(false).
iEditText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
iEditText.setCursorVisible(false);
if (event != null&& (event.getKeyCode() ==
KeyEvent.KEYCODE_ENTER)) {
InputMethodManager in = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(iEditText.getApplicationWindowToken()
,InputMethodManager.HIDE_NOT_ALWAYS);
}
return false;
}
});
If you need to remove the cursor from edittext programmatically you can use
edit_search.clearFocus();
This will shift the cursor to next focusable item in your layout if any.

getCurrentFocus is null when trying to hide the keyboard

I have a RelativeLayout nested inside another RelativeLayout. By default, it is hidden. When user clicks a button, the nested RelativeLayout becomes visible. This layout contains an EditText. When user is finished typing, I want to hide the keyboard:
<RelativeLayout
...
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cameraEmoji"
android:contentDescription="#string/camera_emoji"
android:src="#mipmap/ic_image_black_24dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/cameraText"
android:layout_marginStart="57dp" />
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/emojiTextView"
android:background="#80444444">
<EditText
android:id="#+id/actionEmojiText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="#FFF"
android:imeActionId="#+id/finishText"
android:imeActionLabel="#string/action_complete_emoji_text"
android:imeOptions="actionDone"
android:maxLines="1"
android:singleLine="true"/>
</RelativeLayout>
...
</RelativeLayout>
The activity:
cameraText = (ImageButton)findViewById(R.id.cameraText);
cameraText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
emojiTextView = (RelativeLayout) findViewById(R.id.emojiTextView);
emojiTextView.setVisibility(View.VISIBLE);
final EditText actionEmojiText = (EditText)findViewById(R.id.actionEmojiText);
actionEmojiText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
if (id == EditorInfo.IME_ACTION_UNSPECIFIED) {
emojiTextView.setVisibility(View.INVISIBLE);
View view = MainActivity.this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
dropTextIn(actionEmojiText);
}
return false;
}
});
}
});
getCurrentFocus returns null but the keyboard remains visible. How can I hide the keyboard after typing in the EditText field?
editText.setFocusable(false);
editText.setFocusableInTouchMode(true);
try to add these two properties to EditText, It will hide keyboard whenever you touch any other view after typing to Edittext
I discovered what appears to be the issue. getCurrentFocus() was returning null because the item I had focused I had set to View.INVISIBLE before invoking getCurrentFocus(). Consequently, the view was no longer visible and thus no longer focused.

Keyboard not displayed onClick EditText

I'm struggling to get my keyboard to display in my EditText. When I click the EditText for the first time, the keyboard displays and behaves normally.
When I press the back button to hide the keyboard and click the same EditText again, the keyboard fails to display. I'd really appreciate any help. The relevant code is below.
NewEntry:
assessor.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
EditText email = (EditText) context.findViewById(R.id.email);
email.requestFocus();
return true;
}
Layout:
<EditText
android:layout_height="72dp"
android:layout_width="wrap_content"
android:id="#+id/assessor"
android:textColor="#color/black"
android:textSize="20dp"
android:background="#android:color/transparent"
android:fontFamily="sans-serif-light"
android:layout_marginLeft="72dp"
android:layout_marginRight="16dp"
android:hint="Assessor"
android:inputType="text"
android:imeOptions="actionNext"
android:ems="10"
android:windowSoftInputMode="adjustPan"
/>
Try this:
set an onClickListener for the EditText, and when it is clicked it will request focus
EditTextName.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
editText.requestFocus();
}
I don't know if this will solve your problem, but at least i tried :P

How to handle an enter pressed key android

I am developing an app for an on-line store. One of the main feature is the search functionality. As I need to support API 8 and above instead of the SearchView I have used an EditText like below to implement the search view.
<EditText android:id="#+id/search_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_toLeftOf="#+id/search_button"
android:background="#color/white"
android:ems="20"
android:imeOptions="actionDone"
android:maxLines="1"
android:textColor="#color/black"
android:textColorHint="#color/ebuy_color_second_strip"
android:textSize="18sp" >
</EditText>
Now I want to handle the enter key of the keyboard, in a way than when the user press enter, the search activity begins. The code that I have used is like below:
search = (EditText) findViewById(R.id.search_edit);
search.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
String query_text = search.getText().toString().trim();
try {
query_text = URLEncoder.encode(query_text, "utf-8");
} catch (UnsupportedEncodingException e) {
}
String full_query = query_text;
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(search.getWindowToken(), 0);
Intent bussiness = new Intent(EbuyHomeScreen.this,
EbuySearchActivity.class);
Bundle basket_buss_category = new Bundle();
basket_buss_category.putString(EbuyUtils.TAG_CATEGORY_CODE, full_query);
bussiness.putExtras(basket_buss_category);
startActivity(bussiness);
return true;
}
return false;
}
});
This way works perfectly good with android keyboard and some other keyboards, but when I use swipe or swift keyboard and other personalized keyboards that are downloaded from Google Play Store this doesn't work , and instead of beginning the activity jumps in a new line. Can somebody help me solve this kind of issue.
Try setOnEditorActionListener on your edittext.
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
//your code here
return true;
}
return false;
}
});
You need to set imeAction in your edittext's xml file:
<EditText
android:id="#+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="USERNAME"
android:inputType="text"
android:imeOptions="actionSearch" >
<requestFocus />
</EditText>

Request focus from an edittext to another with a different inputType

I have to different EditText like these :
EditText1 has the inputType "textCapWords"
EditText2 has the inputType "number"
When I launch my activity, I instantly request focus on the EditText1. The only way to do so that is working is to add this on the onCreate :
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
EDITTEXT1.requestFocus();
When the user clicks on "Return" on his soft Keyboard, I want the EDITTEXT2 to obtain focus. So I added this :
EDITTEXT1.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == 66) { // CODE FOR RETURN
EDITTEXT2.requestFocus();
}
return false;
}
});
But when I do so, nothing happens. All my EditText are set FocusableInTouchMode and Focusable :
<EditText
android:id="#+id/EDITTEXT1"
android:layout_width="0dip"
android:layout_height="20dip"
android:layout_weight="2"
android:background="#color/White"
android:ems="10"
android:hint="My First EditText"
android:inputType="textCapWords"
android:padding="2dip"
android:singleLine="true"
android:textColor="#color/Black"
android:textSize="12dip" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/EDITTEXT2"
android:layout_width="0dip"
android:layout_height="20dip"
android:layout_weight="2"
android:background="#color/White"
android:ems="10"
android:hint="My Second EditText"
android:inputType="textCapWords"
android:padding="2dip"
android:singleLine="true"
android:textColor="#color/Black"
android:textSize="12dip" >
</EditText>
Do you have any idea on how I could focus the EDITTEXT2 after pressing OK on EDITTEXT1 ?
Thanks in advance!
Thanks a lot for your answer. I finally found the easiest way to answer my own question :
Add :
android:nextFocusRight="#+id/EDITTEXT2"
To the properties of EDITTEXT1.
Nothing else is needed in order to go to the EDITTEXT2 after clicking "Enter" on EDITTEXT1.
you should create your own EditText which extended from EditText firstly, then override the mothod dispatchKeyEventPreIme like this:
#Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
return super.dispatchKeyEventPreIme(event);
} else if (event.getAction() == KeyEvent.ACTION_UP) {
hideInputMethod();
//change your focus at here
return true;
}
}
return super.dispatchKeyEventPreIme(event);
}
protected void hideInputMethod() {
InputMethodManager imm = (InputMethodManager)getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (null != imm) {
imm.hideSoftInputFromWindow(getWindowToken(), 0);
}
}
By the way, it not a good way to code like that if (keyCode == 66) { // CODE FOR RETURN, and also 66 is not the value of back, it's key enter.
public static final int KEYCODE_ENTER = 66;
you'd better write KeyEvent.KEYCODE_BACK or KeyEvent.KEYCODE_ENTER instead.
I wish this could help you.

Categories

Resources