EditText OnKeyListener not working - android

I know this/similar question has been asked before but the solution given is not working for me so I'm asking again.
I tried the solution given in that answer but still my OnKeyListener is never being invoked on some devices, especially the ones with stock OS. I need to detect pressing of del key of soft keyboard when when there is no character in editText. Here is my code;
EditText et = (EditText) findViewById(R.id.et);
et.setOnKeyListener(new EditText.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.d("OnKeyListener", keyCode + " character(code) to send");
return false;
}
});

Finally solved myself by implementing this feature through TextWatcher. The major hurdle was that, I needed to detect backspace press even when there is no character in EditText or at least the end user perceives that there is no character there. The fist thing couldn't be achieved however I did the later one. Following is the detailed solution.
First of all, I always retained a space ' ' character in my editText.
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
if(cs.toString().length() == 0)
editText.setText(" ");
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { }
#Override
public void afterTextChanged(Editable arg0) { }
});
Then I customized EditText to notify me for every cursor position change. This purpose is achieved by overriding onSelectionChanged method of EditText. My customized EditText looks like this.
public class SelectionEnabledEditText extends EditText {
public SelectionEnabledEditText(Context context) {
super(context);
}
public SelectionEnabledEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SelectionEnabledEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onSelectionChanged(int selStart, int selEnd) {
super.onSelectionChanged(selStart, selEnd);
if(onSelectionChangeListener != null)
onSelectionChangeListener.onSelectionChanged(selStart, selEnd);
}
public static interface OnSelectionChangeListener{
public void onSelectionChanged(int selStart, int selEnd);
}
private OnSelectionChangeListener onSelectionChangeListener;
public void setOnSelectionChangeListener(OnSelectionChangeListener onSelectionChangeListener) {
this.onSelectionChangeListener = onSelectionChangeListener;
}
}
Finally, in my activity, I'm listening for cursor position changed event and resetting my cursor position in editText if it's there at the necessary space charatcer start i.e. at 0th index, like this;
editText.setOnSelectionChangeListener(new SelectionEnabledEditText.OnSelectionChangeListener() {
#Override
public void onSelectionChanged(int selStart, int selEnd) {
if (selEnd == 0) {
if (editText.getText().toString().length() == 0)
editText.setText(" ");
editText.setSelection(1);
}
}
});
Hope this would be helpful in similar situations. Suggestions are welcomed for improvements/optimizations.

The documentation states that the key events will only be propagated for the hardware key strokes, not software.
http://developer.android.com/reference/android/view/View.OnKeyListener.html
The device manufacturers are actually being discouraged to propagate soft keyboard events through key listeners, although it is completely up to the manufacturer to honour that or to actually treat the soft and hard keyboards with equal terms.
Starting from Android 4.2.2, Android system itself will not support key stoke events for the soft keyboards at all, so even the manufacturers will not be able to choose their way.
So the only foolproof option here is to implement your own IME (soft keyboard), and handle the keystrokes yourself.
TextWatcher can be used mostly to replace the key listeners, however editText.setText(...); will also trigger the TextWatcher events, so if one is interested in typed keys only then probably TextWatcher is not a solution either.
Please be cautious when using TextWatcher with AutocomleteTextView or EditText. Do not modify text in the AutocompleteTextView / EditText's content from within TextWatcher events, cause otherwise you'll most probably end up in an infinite event/listening loop.

Based on the documentation of the OnKeyListener it seems that the callback is invoked only for hardware keyboards.
Interface definition for a callback to be invoked when a hardware key event is dispatched to this view. The callback will be invoked before the key event is given to the view. This is only useful for hardware keyboards; a software input method has no obligation to trigger this listener.

There's an attribute for EditText: android:imeOptions
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/main_editor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionGo"
/>
I noticed that for values: actionGo, actionNone, normal, actionSearch onKeyListener got called.
P.S. It even worked without specifying that attribute.!

Try something like this :
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
//Log key
Log.d("OnKeyListener", keyCode + " character(code) to send");
}
return false;
}

Related

Android: soft keyboard Backspace on EditText not working

