Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center Drawable and Text in EditText

I want to make drawable and text center in my EditText. The text is centered but the drawable is not. I want something like this: http://i.imgur.com/cacxR2C.jpg?1

Please help. This is what I have done so far

 <EditText 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Username"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:gravity="center_horizontal"
    android:drawableLeft="@drawable/lol2"
    />

When the user types in something in the EditText, the drawable should move itself to the left if the text is lenghty.

like image 497
Shaw Avatar asked Mar 04 '15 11:03

Shaw


1 Answers

You can do something like this

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:background="@android:drawable/editbox_background"
        >

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Username"
            android:background="@null"
            android:gravity="center"
            android:drawableLeft="@drawable/ic_launcher"
            />
    </RelativeLayout>

enter image description hereenter image description here

like image 51
Nauman Afzaal Avatar answered Oct 14 '22 05:10

Nauman Afzaal