Saturday, 13 October 2012

How to show error in an EditText using setError() in Android?

package com.coderzheaven.seterror;
 
 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;
 
 public class MainActivity extends Activity {
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 Button btn =(Button)findViewById(R.id.button1);
 btn.setOnClickListener(new OnClickListener() {
 
 @Override
 public void onClick(View v) {
 EditText edittext =(EditText)findViewById(R.id.editText1);
 if(edittext.getText().length()==0){
 edittext.setError("Field cannot be left blank.");
 }
 }
 });
 }
 }
 
 
 
 
SetError Android

 
 

No comments:

Post a Comment