This is what happens:
Activity A and B have an EditText and they both have IME_SEARCH. The input is done only via soft keyboard on a SAMSUNG tablet.
On Activity A I can use the EditText without problems. The thing is that on Activity B I can't erase text with backspace after I hit 'space' or whenever I use a word from the suggestions. It behaves like there wasn't text there anymore. If I type new characters, I can erase them up to the space.
Important points:
The View hierarchy that contains the EditTexts are identical
The code that configures the IME_SEARCH processing (via setOnEditorActionListener) is identical
The TextWatcher of both are also identical
In the Manifest, both activities are configures with
android:configChanges="keyboardHidden|keyboard|orientation"
android:windowSoftInputMode="stateAlwaysHidden|adjustUnspecified"
I set a breakpoint on the method beforeTextChanged of both TextWatcher. I inserted a 'space' and hit 'backspace'. On the Edittext of activity A, the method is triggered but on activity B's it is not triggered. I can't see the reason for this to happen since the properties of both Edittext are configured identically.
I also tried removing the IME option but the behavior kept the same.
Does anyone know what could be happening?
EDIT 1:
searchTxt.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.length() == 0) {
btnClear.setVisibility(View.GONE);
} else{
btnClear.setVisibility(View.VISIBLE);
}
}
});
searchTxt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
buildGrid();
return true;
}
return false;
}
});
EDIT 2:
The layout hierarchy is as following.
<LinearLayout
... >
<include layout="#layout/title_bar" />
<RelativeLayout
...>
<EditText
...>
The problem was that, for some reason, the Activity B was overriding dispatchKeyEvent() and always returning true. Removing it solved the problem.
I have similar problems that you are facing and I somehow managed to stumble on the solution. Apparently, I had setOnKeyListener to 'return true'. After I changed it to 'return false', the phone keyboard works perfect with backspace functioning properly once again on edittext. Hope this helps:
.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
...
return false;
}
});

Android - catch backspace (delete) button on custom EditText

I want to catch the backspace/delete button event in soft keyboard on EditText custom.
How can I do it ?
I tried these solutions but they do not work for me :
Android custom EditText and back button override
EditText OnKeyDown
Get back key event on EditText
Android EditText delete(backspace) key event
Thanks for your help !
EDIT :
I found a fix for that isssue with the function DispatchKeyEvent :
#Override public boolean dispatchKeyEvent(#NonNull KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
// ...
}
return super.dispatchKeyEvent(event);
}
I found a fix for that isssue with the function DispatchKeyEvent :
#Override public boolean dispatchKeyEvent(#NonNull KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
// ...
}
return super.dispatchKeyEvent(event);
}
Catching Key Events from soft input methods in Android is unreliable. Here is an excerpt from the JavaDoc for the KeyEvent class:
As soft input methods can use multiple and inventive ways of inputting
text, there is no guarantee that any key press on a soft keyboard will
generate a key event: this is left to the IME's discretion, and in
fact sending such events is discouraged. You should never rely on
receiving KeyEvents for any key on a soft input method. In particular,
the default software keyboard will never send any key event to any
application targetting Jelly Bean or later, and will only send events
for some presses of the delete and return keys to applications
targetting Ice Cream Sandwich or earlier.
One workaround could involve using TextWatcher. Whenever a delete occurs in an EditText, the character count drops.
myEditText.addTextChangedListener(this);
//...
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count == 0) {
//text was deleted.
}
}
Edit: This is untested, so may not be completely reliable. In addition, it's worth pointing out that count may also read 0 if a long section of text is selected and then replaced. I created a library once that was designed to add Undo/Redo functionality to EditText, but can also identify the difference between text being replaced and deleted, so this may be of use to you if you require more accuracy.
Please, take a look at a solution I provided in another similar topic: Android - cannot capture backspace/delete press in soft. keyboard. You need to build your custom EditText class and override onCreateInputConnection method in it. It will give you access to deleteSurroundingText event which is firing when you click backspace. I've tested it in some devices and hope it will work in others also. Try it and give a feedback.
Here you go..
editText.addTextChangedListener(new TextWatcher() {
private int before;
private int after;
private int prevLength;
private boolean isBackPressed;
ArrayList<Integer> arrayList;
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
prevLength = charSequence.length();
}
#Override
public void onTextChanged(CharSequence charSequence, int start, int end, int count) {
}
#Override
public void afterTextChanged(Editable editable) {
isBackPressed = prevLength > editable.length();
if(isBackPressed){
Toast.makeText(getApplicationContext(),"Back",Toast.LENGTH_LONG).show();
}
}
});

