How can I add an image on EditText
If something like this:
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);
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>
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"
/>
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);