50

I want to dynamically add image in EditText. Is it possible?

if anyone knows please give sample code for that.

user3395743
  • 293
  • 2
  • 12
sivaraj
  • 1,849
  • 9
  • 35
  • 52

10 Answers10

64

If something like this:

EditCheck

is what you're talking about, then you just need to either set the Drawable{Right | Left | Top | Bottom} property in the xml, or call the corresponding java command.

EditText text = (EditText)findViewById(R.id.text);
text.setCompoundDrawables(null, null, getResources().getDrawable(R.drawable.check_box), null);
CaseyB
  • 24,780
  • 14
  • 77
  • 112
60

enter image description here

Using the frame layout it is very easy to overcome this.Here another advantage is that you can give click events for buttons.if you set icons using setCompoundDrawables, you cant give the click events.I have implemented in my project that search bar has delete and search icons.

<FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <EditText
                android:id="@+id/search"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/search_bar"
                android:drawablePadding="8dp"
                android:paddingLeft="30dp"
                android:paddingRight="10dp"
                android:inputType="text"
                android:maxLines="1">

                <requestFocus />
            </EditText>

            <Button
                android:id="@+id/searchBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left|center_vertical"
                android:layout_margin="10dp"
                android:background="@drawable/icon_magnify" />

            <Button
                android:id="@+id/delete"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|center_vertical"
                android:layout_margin="8dp"
                android:background="@drawable/icon_remove" />
        </FrameLayout>
Community
  • 1
  • 1
Ramesh Akula
  • 5,720
  • 4
  • 43
  • 67
52

using android:drawableRight or android:drawableLeft (depend where you prefer align image)

<EditText 
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/searchedTxt"
        android:width="200dp"
        android:layout_alignParentRight="true"
        android:drawableRight="@android:drawable/ic_menu_search"
        android:drawablePadding="@dimen/dimen_10dp"
        />  
Kamau Mbûgua
  • 777
  • 1
  • 10
  • 19
Ging3r
  • 2,935
  • 1
  • 18
  • 11
28

you can also try this

    SpannableString ss = new SpannableString("abc\n");
    Drawable d = img.getDrawable();
    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
    ss.setSpan(span, 0, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    editT.setText(ss);
Buda Gavril
  • 21,409
  • 40
  • 127
  • 196
  • 1
    +1 this code is helpful if you want to add more then one drawable to the edit text dynamically. for e.g chating input edittext. User can select smiley icons. one needs to append this Spanable String to the editable. – Rohit Sharma Jun 05 '13 at 10:21
  • where does `3` parameter in `setSpan` come from? – akhy Nov 18 '14 at 07:49
  • it's the length on string "abc" – Buda Gavril Nov 18 '14 at 07:53
  • Hi, in my case it is look like this [link](https://docs.google.com/file/d/0B-d7AyAECAIsZGFka0lONWphY2M/edit?usp=docslist_api). Any idea why? I download drawable like this: `thiss.getResources().getDrawable(R.drawable.friends)` – Eliasz Kubala Nov 24 '14 at 10:40
16

Test Image

You can use setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom).

    EditText text = (EditText) findViewById(R.id.edtTest);
    text.setText("Test");
    text.setCompoundDrawablesWithIntrinsicBounds(null, null,
                       getResources().getDrawable(R.drawable.myDrawable, null), null);
Tom Howard
  • 4,672
  • 2
  • 43
  • 48
Mihir Palkhiwala
  • 2,586
  • 3
  • 37
  • 47
7

You can add an image to your EditText through android:background="@drawable/img".

If you want to modify the style by using nine patch or else, but if you want to add a small image in the left of your EditText consider using android:drawableRight="@drawable/icon".

jonsca
  • 10,218
  • 26
  • 54
  • 62
sami boussacsou
  • 1,063
  • 9
  • 18
3

extend the edittext class and do code as u want it to be

public void setImage(String imageResource) {
    int position = Selection.getSelectionStart(MyEditText.this
            .getText());

    Spanned e = Html.fromHtml("<img src=\"" + imageResource + "\">",
            imageGetter, null);

    MyEditText.this.getText().insert(position, e);
}
1

You can use:

editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.pic_name, 0); 

as suggested here:
Calling setCompoundDrawables() doesn't display the Compound Drawable

Community
  • 1
  • 1
1

Create an instance of the EditText

EditText editText = (EditText) findViewById(R.id.EditText1);

Then create a drawable instance of the image

Drawable image = getResources().getDrawable(R.drawable.ic_warning); // API 21 and below.

OR

Drawable image = getDrawable(R.drawable.ic_warning); // API 22 and above.

Then set the image by calling setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom);

editText.setCompoundDrawablesWithIntrinsicBounds(null, null, image, null); 

The above line will set the image in the right side of the EditText

Ajay Sivan
  • 2,807
  • 2
  • 32
  • 57
0

You can try this if you are in adapter class

    holder.myEditText.setCompoundDrawablesWithIntrinsicBounds(null, null,
            _context.getResources().getDrawable(R.drawable.ic_launcher),
            null);

and You can try this if you are in activity class

    myEditText.setCompoundDrawablesWithIntrinsicBounds(null, null,
            getResources().getDrawable(R.drawable.ic_launcher),
            null);