How to set background image on the right hand side of a shape in android

549 Views Asked by At

I am making a background shape for EditText - xml below:

<?xml version="1.0" encoding="UTF-8" ?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <solid android:color="@color/WhiteSmoke"/> 
  <stroke android:width="1dp" android:color="@color/aadhaar_logo_brown" />   
  <padding android:left="2dp" android:top="2dp" android:right="2dp" android:bottom="2dp" /> 
  <corners android:topRightRadius="10dp" />
  <gradient android:startColor="@color/White" 
            android:centerColor="@color/White" 
            android:endColor="@color/White"
            android:angle="0"/>
</shape>

However I require an image on the right hand side of the EditText to indicate mandatory field. I tried adding the image programmatically but the image does not show up in the runtime

((EditText) findViewById(R.id.txt_enduser_name)).setCompoundDrawables(null, null, getResources().getDrawable(R.drawable.ic_mandatory_text_field), null);

Please help

1

There are 1 best solutions below

0
On BEST ANSWER

You need to provide an "intrinsic size" to the shape:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    ...
    <size android:height="20dp" android:width="20dp"/>
</shape>

Then, you can just set it as the drawableRight:

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableRight="@drawable/ic_mandatory_text_field"/>