VijayKumar
VijayKumar

Reputation: 11

validate the check fileds click a button in android

i want to check the edit text field box. After i click the submit button if textbox was empty it displays message using toast. after i entered something in textbox click the submit button second time some error occured .Pls give solution to check fields using submit button. Thanks in advance. Here code :

     package com.valid;

     import java.util.Locale;

     import android.app.Activity;
     import android.app.AlertDialog;
     import android.content.ContentValues;
     import android.database.sqlite.SQLiteDatabase;
     import android.os.Bundle;
      import android.view.View;
     import android.widget.Button;
     import android.widget.EditText;
     import android.widget.TextView;
     import android.widget.Toast;
     import android.R.id;


   public class ValidationActivity extends Activity {
     /** Called when the activity is first created. */


   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

     Button b=(Button)findViewById(R.id.ok);
      b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) { 
                final EditText input=(EditText)findViewById(R.id.name);

                final String n1=input.getText().toString();
                 if(n1.equalsIgnoreCase(""))
                         {
                           Toast.makeText(getApplicationContext(),"Please fill the textbox",Toast.LENGTH_LONG).show();
                         }
                 else
                 {
                 SQLiteDatabase db;
                db=openOrCreateDatabase("nam5.db",SQLiteDatabase.CREATE_IF_NECESSARY,null);
                db.setVersion(1);
                db.setLocale(Locale.getDefault());
                db.setLockingEnabled(true);
                final String table_create="CREATE TABLE customer("+"name TEXT);";
                db.execSQL(table_create);
                ContentValues values= new ContentValues();
                values.put("name",n1);
                db.insert("customer", null, values);


                 }


        }




    });


}

}

Upvotes: 0

Views: 3317

Answers (1)

RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

compare string with null

final String fname= input.getText().toString().trim();
if(fname.equals("") && fname.length() > 0) {
///show toast message
}else {
//continue 
}

Upvotes: 2

Related Questions