Can I resize an icon inside an EditText

I create a login window with username and password fields, and in each I would like to put icons. When I add an icon to the EditText field, I cannot resize the icon.

The icon is inside the EditText field because I want to change the background of the EditText when it is focused.

I tried to put the icon and EditText in LinearLayout, but I could not change the layout of the background when EditText is focused.

My EditText Code:

<EditText android:id="@+id/user_name_edit_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="35dp" android:layout_marginRight="35dp" android:layout_marginTop="35dp" android:background="@drawable/custom_border_selector" android:drawablePadding="5dp" android:hint="@string/username" android:singleLine="true" android:textColor="#757575" android:textSize="15sp" android:drawableLeft="@drawable/ic_person_grey_24dp" /> 

EditText visual:

enter image description here

+9
source share
3 answers

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

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

Sets Drawables (if any) to appear to the left of, above, to the right of the text and below it. Use 0 if you don't need Drawable. Borders Drawables will be set to their inner borders.

Now, to add an add-on, you can use this

  public void setCompoundDrawablePadding (int pad) 

Sets the size of the gasket between composite drawings and text.

XML related attributes:

 android:drawablePadding 

More details here and here . There are many other ways that you might find interesting.

+6
source

If you want to solve the problem in xml, here is an example code where you can manipulate the size and filling of your image:

 <!-- 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 what it looks like:

enter image description here

+13
source

A better way than installing java would be to install it inside a layered drawing 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] 
+2
source

Source: https://habr.com/ru/post/1243934/


All Articles