2

I'm trying to create an EditText that has the following background images (aligned right) when a user inputs valid/invalid text:

tick

cross

How can I achieve this in Android? I've tried setting the background image using:

editText.setBackgroundResource(R.id.icon_tick), but when I do this, the whole image gets stretched to fit the EditText.

Michael Easter
  • 23,733
  • 7
  • 76
  • 107
Eamorr
  • 9,872
  • 34
  • 125
  • 209

3 Answers3

4

editText.setCompoundDrawables(left, top, right, bottom) where left, top, right and bottom are either drawables or null.

superM
  • 8,605
  • 8
  • 42
  • 51
2

Why does it have to be a background image? Just make it a standard image and position it accordingly in your layout. Otherwise, you'll need to make a large image that actually takes up the entire background of your view (with your icons positioned where you want them). You'll also need to size this image accordingly for the various screen resolutions.

An alternative approach can also be found here:

Background Image Placement

Possible duplicate post.

Community
  • 1
  • 1
SBerg413
  • 14,515
  • 6
  • 62
  • 88
1

You can make the drawable as nine patch image. Have some transparent area to the left of the tick mark and define it as stretchable area. That should do the trick.

Ron
  • 24,175
  • 8
  • 56
  • 97
  • 1
    You know how to make 9-patch PNG images? I assume yours is a simple PNG drawable. Convert it to 9-patch. While converting, add some transparent region to the left side of ur images(cross and tick) and define that region as horizontally stretchable. Thats the easiest solution. You may need graphic designer's help. – Ron Aug 24 '11 at 11:56