6

I have EditText with custom background drawable:

enter image description here

EditText code:

<EditText
    android:id="@+id/etName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@{ViewModel.isAllowEdit  ? @drawable/profile_et_background_active : @drawable/profile_et_background}"
    android:inputType="@{ViewModel.isAllowEdit ? InputType.TYPE_CLASS_TEXT : InputType.TYPE_NULL}"
    android:text="@={ViewModel.name}"
    android:textColor="@color/main_dark_text_color" />

I'm using android databinding library and MVVM architecture.

If ViewModel has isAllowEdit set to true than EditText background set to @drawable/profile_et_background_active.

If isAllowEdit false EditText has background set to @drawable/profile_et_background.

Also i'm disallow edit by setting inputType to TYPE_NULL, and allow edit by setting inputType to TYPE_CLASS_TEXT.

@drawable/profile_et_background_active code:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="rectangle">
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>

    <item
        android:left="-2dp"
        android:right="-2dp"
        android:top="-2dp">
        <shape>
            <solid android:color="@android:color/transparent" />
            <stroke
                android:width="1dp"
                android:color="@color/main_elements_line_color" />
        </shape>
    </item>

</layer-list>

@drawable/profile_et_background code:

<item>
    <shape android:shape="rectangle">
        <solid android:color="@android:color/transparent" />
    </shape>
</item>

When edit is allowed and user start typing text in EditText additional underline appears under typed word (it belongs only to currently typed word, all other parts of EditText text has no underline):

enter image description here

I tried to remove that underline by adding color filter to EditText:

et.setColorFilter(getResources().getColor(android.R.color.transparent), PorterDuff.Mode.SRC_IN)

But it doesn't work.

How can i remove that extra underline ?

UPDATE 1

I already tried to add @android:color/transparent, and I'm getting error:

"java.lang.Integer cannot be cast to android.graphics.drawable.Drawable"

when changing "@{ViewModel.isAllowEdit ? @drawable/profile_et_background_active : @drawable/profile_et_background}"

to "@{ViewModel.isAllowEdit ? @drawable/profile_et_background_active : @android:color/transparent}"

UPDATE 2

Adding InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS does not work for me. So i guess this is not Spell Checker's problem.

Vlad Morzhanov
  • 1,260
  • 3
  • 14
  • 29

6 Answers6

11

The underline text styling is applied by the BaseInputConnection of EditText to the text currently being "composed" using the styling applied by the theme attribute android:candidatesTextStyleSpans, which by default is set to the string <u>candidates</u>.

The text part of the string is ignored, but the style spans are extracted from the string and applied to "composing" text which is the word the user is currently typing, a.o. to indicate that suggestions can be selected or that autocorrect is active.

You can change that styling (e.g. to use bold or italics instead of underlines), or remove the styling altogether, by setting the theme attribute to a styled or unstyled string:

<!-- remove styling from composing text-->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- ... -->
    <item name="android:candidatesTextStyleSpans">candidates</item>
</style>

<!-- apply bold + italic styling to composing text-->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- ... -->
    <item name="android:candidatesTextStyleSpans"><b><i>candidates</i></b></item>
</style>

Caveat: Removing all styling will cause the BaseInputConnection implementation to re-evaluate the theme attribute on every change of text, as the span information is lazy loaded and persisted only if the attribute is set to a styled string. You could alternatively set any other styling as is supported by Html:fromHtml(...), e.g. <span style="color:#000000">...</span> to the default text color, which makes no difference in display.

Thorstenvv
  • 5,376
  • 1
  • 34
  • 28
6

I have seen all the valid answers given above. But at the last try You can something like:

 mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mEditText.clearComposingText();
                    }
                },200);


            }
        });

ClearComposingText method will help you. Hope this helps.

  • Thanks! `clearComposingText()` has been gotten from https://stackoverflow.com/a/15055413/2914140. If you type fast, it won't hide underline. On emulator, if you type fast, it will double blocks of text. – CoolMind Oct 08 '19 at 15:35
2

Use inputType textNoSuggestions in xml.

  <EditText
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:inputType="text|textNoSuggestions"/>

InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS is equivalent flag for this using code

ADM
  • 20,406
  • 11
  • 52
  • 83
  • 1
    Adding InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS does not work for me. So i guess this is not Spell Checker's problem. – Vlad Morzhanov Dec 15 '17 at 15:08
  • It is spell checker problem .what else it can be ?? I have tested it and it worked in my case – ADM Dec 15 '17 at 16:32
  • Did you set input type with code ? Try to set it with code not xml . Then check . If you already checked it then this must be some problem with your background drawable. – ADM Dec 15 '17 at 16:35
  • 1
    Edittext is a big ol'mess. Trying to make it work with dynamic labels and custom borders as well as removing text underline is clear that Google have not thought it through and just threw it over the fence for commercial developers to work out. – angryITguy Sep 11 '18 at 02:24
  • 1
    even after using android:inputType="text|textNoSuggestions i am getting underline can someone help on it – Ajinkya kadam Feb 11 '20 at 11:22
1

As per my understanding. Use this in your editText

android:background="@android:color/transparent"

And If you want to stop Spell Checker for Text which you had typed then use

android:inputType="textNoSuggestions"
coder_baba
  • 447
  • 3
  • 21
  • 2
    OP didn't ask to remove the widget's underline but the one which appears under the text. – Jay Dec 15 '17 at 11:39
  • I already tried this, and I'm getting error "java.lang.Integer cannot be cast to android.graphics.drawable.Drawable" when changing "@{ViewModel.isAllowEdit ? @drawable/profile_et_background_active : @drawable/profile_et_background}" to "@{ViewModel.isAllowEdit ? @drawable/profile_et_background_active : @android:color/transparent}" – Vlad Morzhanov Dec 15 '17 at 11:40
  • @VladMorzhanov If I am right then R u getting underline as Spell Checker ? – coder_baba Dec 15 '17 at 11:43
0

This will help You For Sure. Just make changes to Theme style.

<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="textInputStyle">@style/Widget.MyApp.TextInputLayout</item>
</style>

<style name="Widget.MyApp.TextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
    <item name="boxStrokeWidth">0dp</item>
    <item name="boxStrokeWidthFocused">0dp</item>
</style>
0

Use android:inputType = "textVisiblePassword" to remove the extra underline comes under text

And Use android:background="@android:color/transparent" to remove EditText's bottom underline

Sumit Jain
  • 1,100
  • 1
  • 14
  • 27