Below is my layout:
<EditText
android:id="@+id/account_et"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:drawableRight="@drawable/icon_backall"
android:ems="10"
android:hint="@string/str_md_email"
android:inputType="textEmailAddress"
android:padding="10dp" >
</EditText>
I want to show the drawableRight when EditText be focused.
And hide while without focus.
Another one is that I want to set OnClickListener of drawableRight.
How can I do?
I hope my sample will help you
activity_main.xml
<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" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="130dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:inputType="textEmailAddress" >
</EditText>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="14dp"
android:layout_marginTop="43dp"
android:src="@drawable/ic_launcher"
android:visibility="invisible" >
</ImageView>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/editText1"
android:ems="10" >
<requestFocus />
</EditText>
</RelativeLayout>
this is the class file in which drawable options are performed
MainActivity.java
package com.example.doubtedittext;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
public class MainActivity extends Activity {
private EditText etext;
private ImageView imageView1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etext = (EditText) findViewById(R.id.editText1);
imageView1 = (ImageView) findViewById(R.id.imageView1);
etext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
imageView1.setVisibility(View.VISIBLE);
} else {
imageView1.setVisibility(View.INVISIBLE);
}
}
});
imageView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
}
the operation you required successfully performed above.....
i would suggest you to add imageView separated from the EditText and align him to be on top of him with align top and align right and that you have full control so you can invisible him and setOnClickListner
<ImageView android:id="@+id/account_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/account_et"
android:layout_alignTop="@+id/account_et"
android:background="@drawable/icon_backall">
</ImageView>
Use View.OnFocusChangeListener. Putting drawable to edittext when you catch the focus, then replacing drawable with null would solve the problem.
Use like belwo
edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus)
{}
else {}
}
});