EditText with cross(x) button at end of it

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

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

Answers


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.


Need Your Help

Scrapy to manage session cookies with full webkit javascript execution

python session cookies webkit scrapy

I'm trying to use scrapy to scrape a site that uses javascript extensively to manipulate the document, cookies, etc (but nothing simple like JSON responses). For some reason I can't determine from ...

How to use joomla login to be authenticated from an external webservice

c# web-services rest joomla3.0

I have developped a web service using .Net framework and it will get the login details such as user name and password. when login is clicked it has to request web service using rest and get

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.