Set onclicklistener on EditText inside drawable image in android

How to add on click event function above EditText icon image in android.

Setting up onclicklistener() function is too difficult because there are lot’s of way to add click functionality above drawable image inside EditText, so here is the best solution to make your drawable icon works as real buttons. You can do anything on icon clicked event. So here is the complete step by step tutorial for Set onclicklistener on EditText inside drawable image in android .

android-project-download-code-button

What i am doing in this project.

Firstly creating a frame layout in activity_main.xml file then i am putting  edittext widget in it and set drawable image inside edittext now i am creating a button and set button background as invisible via ” android:background=”?android:attr/selectableItemBackground ” attribute in button. Then setting up on click listener function above it.

How to Set onclicklistener on EditText inside drawable image in android .

Code for MainActivity.java file.

package com.android_examples.com.edittextinsideimageclickfunction;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {

 Button ButtonInvisible;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 ButtonInvisible = (Button)findViewById(R.id.button1);
 
 ButtonInvisible.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 Toast.makeText(MainActivity.this,"You clicked on EditText drawable image ", Toast.LENGTH_LONG).show();
 
 }
 });
 
 }
}

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"
 tools:context="com.android_examples.com.edittextinsideimageclickfunction.MainActivity" >

 <FrameLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true" >

 <EditText
 android:id="@+id/editText1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:drawableRight="@drawable/demo_image"
 android:ems="13"
 android:hint="EditText Image onClickListner " />
 
 <Button 
 android:id="@+id/button1"
 android:layout_width="50dp"
 android:layout_height="50dp"
 android:layout_gravity="right"
 android:text=" "
 android:background="?android:attr/selectableItemBackground"
 />
 </FrameLayout>

</RelativeLayout>

Screenshot:

Set onclicklistener on EditText inside drawable image in android

Click Here To Download Set onclicklistener on EditText inside drawable image in android project.

Leave a Reply

Your email address will not be published. Required fields are marked *