Set image into editText on button click via MainActivity.java file.
Edittext inside image can also be set through dynamically assignment process this feature are called as setting up edittext image at edittext. After Setting up this image application user an set on click event on so each and every time when user clicks on it, it will open a new activity, alert dialog box, sticker window, smiley screen. So here is the complete step by step tutorial for Add image icon inside edittext android programmatically.
How to Add image icon inside EditText android programmatically.
Code for MainActivity.java file.
package com.android_examples.com.edittextimageaddprogrammatically; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { EditText ImageSet; Button AddImageLEFT, AddImageRIGHT; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageSet = (EditText)findViewById(R.id.editText1); AddImageLEFT = (Button)findViewById(R.id.button1); AddImageRIGHT = (Button)findViewById(R.id.button2); AddImageLEFT.setOnClickListener(new View.OnClickListener() { @SuppressWarnings("deprecation") @Override public void onClick(View v) { ImageSet.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.demo_image_2), null,null, null); } }); AddImageRIGHT.setOnClickListener( new View.OnClickListener() { @SuppressWarnings("deprecation") @Override public void onClick(View v) { ImageSet.setCompoundDrawablesWithIntrinsicBounds(null, null,getResources().getDrawable(R.drawable.demo_image_2), null); } }); } }
Code for activity_main.xml layout file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.android_examples.com.edittextimageaddprogrammatically.MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/editText1" android:layout_centerHorizontal="true" android:layout_marginTop="15dp" android:text="ADD IMAGE AT LEFT SIDE INSIDE EDITTEXT BOX" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_below="@+id/button1" android:text="ADD IMAGE AT RIGHT SIDE INSIDE EDITTEXT BOX" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:ems="10" /> </RelativeLayout>
Screenshot: