Search code examples
androidattributestextviewandroid-custom-viewmarquee

Get the value of android:ellipsize attribute, in a custom view constructor


How to get the value of the attribute android:ellipsize in a custom View (that extends TextView)?

I tried these ways, but didn't work:

Log.d(TAG, attrs.getAttributeIntValue(ANDROID_NAMESPACE, "ellipsize", -1));
Log.d(TAG, attrs.getAttributeValue(ANDROID_NAMESPACE, "ellipsize"));
Log.d(TAG, attrs.getAttributeUnsignedIntValue(ANDROID_NAMESPACE, "ellipsize", 111));
Log.d(TAG, attrs.getAttributeListValue(ANDROID_NAMESPACE, "ellipsize", new String[] {"x", "marquee"}, -1));

Note: The attrs is the AttributeSet (constructor parameter) and ANDROID_NAMESPACE is "http://schemas.android.com/apk/res/android".


Solution

  • The getAttributeValue method works for me in this example (Kotlin):

    import android.content.Context
    import android.util.AttributeSet
    import android.widget.TextView
    
    class Text(context: Context, attrs: AttributeSet) : TextView(context, attrs) {
        init {
            println("ellipsize: " +
                    attrs.getAttributeValue(
                            "http://schemas.android.com/apk/res/android",
                            "ellipsize"
                    )
            )
        }
    }
    

    It prints:

    11-20 20:49:18.808 26804-26804/com.package.app I/System.out: ellipsize: 1

    for this view:

    <com.package.app.Text
            android:id="@+id/test"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="start" />