Is it possible to change the size of icon inside EditText

后端 未结 3 1901
南旧
南旧 2021-02-20 09:56

I am creating a login window with username and password fields and in each I would like to put icons. When I add icon to EditText field I cannot change the size of icon.

相关标签:
3条回答
  • 2021-02-20 10:36

    A better way than setting by java would be by setting It inside a layered drawable as follows.

    Layered list drawable is as under :
    
        <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
    
            >
            <shape>
                <solid android:color="@color/bg_row_drop_completed"/>
                <size android:width="96dp"
                    />
                <padding android:top="-15dp"
                    android:bottom="-15dp"/>
                <corners android:topLeftRadius="@dimen/btn_add_bg_radius_purple" android:topRightRadius="@dimen/btn_add_bg_radius_purple"/>
            </shape>
        </item>
        <item
            >
            <bitmap android:src="@drawable/_ic_arrow_drop_up_normal" android:gravity="center_vertical"
                ></bitmap>
        </item>
    </layer-list>[enter image description here][1]
    
    0 讨论(0)
  • 2021-02-20 10:44

    You can change the icon and use a smaller one on focus. This is the method you should use.

    public void setCompoundDrawablesWithIntrinsicBounds (
                                           int left, int top, int right, int bottom)
    

    Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.

    Now to add padding you can use this

     public void setCompoundDrawablePadding (int pad)
    

    Sets the size of the padding between the compound drawables and the text.

    Related XML Attributes:

    android:drawablePadding
    

    More information here and here. There are many other methods you might find interesting.

    0 讨论(0)
  • 2021-02-20 10:44

    In case you'd like to solve the problem in xml, here's sample code where you can manipulate your image size/padding:

    <!-- USERNAME INPUT -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/edit_text_selector">
    
        <!-- INPUT -->
        <EditText
            android:id="@+id/username_input"
            android:layout_marginLeft="-10dp"
            android:layout_toRightOf="@+id/username_icon"
            android:text=""
            android:hint="Username"
            android:padding="10dp"
            android:background=""
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
        <!-- ICON -->
        <ImageView
            android:padding="3dp"
            android:id="@+id/username_icon"
            android:src="@drawable/perm_group_personal_info"
            android:layout_width="40dp"
            android:layout_height="40dp" />
    
    </RelativeLayout>
    

    And here is how it looks like:

    0 讨论(0)
提交回复
热议问题