Space Key event behaviour on Numeric Input field?

I created two EditText with "android:inputType="number" property.
here I am using hardware Keyboard, so when I perform Space Key event on textField, focus control directly shift from editText view to some other random view of screen. In normal text field type it took it as an another character, that's fine.
Any one have idea how can use Space key event to retain focus on same field.
Using editText.setInputType(InputType.TYPE_CLASS_NUMBER) may probably solve your problem.
Change/Add your EditText Property with android:imeOptions="actionNext" like as follows
<EditText
android:id="#+id/edit_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:inputType="text" />
Or with android:imeOptions="actionNone" for default behavior of EditText
<EditText
android:id="#+id/edit_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNone"
android:inputType="text" />
So it looks like your problem doesn't have to do with the 'input' type of your EditText, but rather the key press events coming from the keyboard....
So 'hopefully' this should fix your problem for you (by 'skipping over' the 'next' event from the 'space' button being pressed.)
// do for both EditText(s)
editText1.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
log.d("main","KeyPress:"+ actionID);
if (event != null ) { log.d("main","KeyPress:" + event.getKeyCode() ); }
if ( actionId == EditorInfo.IME_ACTION_NEXT) {
// do 'nothing' or 'add a space' if you want that
return true;
}
return false;
}
});
This is likely what is happening (hard to tell without your XML)
So I found something that should help you figure out the pattern + possibly lead to solving it... from https://stackoverflow.com/a/17990096
if there is a android:nextFocus.... setting in your XML (or equivalent in code) and/or the physical keyboard 'space' is also signalling IME_ACTION_NEXT (or another IME action similar to it)
If the EditorInfo.IME_ACTION_NEXT isn't the IME action that is causing your problems, then you can try this... to determine WHAT is.
from: https://stackoverflow.com/a/4171427
if you have PHYSICAL keyboard you can use to detect keyDown events and handle them appropriately,
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
log.d("main","KeyPress:" + keyCode);
if (event != null ) { log.d("main","KeyPress:" + event.getKeyCode() ); }
// if you want to 'handle' the keyPess here, best to use switch like below
// switch (keyCode) {
// case KeyEvent.KEYCODE_A:
// {
// //your Action code
// return true;
// }
/// }
return super.onKeyDown(keyCode, event);
}
BUT... if you have software keyboard You need to use addTextChangedListener/TextWatcher Because the physical key press is 'eaten' by the EditText (from what I saw in another post, but seems to be correct from my testing.)
mMyEditText.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
/*This method is called to notify you that, within s, the count characters beginning at start are about to be replaced by new text with length after. It is an error to attempt to make changes to s from this callback.*/
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
);
You can override what the EditText does when a 'space' is entered.
This seems pretty easy when in 'software keyboard', but physical keyboard seems to be a bit more difficult.
https://developer.android.com/reference/android/widget/TextView.OnEditorActionListener.html
Similar to this question (but 'space' instead of 'enter') Android - Handle "Enter" in an EditText
This could help you determine the 'pattern' of the focus being changed. if it is random or not (most likely -not- random, but possibly 'interesting' to look into)
http://developer.android.com/reference/android/view/View.OnFocusChangeListener.html
// try this to see
View.OnFocusChangeListener changeListener = new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus) {
log.d("YourClassName", "FocusChanged" + hasFocus)
}
};
EditText1.setOnFocusChangeListener(changeListener);
EditText2.setOnFocusChangeListener(changeListener);
Button.setOnFocusChangeListener(changeListener);
// ... etc... (add to views to see if there is pattern)

I need to configure a multiline edit text but never leave a newline ENTER [duplicate]

