0

I set the picture to my EditText using android:drawableLeft But I had a problem in the size of this picture, how can I change it in xml?

xml

    <EditText
        android:id="@+id/RegisterActivity_EditText_Email"
        android:layout_width="288dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="27dp"
        android:background="@drawable/field_edit_text_style"
        android:fontFamily="sans-serif-light"
        android:drawableLeft="@drawable/mail_img_foreground"
        android:inputType="textEmailAddress"
        android:gravity="center"

loveu
  • 23
  • 6

3 Answers3

1

you can set the layout_height like this

<EditText
    android:id="@+id/RegisterActivity_EditText_Email"
    android:layout_width="288dp"
    android:layout_height="50dp"
    android:layout_marginTop="27dp"
    android:background="@drawable/field_edit_text_style"
    android:fontFamily="sans-serif-light"
    android:drawableLeft="@drawable/mail_img_foreground"
    android:inputType="textEmailAddress"
    android:gravity="center"
  • [link](https://prnt.sc/q3bxlu) I need to change the size of the image so that it fits well in the field. In the screenshot, I think it’s clear what I'm trying to achieve. –  loveu Nov 28 '19 at 12:58
  • Drawable drawable = ContextCompat.getDrawable(context,R.drawable.yourdrawable); drawable.setBounds(0, 0, (int)(drawable.getIntrinsicWidth()*0.5), (int)(drawable.getIntrinsicHeight()*0.5)); ScaleDrawable sd = new ScaleDrawable(drawable, 0, scaleWidth, scaleHeight); Button btn = findViewbyId(R.id.yourbtnID); RegisterActivity_EditText_Email.setCompoundDrawables(sd.getDrawable(), null, null, null); – djibril diop Nov 28 '19 at 13:02
  • or you can create an layerlist like this – djibril diop Nov 28 '19 at 13:03
1

You can always cheat and use:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/field_edit_text_style">

<EditText
        android:id="@+id/RegisterActivity_EditText_Email"
        android:layout_width="288dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="27dp"
        android:layout_marginLeft="-10dp"
        android:layout_toRightOf="@+id/icon"
        android:background=""
        android:fontFamily="sans-serif-light"
        android:padding="10dp"
        android:inputType="textEmailAddress"
        android:gravity="center"/>

    <ImageView
        android:padding="3dp"
        android:id="@+id/icon"
        android:src="@drawable/perm_group_personal_info"
        android:layout_width="40dp"
        android:layout_height="40dp" />

</RelativeLayout>
JCDecary
  • 557
  • 1
  • 7
  • 18
0

You just need a Linear Layout.

Add ImageView and EditText in LinearLayout and set 'layout Orientation' horizontal.

android:orientation="horizontal"

Try below code after changing height and width accordingly.

<LinearLayout
    android:layout_width="width"
    android:layout_height="height"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="width"
        android:layout_height="height"
        android:src="@drawable/mail_img_foreground" />

    <EditText
        android:id="@+id/RegisterActivity_EditText_Email"
        android:layout_width="288dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="27dp"
        android:background="@drawable/field_edit_text_style"
        android:fontFamily="sans-serif-light"
        android:gravity="center"
        android:inputType="textEmailAddress" />

</LinearLayout>

Feel free to ask in comments. :-)

CodingDevil
  • 125
  • 1
  • 9