how to define a reset button to clear all text field at one click?

ramesh picture ramesh · Aug 20, 2013 · Viewed 44k times · Source
<ScrollView
    android:id="@+id/ScrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

     <LinearLayout
         android:id="@+id/MainParent"
         android:layout_width="wrap_content"
         android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginLeft="20sp"
        android:layout_marginRight="20sp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="80sp"
            android:text="Sim Card Master"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/pumpkinorange"
            android:textSize="17sp" />

        <TextView
            android:id="@+id/simno"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5sp"
            android:text="Sim No"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/darkblue"
            android:textSize="14sp" />

        <EditText
            android:id="@+id/simName"
            android:layout_width="275dp"
            android:layout_height="wrap_content"
            android:selectAllOnFocus="true" >
            <requestFocus />
        </EditText>

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:text="Service Provider"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/darkblue"
            android:textSize="14sp" />

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:entries="@array/service_arrays"
            android:prompt="@string/serviceprovid" />

        <TextView
            android:id="@+id/TextView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="100sp"
            android:layout_marginTop="5dp"
            android:text="Unit Master"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/pumpkinorange"
            android:textSize="17sp" />

        <TextView
            android:id="@+id/TextView03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5sp"
            android:text="Unit No"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/darkblue"
            android:textSize="14sp" />

        <EditText
            android:id="@+id/unitNo"
            android:layout_width="275dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:selectAllOnFocus="true" />

        <TextView
            android:id="@+id/TextView04"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:text="IMEI No"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/darkblue"
            android:textSize="14sp" />

        <EditText
            android:id="@+id/imeiNo"
            android:layout_width="275dp"
            android:layout_height="wrap_content"
            android:ems="10" 
            android:selectAllOnFocus="true"/>

        <TextView
            android:id="@+id/vehicleNo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="80sp"
            android:layout_marginTop="5dp"
            android:gravity="center"
            android:text="Vehicle Master"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/pumpkinorange"
            android:textSize="17sp" />

        <TextView
            android:id="@+id/TextView06"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="Vehicle No"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/darkblue"
            android:textSize="14sp" />

        <EditText
            android:id="@+id/vehicleNo"
            android:layout_width="275dp"
            android:layout_height="wrap_content"
            android:ems="10" 
            android:selectAllOnFocus="true"/>

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="34dp"
            android:layout_gravity="center"
            android:text="Save"
            android:textSize="15sp"
            android:textStyle="italic"
            android:typeface="normal" />

        <Button
            android:id="@+id/button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="Reset"
            android:textSize="15sp"
            android:textStyle="italic"
            android:typeface="normal" />

    </LinearLayout>

  </LinearLayout>
</ScrollView>

In this xml layout has four text fields and two buttons. while click on the reset button all the four textfields get cleared. How to define code for this in main activity.please Show explain me with code.

Answer

hemantsb picture hemantsb · Aug 20, 2013

In your MainActivity, In your onCreate do this

Button reset = (Button)findViewById(R.id.reset);

Similarly Find your all textview here. then,

reset.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        textview1.setText("");
        textview2.setText("");
        ......
        textview8.setText("");
    }
});