Monday 20 January 2014

ANDROID SIMPLE FORM VALIDATION USING EDITTEXT

In this post i am going to cover how to validate edittext in android. Many android beginners struggling to understand validate the form. The following example has one edittext and two textview. Edittext value set for textview.

Layout Form Design File

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#99ccff">

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Enter your name:"
    android:textSize="25sp"
    android:textColor="#660000"/>

 <EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="number"
android:hint="Enter Number"
android:id="@+id/enterdata"/>

<Button
android:layout_height="wrap_content"
android:text="Submit"
android:layout_gravity="center"
 android:layout_width="200sp"
 android:onClick="check"
 android:textSize="20sp"
 android:id="@+id/submit"/>


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#660000"
android:text="Entered name is : "
android:id="@+id/display"/>
</LinearLayout>



In above edittext accept only numbers by using attribute inputtype="number".

Classfile MainActivity.java

package com.example.zformm;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
 EditText enternumber, ee;
 Button submit;
 TextView display;  
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        enternumber = (EditText)findViewById(R.id.enterdata);
        display = (TextView)findViewById(R.id.display);
        submit = (Button)findViewById(R.id.submit);
    }  
  
    public void check(View v){
       
        //check if edittext value is empty
         if (enternumber.getText().length() == 0) {
             Toast.makeText(this, "Enter the value", Toast.LENGTH_LONG).show();
         } else {
            //check if edittext value is below 10
             Double number = Double.parseDouble(enternumber.getText().toString());
             if(number > 10 || number == 0){
                 Toast.makeText(this, "Enter the number below 10", Toast.LENGTH_LONG).show();
             } else {
                 //Finally set edittext value to textview
                 display.setText("Entered Value is : "+number);
             }
         }

    }
}


In above example If editext value is empty or above 10 then display toast error

I hope this example would help you. Thanks for read this example.






No comments:

Post a Comment