Apply validations to Contact Form in Android application
Hi friends ,In this article i would like to explain how to apply validations in android applications..Here i am going to explain only Required Field Validations later i will explain Regular expressions and so on..so lets start
First ,Create a New android project and open the main.xml file
Next,Design the form ,I have already explained how to create a contact form in android
http://www.developerscode.com/2012/02/create-socail-network-contact-form-in.html
after the designing process is completed .Open the ValidationsActivity.java class and write the following code
public class ValidationsActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final EditText name=(EditText) findViewById(R.id.name); final EditText email=(EditText) findViewById(R.id.email); final EditText mobile=(EditText) findViewById(R.id.mobile); final EditText message=(EditText) findViewById(R.id.message); Button submit = (Button) findViewById(R.id.submit); submit.setOnClickListener(new OnClickListener() { public void onClick(View v) { if(name.getText().toString().length()==0) { name.setError( "Please Enter Name" ); } else if(email.getText().toString().length()==0) { email.setError( "Please Enter Email" ); } else if(mobile.getText().toString().length()<10) { mobile.setError( "Please Enter Valid Mobile No" ); } else if(message.getText().toString().length()==0) { message.setError( "Please Enter Message" ); } else { Intent submitform=new Intent(ValidationsActivity.this,Home.class); startActivity(submitform); } } }); } }
Code Explanation :
final EditText name=(EditText) findViewById(R.id.name); means
assiging a name to the control
name.setError( "Please Enter Name" ); means
it displays a custom message what ever want
Intent submitform=new Intent(ValidationsActivity.this,Home.class);
means
Once the form is successfully submitted..it will redirects to Home screen
Thats it...Happy Coding...:)
Labels: Android, Android Tutorials, AndroidBasic, Tanisha
2 Comments:
thanq ..helped me a lot
thanq so much..........helped me a llot
Post a Comment
Subscribe to Post Comments [Atom]
<< Home