46

Whenever a word is typed in the EditText box, I always see an underline under the word being typed. But when I press a space after that word I no longer see the underline.

My reqirement is to remove that underline when the user is typing the message.

Added is the screenshot and we see that Smith is underlined. But I don't want this to happen.

Below is the xml that I use for the AlertDialog box.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView 
    android:id="@+id/name_view"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_marginLeft="20dip"
    android:layout_marginRight="20dip"
    android:text="@string/alert_dialog_name"
    android:gravity="left"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/username_edit"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_marginLeft="20dip"
    android:layout_marginRight="20dip"
    android:scrollHorizontally="true"
    android:autoText="false"
    android:inputType="textPersonName"
    android:capitalize="none"
    android:gravity="fill_horizontal"
    android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

enter image description here

Sana
  • 9,895
  • 15
  • 59
  • 87
  • 1
    The appearance of words currently being typed (at least with autocomplete on) varies from device to device. On HTC SenseUI on Android 2.2, for example, the current word under autocomplete consideration is shown with a green highlight. Why would you want to mess with what the user expects to see on the particular device/keyboard they're used to? – Yoni Samlan Mar 31 '11 at 18:25
  • I just want to use whatever the user types and send it across to the server, I don't want any logic to be written to remove the last underlined word. – Sana Apr 01 '11 at 03:18
  • 1
    This is IME-specific. If you want the IME to interfere as little as possible, you can use a different `inputType`, but in this case, you're using the right type, and it does what it thinks is best for this particular type of input, and that's what the user will expect too. – EboMike Apr 01 '11 at 08:29
  • 1
    You don't have to use any logic to remove the underlines -- just call getText().toString() when you want to use the value. It won't include special formatting or anything. – Yoni Samlan Apr 01 '11 at 14:47
  • 1
    OK, I added it as an answer so you can accept it, although I don't really consider it much of a problem or a solution -- it's just "ignore what it looks like and treat it like any normal text input." – Yoni Samlan Apr 01 '11 at 19:21

9 Answers9

66

android:inputType="textNoSuggestions"

sgibly
  • 3,828
  • 1
  • 21
  • 21
  • Is it possible to still have suggestions, but to avoid the underline of the current word? – android developer Feb 11 '18 at 14:53
  • I assume that for that, you may need to override the span behavior. I think that those underlines are coming from the SuggestionSpan. This is just an idea, which I have not tried :) 1. On the Editable, call setSpan with a new custom span that implements SpanWatcher. Set it from zero to length. 2. Override onSpanAdded at the SpanWatcher to check for SuggestionSpan types. 3. Remove any SuggestionSpan and replace it with your own class that extends from it and is initialized with the same suggestions and flags. 4. Override updateDrawState to set different color (or a transparent one). – sgibly Feb 14 '18 at 21:21
  • Unfortunately, I could not find other ways to control the suggestions colors at the SuggestionSpan. But there may be some other way I'm not aware of. – sgibly Feb 14 '18 at 21:23
22

There is a function that removes any composing state. I think if you call it after every time a user types, you will not see the underline. I use it after the user finishes typing, to get the drawing cache of the entire textView (which I need without underline). It's

someTextView.clearComposingText();
n00b programmer
  • 2,671
  • 7
  • 41
  • 56
  • Thanks, it works, but it is difficult to use in `TextWatcher.afterTextChanged`, because `EditText` blinks during typing and after finish editing it again adds underline. Use timeouts during type, see https://stackoverflow.com/a/47842636/2914140. – CoolMind Oct 08 '19 at 15:28
12

accepted answer is not given solution, and some other user given answer but no one given full anser, so that's why i write here working solution.

If you want to remove underline then just add below code.

XML

android:inputType="textNoSuggestions"

Java

EditText ed;
ed = findViewById(yourId);
ed.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

If you want to set more than one input type then,

ed.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

May be above information is helpful to others.

Yogesh Rathi
  • 6,331
  • 4
  • 51
  • 81
12

You don't have to use any logic to remove the underlines -- just call getText().toString() when you want to use the value. It won't include special formatting or anything.

Yoni Samlan
  • 37,905
  • 5
  • 60
  • 62
8

The best solution for this is to add on your EditText:

 android:inputType="textMultiLine|textVisiblePassword"
Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44
LIAM Tech
  • 81
  • 1
  • 1
  • 1
    Hi, welcome to Stack Overflow and thanks for your answer. Great answers also include an explanation of what the fault was with the original question and why this answer addresses it. – Spangen Feb 23 '18 at 11:56
3

Use this from your class, if EditText view is dynamic (created from class file):

EditText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

OR include android:inputType="textNoSuggestions" with your EditText in XML.

Gaurav
  • 491
  • 2
  • 8
  • 17
2

Read this if you want to keep emojis and textNoSuggestions doesn't work for you.

textNoSuggestions does not work in every keyboard. inputType="textVisiblePassword" works but it removes emoji's from most keyboards.

I found that setting inputType="textUri" works and keeps the ability to add emojis.

P Fuster
  • 2,224
  • 1
  • 20
  • 30
0

try:

android:inputType= InputTypes.TextVariationVisiblePassword;
sKhan
  • 9,694
  • 16
  • 55
  • 53
asaf
  • 958
  • 1
  • 16
  • 38
-2
<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:imeOptions="actionDone"
        android:inputType="textNoSuggestions"
        android:maxLines="1"
        />