EditText Hint Text and Icon

2019-07-01 14:31发布

I am trying to create an EditText with the hint as icon and text both together. But hint text goes to the center but I want hint text to be left aligned so that there should only be a tab space (gap) between hint icon and hint text.

This is what I have tried:

<EditText
        android:id="@+id/emailAddressInput"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:ems="10"
        android:layout_marginTop ="20dp"
        android:layout_marginLeft ="20dp"
        android:layout_marginRight ="20dp"
        android:background="@drawable/rounded_edge"
        android:drawableLeft="@drawable/email_box"
        android:hint="e-mail address"
        android:gravity="left|center_vertical"
        android:maxLength="100"
        android:inputType="textEmailAddress" />

Any idea how can I achieve desired result?

3条回答
该账号已被封号
2楼-- · 2019-07-01 14:47

ImageSpan is the best way to do this.

But you got to do it through Java code.

ImageSpan imageHint = new ImageSpan(mContext, R.drawable.icon_hint);
SpannableString spannableString = new SpannableString("_I'm the hint");
spannableString.setSpan(imageHint, 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mEditor.setHint(spannableString);
查看更多
Animai°情兽
3楼-- · 2019-07-01 14:55

Add

android:gravity="top"

to your EditText.

查看更多
Root(大扎)
4楼-- · 2019-07-01 15:00

Okay so after a little debugging, I found out that the image size was taking that much space, I have cropped the image to smaller size and it worked.

查看更多
登录 后发表回答