Friday, 22 May 2015

Android Clear all EditText Fields


 Here I am going to explain how to clear all the EditText Fields in a single Button  click

Step 1)Open res->layout->activity_main.xml;

   <RelativeLayout    
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/allClear"
    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.example.clear_edittext.MainActivity" >

    <TextView
        android:id="@+id/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android Dhina Textwatcher Example" />

    <EditText
        android:id="@+id/editext1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/android"
        android:layout_marginTop="50dp"
        android:textColor="#000000" />
   <EditText
        android:id="@+id/editext2"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editext1"
        android:layout_marginTop="50dp"
        android:textColor="#000000" />


   <EditText
        android:id="@+id/editext3"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editext2"
        android:layout_marginTop="50dp"
        android:textColor="#000000" />


  <Button
        android:id="@+id/clear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:layout_below="@+id/editext3"
        android:text="Clear" >

    </Button>

</RelativeLayout >



Step 2)Activity Class
  
package com.example.clear_edittext;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends Activity 
{

EditText editext1,editext2,editext3;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editext1=(EditText)findViewById(R.id.editext1);
editext2=(EditText)findViewById(R.id.editext2);
  editext3=(EditText)findViewById(R.id.editext3);

  Button Clear=(Button)findViewById(R.id.Clear);
  Clear.setOnClickListener(new OnClickListener() {
            @Override
    public void onClick(View v) 
     {
ViewGroup group = (ViewGroup) findViewById(R.id.allClear);
clearEditext(group );
}

});


}     

   private void clearEditext(ViewGroup group
{
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
        View view = group.getChildAt(i);
        if (view instanceof EditText) {
        ((EditText)view).setText("");
        }
        if(view instanceof ViewGroup&& (((ViewGroup)view).getChildCount() > 0))
        clearEditext((ViewGroup)view);

}
}
}
Happy Coding :)