Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ellipsize content of editText when disabled

I have a layout in which an EditText is filled by the user, then later on it becomes disabled to act as standard TextView.

Problem is, the ellipsize never works on this EditText. I'd like it to display "..." at the end of the text when it is too large to be displayed completely, but I can't find any way to make it works, and I have no idea why it doesn't.

Here is my layout

<RelativeLayout
android:id="@+id/search_container"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/toolbar"
android:background="@drawable/bg_border_bot_clickable"
android:addStatesFromChildren="@+id/ic_edit"
android:animateLayoutChanges="true">

    <View
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="5dp"
        android:layout_alignParentLeft="true"
        android:background="@drawable/ic_action_search"
        android:id="@+id/ic_search" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:textColorHint="@color/text_hint"
        android:hint="@string/search_hint"
        android:id="@+id/et_search"
        android:textColor="@android:color/white"
        android:inputType="text|textCapSentences"
        android:ellipsize="end"
        android:singleLine="true"
        android:lines="1"
        android:imeOptions="actionSearch"
        android:background="@drawable/transition_rounded_rectangle"
        android:layout_toLeftOf="@+id/ic_edit"
        android:layout_toRightOf="@+id/ic_search"
        />

<View
    android:layout_width="45dp"
    android:layout_height="45dp"
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"
    android:background="@drawable/ic_edit"
    android:id="@+id/ic_edit"
    android:visibility="gone"
    android:clickable="true"/>

Any ideas on how to accomplish that?

like image 279
Gyome Avatar asked Dec 14 '22 14:12

Gyome


2 Answers

Add the following attributes to your EditText to wrap the content with ellipsis.

android:scrollHorizontally="true"
android:ellipsize="end"
android:singleLine="true"
android:editable="false"

if you don't want to set editable to false by default in your XML, you can achieve the same result by calling setKeyListener(null).

editText.setKeyListener(null);
like image 166
Christian Abella Avatar answered Dec 17 '22 04:12

Christian Abella


This is my solution(I've worked it out):

xml:

<androidx.appcompat.widget.AppCompatEditText
    android:id="@+id/et2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginHorizontal="10dp"
    android:layout_marginTop="10dp"
    android:background="@drawable/selector_btn_check_red_grey"
    android:hint="Please enter characters"
    android:inputType="text"
    android:lines="1"
    android:paddingHorizontal="10dp"
    android:paddingVertical="5dp"
    android:text="就会角度打开的事开发的急啊看手机发就卡的房多少"
    android:textAllCaps="false"
    android:textColor="@color/red_500"
    android:textSize="15sp" />

java:

    //Initialize to non-editable state
    et2.setKeyListener(null);
    et2.setEllipsize(TextUtils.TruncateAt.END);//setEllipsize只对hint有效果

    et2.setOnFocusChangeListener((v, hasFocus) -> {
        if (hasFocus) {
            //Editable status
            et2.setEllipsize(null);
            et2.setKeyListener(new TextKeyListener(TextKeyListener.Capitalize.NONE, false));
        } else {
            //Not editable[enter image description here][1]
            et2.setKeyListener(null);
            et2.setEllipsize(TextUtils.TruncateAt.END);
        }
    });
like image 21
sindi.chen Avatar answered Dec 17 '22 02:12

sindi.chen