Question

Is there any way to add the x-graphics in the android Editbox like the of iPhone So that by clicking on that x graphic it can clear all the values in the Editbox

alt text

Is there any way to listen weather i touch a specific part of an edit text Thank you

Was it helpful?

Solution

Yes there is a way to achieve this.

Define a RelativeLayout like this one.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
  <EditText android:id="@+id/edittext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"/>
  <ImageButton android:id="@+id/clear"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/icon"
    android:layout_alignRight="@id/edittext"/>
</RelativeLayout>

Now what happens. The ImageButton gets drawn on top of the EditText. We set its right edge to be equal to the right edge of the EditTexts in order to get it appear on the right side.

Now you have to assign your ImageButton an OnCLickListener with if overridden method to just set the EditTexts text to a empty string like that.

EditText editText = null;
ImageButton clear = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.clear);

    editText = (EditText) findViewById(R.id.edittext);
    clear = (ImageButton) findViewById(R.id.clear);

    clear.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            editText.setText("");
        }
});

}

Now here we simply tell our ImageViews OnClickListener to reset our EditTexts text upon a click. Simple as that. ;)

Of course my example uses not very aesthetic images but you can fine tune the images yourself. The principle works.

OTHER TIPS

You can download it, it works like iPhone

https://github.com/GhOsTTT/editTextXbutton

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top