EditText ellipsize (three dots...)
Asked Answered
E

7

16

Unfortunatelly I am not able to make ellipsize for EditText works. Is even possible to put three dots at the end of the text when the text is too long? It is working perfectly for TextiView but not for EditText. Some idea?

 android:id="@+id/ed_email_personalInfo"
 android:layout_width="match_parent"
 android:layout_height="55dp"
 android:background="@color/transparent"
 android:ellipsize="end"
 android:ems="10"
 android:hint="@string/email"
 android:inputType="textEmailAddress"
 android:maxLength="64"
 android:paddingLeft="10dp"
 android:paddingRight="10dp"
 android:singleLine="true"
 android:textColorHint="@color/col9a9a9a"
 android:textSize="15.7sp"
Ethnarch answered 9/10, 2013 at 15:31 Comment(1)
recently I faced the same problem, so I developed this solution (https://mcmap.net/q/747505/-ellipsize-content-of-edittext-when-disabled) which works as expected (adding ellipses) on Android versions starting from API 23 (Android Marshmallow).Volga
K
23

Set this property to edit text. Elipsize is working with disable edit text

    android:lines="1"
    android:scrollHorizontally="true"
    android:ellipsize="end"
    android:singleLine="true"
    android:editable="false"
Kitkitchen answered 31/12, 2013 at 12:13 Comment(2)
It works. And there is no need to set scrollHorizontally and lines properties.Outgoings
android:editable property is deprecated and Google recommends to use android:inputType. However, according to my test, it would not work any more if there exists android:inputTypeCongdon
D
23

You have to remove android:inputType attribute.

Ellipsize doesn't work if inputType is defined.

Darbee answered 18/8, 2015 at 12:13 Comment(4)
Perfect, following attributes with the above answer worked - android:ellipsize="end" android:singleLine="true" android:editable="false"Redford
This answer does not work. It is also ridiculous if it did because why would Android show ellipsis based on the input type?Bulldog
@Bulldog undocumented feature b̶u̶g̶ in Android?Darbee
This is the key answer to those who can't ellipsize an EditText despite setting all relevant properties. Once you set inputType, it overrides the ellipsize property. Moreover, the default behaviour of a single-line EditText is that you can scroll horizontally when the text doesn't fit in the view.Jeseniajesh
M
6

I'm not sure it answers the question (and I guess the original asker is not so interested anymore, after 7 and a half years), but I'm pretty confident my answer will come in handy to anyone stumbling into ellipsis issues with EditText.

Don't try to find any logic in this, but based on the answers from Oleksandr and Mubarak, I figured out the following:

To ellipsize an EditText in XML, use deprecated android:editable="false". In my case:

android:editable="false"
android:focusable="false"
android:singleLine="true"
android:maxLines="1"
android:ellipsize="end"

No need to mess with android:inputType="none" for the moment. The EditText looks just like a TextView and is properly ellipsized.

It is possible to make the EditText editable (and remove the ellipsis) like this:

editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.setActivated(true);
editText.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS); // or whatever fits your needs
editText.setSingleLine(true);
editText.setEllipsize(null);
editText.requestFocus();

Your can then restore the EditText to its original state (ie. disguised as a TextView and properly ellipsized) like this:

editText.setFocusable(false);
editText.setFocusableInTouchMode(false);
editText.setActivated(false);
editText.setSingleLine(true);
editText.setKeyListener(null); // this is the key
editText.setEllipsize(TextUtils.TruncateAt.END);
Meenen answered 30/4, 2021 at 9:1 Comment(0)
S
3

Might not be possible in EditText (unless you create your own View). I think the default behavior (for singleLine EditText) is that you can scroll the text sideways when it can't fit in the view.

Sempiternal answered 10/10, 2013 at 3:34 Comment(0)
N
0

EditText is subclass of TextView, you can override edittext method to make it work like textview I try this and it work

class CustomTextInputEditTextView: TextInputEditText {
constructor(context: Context, attributeSet: AttributeSet?): super(context, attributeSet) {

}

override fun getDefaultEditable(): Boolean {
    return false
}

override fun getDefaultMovementMethod(): MovementMethod? {
    return null
}

}

<com.CustomTextInputEditTextView
            android:cursorVisible="false"
            android:ellipsize="end"
            android:lines="1"
            android:inputType="none" />
Nightwalker answered 2/3, 2023 at 7:21 Comment(0)
T
0

An alternative would be to overlay the EditText view with an ellipsizable, non-transparent TextView containing the same text. When you tap the TextView, it hides the TextView overlay and passes the click event to the EditText. On completion, refresh the TextView with the new text and make it visible (and ellipsized) again.

Thordis answered 18/10, 2023 at 2:29 Comment(0)
W
-3

You need write a new class that extends EditText. for example:

MyEditTextEllipsize extends EditText{

private String dotsString;

private String storeString;

public MyEditTextEllipsize(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

@Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);

         if(focused)

     {
       setText(storeString);
      }else {
             String NOW = getText().toString();
                storeString = NOW;
            if (NOW != null && getWidth() <= getTextSize() * NOW.length()) {

                    dotsString = NOW.substring(0, (int) (getWidth() / getTextSize())) + "...";

                    setText(dotsString);

                }
}

    }
}
Wimbush answered 1/12, 2014 at 3:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.