My EditText in xml wrapped in LinearLayout:
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="TYPE HERE"
android:padding="7dp"
android:singleLine="true"
android:cursorVisible="true"
android:textIsSelectable="true"
/>
And in my fragment:
mEditText.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
mEditText.requestFocus();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT);
return true;
}
Copy and paste feature doesn't work on the edit text ??
When I comment onTouchListener it works !
acbacbeaoclacm m cl adc a dcl arc ac a c amid ncoeocnejnlwencnajlendjlcejbcbaclbaldcblabdlbclabdclbladbclaelbclabdbcalebecam emac cjaecljabecbaebcjbec
From the source (http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/widget/TextView.java) it seems that onTouch events are handling the IME, keyboard, copy/paste, and similar operations. If you override those methods or provide custom listeners, make sure you handle the events yourself.
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
I want to make similar feature in my app imageview onClick open keyboard that i have done and keyboard is opening very well.
I'm using a relative layout i have placed the image of keyboard on the edittext which is similar to this reference hangouts image.
problem is when i type something it does not show up in edittext's text field. How to handle it? guide me
protected void onCreate(Bundle savedInstanceState) {
imageview = (ImageView) findViewById(R.id.keyboard);
imageview.setFocusableInTouchMode(true);
imageview.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
try {
showSoftKeyboard(imageview);
} catch (Exception e) {
Log.e("Error MA onCreate keyboard open", "Error");
}
break;
}
return true;
}
});
}
public void showSoftKeyboard(View view) {
if (view.requestFocus()) {
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
this is my xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="#+id/search_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="12dp" >
<ImageView
android:id="#+id/keyboard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="true"
android:src="#drawable/ic_action_keyboard" />
<EditText
android:id="#+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/keyboard"
android:hint="Search Here"
android:focusable="true" />
</RelativeLayout>
<ListView
/>
</RelativeLayout>
Problem: imageView is calling the softkeyboard.
Possible solution: After clicking on the imageView call showSoftKeyboard() method using your edittext as a parameter.
public void showSoftKeyboard(View view) {
if (view.requestFocus()) {
// myEditText.requestFocus();//this line is also worth a try
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
public void showSoftKeyboard(View view) {
if (view.requestFocus()) {
mEditText.setFocusable(true);
mEditText.requestFocus();// this line is also worth a try
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT);//sending mEditText here in the parameter solved my problem instead of sending view
}
}
I have a customized Dialog. Inside the dialog, there's an EditText. When I touch inside the EditText, the soft keyboard is shown. I want to hide this keyboard when I touch inside the other place of the Dialog.
I know how to hide a keyboard in activity. I just don't know how to do this in Dialog.
Thank you.
You can do that easy by using focuslisners, see my code sample below:
EditText.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(EditText, InputMethodManager.SHOW_IMPLICIT);
}
else
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(EditText.getWindowToken(), 0);
}
}
});
edit:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable = "true"
android:isFocusableInTouchMode = "true"
android:clickable = "true" >
<EditText
android:id="#+id/EditText"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:hint="hint"
android:singleLine="true" />
</LinearLayout>
</RelativeLayout>
\to be sure to get focus:
LinearLayout actionHide = (LinearLayout) findViewById(R.id.wrapper);
actionHide.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
actionHide.requestFocus(); // use this to trigger the focus listner
//or use code below to set the keyboard to hidden
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(EditText.getWindowToken(), 0);
}
});
Try by setting InputMethodManager.SHOW_FORCED for Edittext :
InputMethodManager input_manager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
input_manager.showSoftInput(edittext, InputMethodManager.SHOW_FORCED);
I am facing a weird problem in edittext control of android.
The issue is, when i type anything in softkeyboard. the text is not getting printed in editbox.
Here is the code
mEditview.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
});
mEditview.requestFocus();
layout code is added
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/filter_text"
android:imeOptions="actionDone"
android:singleLine="true"
android:hint="Search"/>
Here is what i get as output in emulator. Why its coming like this? how to fix this problem