Saturday, 18 February 2012

Set Validation error for edittext in android

suppose user not fill the edittext filed  at the time we can show alert\error msg .

Solution: 

we can solve this issue using setError().

ex code:
  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.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("please enter the value");
                }

            }
        });
    }

screen shot: