Click here to Skip to main content
15,667,864 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The following piece of code is not displaying the validation message properly

I am 100% sure that i am not using brackets properly

package com.Wase.edittext;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;
import com.Wase.edittext.R;
import android.widget.TextView;
 
public class MyAndroidAppActivity extends Activity {
 
 private EditText edittext;
 private EditText edittext1;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_my_android_app);
 
	addKeyListener();
 edittext.requestFocus();
}
 

 
 public void addKeyListener() {

     // get edittext component
     edittext = (EditText) findViewById(R.id.editText);
     edittext1 = (EditText) findViewById(R.id.editText1);



     // add a keylistener to keep track user input
     edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
         @Override
         public boolean onEditorAction(TextView v, int keyCode, KeyEvent event) {

         // if keydown and "enter" is pressed
             if(keyCode == EditorInfo.IME_ACTION_GO)
         {
             edittext1.requestFocus();
             return true;
         }


         return false;
      } });

      edittext1.setOnEditorActionListener(new TextView.OnEditorActionListener() {
              @Override
              public boolean onEditorAction(TextView a, int b, KeyEvent c) {

         // if keydown and "enter" is pressed
              if(b == EditorInfo.IME_ACTION_GO) {

             //hide the keyboard
              InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
              imm.hideSoftInputFromWindow(edittext1.getWindowToken(), 0);
              if (edittext.getText().toString().equals(""))  
              {
            	 
            	  edittext.setError("This is Empty");
              }
              
              else {
            	   if (edittext1.getText().toString().equals(""))
            	   {
            		   
            		   edittext1.setError("This is Empty");
            	 
            		  
            	  }
              
              
            	  else 
            	  {
            		if (edittext.getText().toString().equals("") && edittext1.getText().toString().equals(""))
            		{
            		      edittext1.setError ("This is Empty");
            			  edittext.setError ("This is Empty");
            		}
            	  
            	  
              else
              {
            	  
            	  
         // display a floating message
             Toast.makeText(MyAndroidAppActivity.this, edittext.getText().toString() + "" + edittext1.getText().toString(), Toast.LENGTH_SHORT).show();
              }
            		return true;
              }
              }
              }
              
    return false;
              }});
      }
      
 }


And also i want the toast message to be displayed only after all validation message is passed

Its urget please help
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900