How to Add an Image on Edittext

How can I add an image on EditText

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);

Set Image on Left side of EditText in Android

Instead of using android:drawableLeft="@drawable/name_icon_edittext", create separateImageView for the name_icon.
This way you have more control on placing it on the layout

Note: I am assuming your layout is RelativeLayout

how to insert image to a editText

Do something like this (note: you can reuse SpannableStringBuilder)

editText = (EditText)mRoot.findViewById(R.id.content);
ImageSpan imageSpan = new ImageSpan(preview);

SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(editText.getText());

// this is a string that will let you find a place, where the ImageSpan is.
String imgId = "[img=1]";

int selStart = editText.getSelectionStart();

// current selection is replaceв with imageId
builder.replace(editText.getSelectionStart(), editText.getSelectionEnd(), imgId);

// This adds a span to display image where the imageId is. If you do builder.toString() - the string will contain imageId where the imageSpan is.
// you can use it later - if you want to find location of imageSpan in text;
builder.setSpan(imageSpan, selStart, selStart + imgId.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setText(builder);

Note: See follow up answer for dealing with partial deletion of tags

Inserting imageview inside edittext android

Use this:

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit_text"
android:drawablePadding="8dp"
android:drawableRight="@drawable/yourImage"/>

you can use drawable left,right,top or bottom of your text according to your need.

To manipulate in java use this:

EditText editText = (EditText) findViewById(R.id.yourEditTextId);
editText.setCompoundDrawablesWithIntrinsicBounds(left,top,right,bottom);

for manipulating image set on right:

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

similarly you can do it for other cases.

Hope it helps!!!

Android: How to insert images into edit text

Although the image isn't very clear in describing your question, you can however use the below code to add a drawable in EditText.

<EditText
...
android:drawableLeft="@drawable/some_drawable"
android:drawablePadding="10dp"/>

how to set text and image as a hint in edittext in center?

You can add an image in edittext as drawableleft and remove it while getting focus.

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Username"
android:drawableLeft="@drawable/yourimage" />

remove it when getting focus

final EditText et=(EditText) findViewById(R.id.editText1);
et.setOnFocusChangeListener(new OnFocusChangeListener() {

@Override
public void onFocusChange(View arg0, boolean gotfocus) {
// TODO Auto-generated method stub
if(gotfocus){
et.setCompoundDrawables(null, null, null, null);
}
else if(!gotfocus){
if(et.getText().length()==0)
et.setCompoundDrawablesWithIntrinsicBounds(R.drawable.yourimage, 0, 0, 0);
}

}
});

How to add ImageView inside EditText?

Instead of using the rounded corner rectangle to your EditText Background, use it on the parent RelativeLayout. Make the subsequent children bg transparent:

<RelativeLayout
android:layout_marginTop="15dp"
android:background="@drawable/rounded_rectangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<EditText
android:id="@+id/editTextDate"
android:background="@android:color/transparent"
android:layout_toRightOf="@+id/dateIcon"
android:text=""
android:hint="Date"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<ImageView
android:layout_marginLeft="10dp"
android:padding="3dp"
android:tint="@color/colorPrimaryDark"
android:id="@+id/dateIcon"
android:src="@android:drawable/ic_menu_my_calendar"
android:background="@android:color/transparent"
android:layout_width="40dp"
android:layout_height="40dp" />

</RelativeLayout>


Related Topics



Leave a reply



Submit