3

I'm looking for a way to have a grayed out text as prefix in an EditText. This text should be not selectable. It's a bit like the To field when you're composing a message with Gmail. The only (visual) difference is that this text disappears when you start typing.

Is there any trick to achieve this in Android?

Thanks!

321X
  • 3,153
  • 2
  • 30
  • 42
  • What Gmail app is doing is a `android:hint="To"` that's do-able, but what you require, needs some research – Aman Alam Mar 29 '11 at 09:22
  • Thanks! In my situation there is already text in the field. It's like "EUR 2500" where the EUR part is read-only/not selectable. I do expect it's a hard one to work out – 321X Mar 29 '11 at 09:28

2 Answers2

4

You can use an image of the part "EUR 2500". this you can display in your editbox without affecting the rest of the part. Follow the code:

Drawable editTextDrawable = context.getResources().getDrawable(imageId);
    editTextDrawable.setBounds(0, 0, editTextDrawable.getIntrinsicWidth(),
            editTextDrawable.getIntrinsicHeight());

The drawable can be used inside the edittext as follows:

editTxtItemName.setCompoundDrawables(,
            ListViewConstants.editTextDrawable, null, null, null);
Vicky Kapadia
  • 6,025
  • 2
  • 24
  • 30
0

As an ultimate solution, you can rewrite the full EditText class by extending it and modifying it in a way that it has a custom Background set by you, and a predefined padding set by you.

Put the EUR as the background, positioning it in the left side, and then give the starting padding of the EditText in such a way that the text the user types, starts right after the EUR text.

This maybe regarded as an overkill or a poor-man's solution to this problem, but still its the ultimate option. Not the smartest one perhaps, and I also don't know if its gonna work for sure :P

All the best!

Aman Alam
  • 11,231
  • 7
  • 46
  • 81
  • Yes, this is what I had in mind too... But, since I'm an beginning Android developer I don't have any idea how to set such a custom background. I've Googled it but it's hard to find. Any hints? – 321X Mar 29 '11 at 10:06
  • Create a class, let it extend the class `EditText`. read the `EditText` source here: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/widget/EditText.java and try to find out what are the methods that you need to override. while creating an EditText, refer to this class, create an object of this class and then use it. I know it might be tough for you, but it will be interesting for sure! – Aman Alam Mar 29 '11 at 10:14
  • Putting it in the background doesn't quite work right because if you enter more text than will fit in the view it isn't scrolled. – Timmmm Sep 26 '12 at 12:11