How do I make an EditText on Android such that the user may not enter a multi-line text, but the display is still multi-line (i.e. there is word-wrap instead of the text going over to the right)?
It's similar to the built-in SMS application where we can't input newline but the text is displayed in multiple lines.
I would subclass the widget and override the key event handling in order to block the Enter key:
class MyTextView extends EditText
{
...
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode==KeyEvent.KEYCODE_ENTER)
{
// Just ignore the [Enter] key
return true;
}
// Handle all other keys in the default way
return super.onKeyDown(keyCode, event);
}
}
This is a method, where you don't have to override the EditText class. You just catch and replace the newlines with empty strings.
edittext.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void afterTextChanged(Editable s) {
/*
* The loop is in reverse for a purpose,
* each replace or delete call on the Editable will cause
* the afterTextChanged method to be called again.
* Hence the return statement after the first removal.
* http://developer.android.com/reference/android/text/TextWatcher.html#afterTextChanged(android.text.Editable)
*/
for(int i = s.length()-1; i >= 0; i--){
if(s.charAt(i) == '\n'){
s.delete(i, i + 1);
return;
}
}
}
});
Credit to Rolf for improvement on an earlier answer.
Property in XML
android:lines="5"
android:inputType="textPersonName"
This one works for me:
<EditText
android:inputType="textShortMessage|textMultiLine"
android:minLines="3"
... />
It shows a smiley instead of the Enter key.
Here's a more correct answer that does not display the enter key on the IME keyboard:
// IMPORTANT, do this before any of the code following it
myEditText.setSingleLine(true);
// IMPORTANT, to allow wrapping
myEditText.setHorizontallyScrolling(false);
// IMPORTANT, or else your edit text would wrap but not expand to multiple lines
myEditText.setMaxLines(6);
Also, you may replace setSingleLine(true) with either, an explicit android:inputType on the XML layout file, or setInputType(InputType.*) on code – in which, the input type used, is anything that you know restricts the input to single lines only (i.e., anything that calls setSingleLine(true) implicitly already).
Explanation:
What setSingleLine(true) does is calling setHorizontallyScrolling(true) and setLines(1) implicitly, alongside with altering some IME keyboard settings to disable the enter key.
In turn, the call to setLines(1) is like calling setMinLines(1) and setMaxLines(1) in one call.
Some input types (i.e., the constants from InputType.TYPE_*) calls setSingleLine(true) implicitly, or at least achieves the same effect.
Conclusion:
So to achieve what the OP wants, we simply counter those implicit settings by reverting those implicit calls.
The answer provided by #Andreas Rudolph contains a critical bug and shouldn't be used. The code causes an IndexOutOfBoundsException when you past text inside the EditText which contains multiple new-line characters. This is caused by the type of loop which is used, the Editable object will call the afterTextChanged method as soon as its contents change (replace, delete, insert).
Correct code:
edittext.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void afterTextChanged(Editable s) {
/*
* The loop is in reverse for a purpose,
* each replace or delete call on the Editable will cause
* the afterTextChanged method to be called again.
* Hence the return statement after the first removal.
* http://developer.android.com/reference/android/text/TextWatcher.html#afterTextChanged(android.text.Editable)
*/
for(int i = s.length()-1; i >= 0; i--){
if(s.charAt(i) == '\n'){
s.delete(i, i + 1);
return;
}
}
}
});
I'm testing this and it seems to work:
EditText editText = new EditText(context);
editText.setSingleLine(false);
editText.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_EMAIL_SUBJECT);
Try this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER)
{
//Nothing
return true;
}
return super.onKeyDown(keyCode, event);
}
You can set it from the xml like this:
android:imeOptions="actionDone"
android:inputType="text"
android:maxLines="10"
don't forget android:inputType="text", if you don't set it, it doesn't work. I don't know why though.
Also don't forget to change maxLines to your preferred value.
Simply add
android:singleLine="true"
to your EditText
Here is the solution....
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="150"
android:textSize="15dp"
android:imeOptions="actionDone"
android:inputType="text|textMultiLine"/>
Usage in java class
editText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_ENTER)
{
// Just ignore the [Enter] key
return true;
}
// Handle all other keys in the default way
return (keyCode == KeyEvent.KEYCODE_ENTER);
}
});
The accepted answer worked so well until I copied text with line-breaks into into the EditText. So I added onTextContextMenuItem to monitor the paste action.
#Override
public boolean onTextContextMenuItem(int id) {
boolean ret = super.onTextContextMenuItem(id);
switch (id) {
case android.R.id.paste:
onTextPaste();
break;
}
return ret;
}
public void onTextPaste() {
if (getText() == null)
return;
String text = getText().toString();
text = text.replaceAll(System.getProperty("line.separator"), " ");
text = text.replaceAll("\\s+", " ");
setText(text);
}
You can change the action button from code
editText.imeOptions = EditorInfo.IME_ACTION_DONE
editText.setRawInputType(InputType.TYPE_CLASS_TEXT)
Xml
android:inputType="textMultiLine"
<EditText
android:id="#+id/Msg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:lines="5"
android:selectAllOnFocus="true"
android:hint="Skriv meddelande...\n(max 100tkn)"/>
EditText et = (EditText)findViewById(R.id.Msg);
String strTmp = et.getText().toString();
strTmp = strTmp.replaceAll("\\n"," ");
EditText textView = new EditText(activity);
...
textView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if(KeyEvent.KEYCODE_ENTER == keyEvent.getKeyCode()) {
return false;
}
.......
}
});
For a URI you could use:
android:inputType="textUri"
android:lines="1"
android:maxLength="128"
Otherwise android:inputType="textPersonName" as mentioned above works for other EditText such user name, etc.
I will give another option so you don't have to subclass EditText. Create an InputFilter that filters out linebreaks. Then use EditText.addInputFilter.
Source code for such an input filter is here: https://gist.github.com/CapnSpellcheck/7c72830e43927380daf5205100c93977
You can pass 0 in the constructor, and it won't allow any newlines. Also, you can combine this with one of the other tweaks such as android:imeOptions="actionDone", as this will help improve the experience on some devices.
Adding this property to the EditText XML works for me:
android:lines="1"
It lets the users input newline characters but the EditText itself does not increase in height.

