I want to customize android edit text so that when clicked outside, it will automatically scroll back to first and show three dots at the end if the text is too long. I have customized it to try it to run well but not yet optimized, please refer to it and if possible, give me a more optimal way.
class CustomTextInputEditText(context: Context, attrs: AttributeSet) :
TextInputEditText(context, attrs) {
private var dotsString: String? = null
private var storeString: String? = null
private var mWidthMeasureSpec: Int = 0
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
Log.e("CustomTextInputEditText", "onMeasure 1: $mWidthMeasureSpec")
mWidthMeasureSpec = MeasureSpec.getSize(widthMeasureSpec)
}
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
Log.e("CustomTextInputEditText", "onMeasure 2: $mWidthMeasureSpec")
mWidthMeasureSpec = width
setText(text, BufferType.NORMAL)
}
override fun setText(text: CharSequence?, type: BufferType) {
if (!isFocused) {
if (!text.isNullOrEmpty()) {
val tmpString = text.toString()
val textWidth = this.paint.measureText(tmpString)
Log.e("CustomTextInputEditText", " textWidth = " + textWidth)
Log.e("CustomTextInputEditText", " mWidthMeasureSpec = " + mWidthMeasureSpec)
storeString = tmpString
dotsString = tmpString
if (mWidthMeasureSpec > 0 && mWidthMeasureSpec <= textWidth + context.convertDpToPx(69f)) {
if (dotsString?.length!! >= (mWidthMeasureSpec / textWidth * tmpString.length - 5).toInt() && (mWidthMeasureSpec / textWidth * tmpString.length) >= 5)
dotsString = tmpString.substring(0, (mWidthMeasureSpec / textWidth * tmpString.length - 5).toInt()) + "..."
}
Log.e("CustomTextInputEditText", "setText: $dotsString")
}
Log.e("CustomTextInputEditText", "not focus: $dotsString")
super.setText(dotsString, type)
} else {
Log.e("CustomTextInputEditText", "focus: $dotsString")
super.setText(storeString, type)
}
}
override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
super.onFocusChanged(focused, direction, previouslyFocusedRect)
if (focused) {
setText(storeString)
} else {
val tmpString = text.toString()
val textWidth = this.paint.measureText(tmpString)
storeString = tmpString
if (mWidthMeasureSpec <= textWidth) {
dotsString = tmpString.substring(
0,
(mWidthMeasureSpec / textWidth * tmpString.length - 10).toInt()
) + "...";
setText(dotsString)
}
}
}
fun getRawText(): String {
if (storeString == null)
return ""
else
return storeString!!
}
}
My xml
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/til_select_address"
style="@style/TextInputLayoutStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="@dimen/margin_10"
android:paddingTop="@dimen/margin_16"
android:hint="@string/select_address"
android:paddingEnd="@dimen/margin_10">
<vn.leaftech.f99fruits.utils.custom_view.CustomTextInputEditText
android:id="@+id/edt_select_address"
style="@style/TextInputEditTextLayoutStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textCursorDrawable="@null"
android:paddingEnd="@dimen/margin_24"
android:ellipsize="end"
android:lines="1"
android:text=""
android:maxLines="1"
android:focusable="false"
android:fontFamily="@font/opensans_regular"
android:scrollHorizontally="true"
android:textSize="@dimen/margin_16"/>
</com.google.android.material.textfield.TextInputLayout>
I tried the code bellow but it doesn't work:
android:lines="1"
android:scrollHorizontally="true"
android:ellipsize="end"
android:singleLine="true"
android:editable="false"