Search code examples
android-textinputlayoutandroid-textinputedittext

How can I change the position of drawable in TextInputEditText?


I set a drawable in a TextInputEditText with attribute android:drawableStart, and it looks like this:

drawable default

But actually, I need to put it in this way:

The drawable in the way I want

It seems to there's no attribute in xml to do this, I review the properties and nothing seems to fit what I want. Also, I've been looking for a while on Internet but I don´t get the information that I need to achieve this. Any idea???


Solution

  • After keep searching on Internet, I was able to find a solution. First of all at this post, it suggests in the accepted answer, that it is not possible to place the icon at the top, however, you have to wrap the EditText inside a LinearLayout and he place the example code below:

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@android:drawable/edit_text"
    android:orientation="horizontal">
    
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@android:drawable/ic_menu_send"
        android:scaleType="fitStart"
        android:contentDescription="message icon"
        />
    
    <EditText
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@null"
        android:padding="4dp"
        android:text="Message"
        android:inputType="textMultiLine"
        android:gravity="top"
        android:layout_weight="1" />
    

    I took that idea, and achieved it by overlapping the icon inside the TextInputLayout with a FrameLayout wrapped in a LinearLayout. My code looks like this:

            <LinearLayout
            android:layout_marginTop="28dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <FrameLayout
                android:background="@drawable/edt_bg_selector_fafa_orange"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="start">
    
                <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/tilComentReporte"
                    android:gravity="top|start"
                    app:errorTextColor="@color/colorRojo"
                    app:errorEnabled="true"
                    android:maxLines="10"
                    android:background="@drawable/edt_bg_selector_fafa_orange"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
    
                    <com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/etComentReporte"
                        android:paddingBottom="10dp"
                        android:background="@null"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:lines="5"
                        android:gravity="top|start"
                        android:singleLine="false"
                        android:inputType="textMultiLine|textAutoComplete"
                        android:drawablePadding="15dp"
                        android:fontFamily="@font/lato"
                        android:hint="@string/sign_describe_tu_problema"
                        android:paddingStart="20dp"
                        android:textSize="16sp" />
                </com.google.android.material.textfield.TextInputLayout>
    
                <ImageView
                    android:paddingTop="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:scaleType="fitStart"
                    android:src="@drawable/ic_contacto_nar" />
            </FrameLayout>
        </LinearLayout>
    

    And it looks like this on device:

    The view now