Prevent enter key on EditText but still show the text as multi-line

How do I make an EditText on Android such that the user may not enter a multi-line text, but the display is still multi-line (i.e. there is word-wrap instead of the text going over to the right)?
It's similar to the built-in SMS application where we can't input newline but the text is displayed in multiple lines.
I would subclass the widget and override the key event handling in order to block the Enter key:
class MyTextView extends EditText
{
...
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode==KeyEvent.KEYCODE_ENTER)
{
// Just ignore the [Enter] key
return true;
}
// Handle all other keys in the default way
return super.onKeyDown(keyCode, event);
}
}
This is a method, where you don't have to override the EditText class. You just catch and replace the newlines with empty strings.
edittext.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void afterTextChanged(Editable s) {
/*
* The loop is in reverse for a purpose,
* each replace or delete call on the Editable will cause
* the afterTextChanged method to be called again.
* Hence the return statement after the first removal.
* http://developer.android.com/reference/android/text/TextWatcher.html#afterTextChanged(android.text.Editable)
*/
for(int i = s.length()-1; i >= 0; i--){
if(s.charAt(i) == '\n'){
s.delete(i, i + 1);
return;
}
}
}
});
Credit to Rolf for improvement on an earlier answer.
Property in XML
android:lines="5"
android:inputType="textPersonName"
This one works for me:
<EditText
android:inputType="textShortMessage|textMultiLine"
android:minLines="3"
... />
It shows a smiley instead of the Enter key.
Here's a more correct answer that does not display the enter key on the IME keyboard:
// IMPORTANT, do this before any of the code following it
myEditText.setSingleLine(true);
// IMPORTANT, to allow wrapping
myEditText.setHorizontallyScrolling(false);
// IMPORTANT, or else your edit text would wrap but not expand to multiple lines
myEditText.setMaxLines(6);
Also, you may replace setSingleLine(true) with either, an explicit android:inputType on the XML layout file, or setInputType(InputType.*) on code – in which, the input type used, is anything that you know restricts the input to single lines only (i.e., anything that calls setSingleLine(true) implicitly already).
Explanation:
What setSingleLine(true) does is calling setHorizontallyScrolling(true) and setLines(1) implicitly, alongside with altering some IME keyboard settings to disable the enter key.
In turn, the call to setLines(1) is like calling setMinLines(1) and setMaxLines(1) in one call.
Some input types (i.e., the constants from InputType.TYPE_*) calls setSingleLine(true) implicitly, or at least achieves the same effect.
Conclusion:
So to achieve what the OP wants, we simply counter those implicit settings by reverting those implicit calls.
The answer provided by #Andreas Rudolph contains a critical bug and shouldn't be used. The code causes an IndexOutOfBoundsException when you past text inside the EditText which contains multiple new-line characters. This is caused by the type of loop which is used, the Editable object will call the afterTextChanged method as soon as its contents change (replace, delete, insert).
Correct code:
edittext.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void afterTextChanged(Editable s) {
/*
* The loop is in reverse for a purpose,
* each replace or delete call on the Editable will cause
* the afterTextChanged method to be called again.
* Hence the return statement after the first removal.
* http://developer.android.com/reference/android/text/TextWatcher.html#afterTextChanged(android.text.Editable)
*/
for(int i = s.length()-1; i >= 0; i--){
if(s.charAt(i) == '\n'){
s.delete(i, i + 1);
return;
}
}
}
});
I'm testing this and it seems to work:
EditText editText = new EditText(context);
editText.setSingleLine(false);
editText.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_EMAIL_SUBJECT);
Try this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER)
{
//Nothing
return true;
}
return super.onKeyDown(keyCode, event);
}
You can set it from the xml like this:
android:imeOptions="actionDone"
android:inputType="text"
android:maxLines="10"
don't forget android:inputType="text", if you don't set it, it doesn't work. I don't know why though.
Also don't forget to change maxLines to your preferred value.
Simply add
android:singleLine="true"
to your EditText
Here is the solution....
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="150"
android:textSize="15dp"
android:imeOptions="actionDone"
android:inputType="text|textMultiLine"/>
Usage in java class
editText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_ENTER)
{
// Just ignore the [Enter] key
return true;
}
// Handle all other keys in the default way
return (keyCode == KeyEvent.KEYCODE_ENTER);
}
});
The accepted answer worked so well until I copied text with line-breaks into into the EditText. So I added onTextContextMenuItem to monitor the paste action.
#Override
public boolean onTextContextMenuItem(int id) {
boolean ret = super.onTextContextMenuItem(id);
switch (id) {
case android.R.id.paste:
onTextPaste();
break;
}
return ret;
}
public void onTextPaste() {
if (getText() == null)
return;
String text = getText().toString();
text = text.replaceAll(System.getProperty("line.separator"), " ");
text = text.replaceAll("\\s+", " ");
setText(text);
}
You can change the action button from code
editText.imeOptions = EditorInfo.IME_ACTION_DONE
editText.setRawInputType(InputType.TYPE_CLASS_TEXT)
Xml
android:inputType="textMultiLine"
<EditText
android:id="#+id/Msg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:lines="5"
android:selectAllOnFocus="true"
android:hint="Skriv meddelande...\n(max 100tkn)"/>
EditText et = (EditText)findViewById(R.id.Msg);
String strTmp = et.getText().toString();
strTmp = strTmp.replaceAll("\\n"," ");
EditText textView = new EditText(activity);
...
textView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if(KeyEvent.KEYCODE_ENTER == keyEvent.getKeyCode()) {
return false;
}
.......
}
});
For a URI you could use:
android:inputType="textUri"
android:lines="1"
android:maxLength="128"
Otherwise android:inputType="textPersonName" as mentioned above works for other EditText such user name, etc.
I will give another option so you don't have to subclass EditText. Create an InputFilter that filters out linebreaks. Then use EditText.addInputFilter.
Source code for such an input filter is here: https://gist.github.com/CapnSpellcheck/7c72830e43927380daf5205100c93977
You can pass 0 in the constructor, and it won't allow any newlines. Also, you can combine this with one of the other tweaks such as android:imeOptions="actionDone", as this will help improve the experience on some devices.
Adding this property to the EditText XML works for me:
android:lines="1"
It lets the users input newline characters but the EditText itself does not increase in height.

Categories

Resources