title: Android "wrong password or username " message box display error
body: <p>I am working on an activity that has username and password. I used the <strong>AlertDialog.Builder</strong> in the <strong>else</strong> part to show the message of "wrong password or username". I am not connecting it with any database just for the test purpose using strings for password and username to compare the values of editText fields with, in the <strong>if</strong> condition and if match it takes the user to a new screen name <strong>"NewMenu"</strong>. My problem is that when the login activity starts (the app runs), it shows first of all the message, that is the <strong>else</strong> part, I want to show this message after the submit button has been clicked and the the password or username was wrong. Here is the code</p>
  
  <pre><code>public class Login extends Activity{
  
  
      private String pass=new String();
      private String nam=new String();
      private Button log;
      private View textreg;
      private EditText text1;
      private EditText text2;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.login);
          textreg=(TextView) findViewById(R.id.textRegister);
          log=(Button) findViewById(R.id.btnsubmit);
          text1=(EditText) findViewById(R.id.txtuname);
          text2=(EditText) findViewById(R.id.txtpassword);
  
  
  
                //IF NOT REGISTERED USERS//
  ////A textview when clicked takes user to registeration page/////
  
          textreg.setOnClickListener(new OnClickListener() {
  
              @Override
                      public void onClick(View view) {
                  startActivity(new Intent("com.DRMS.Register"));
                      }
                  });
  
  
           //                                                        //
          // IF PASSWORD AND USERNAME CORRECT, GOTO NewMenu Activity //
         //                                                        //
  
          pass="12345";
          nam="12345";
  
          if(text1.getText().toString().equals(nam) &amp;&amp; text2.getText().toString().equals(pass) ){
  
  
              log.setOnClickListener(new OnClickListener() {
  
                  @Override
  
                          public void onClick(View view) {
                      startActivity(new Intent("com.DRMS.NewMenu"));
                          }
                      });
  
          }
  
              else{
              AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);
  
              dlgAlert.setMessage("wrong password or username");
              dlgAlert.setTitle("Error Message...");
              dlgAlert.setPositiveButton("OK", null);
              dlgAlert.setCancelable(true);
              dlgAlert.create().show();
  
              dlgAlert.setPositiveButton("Ok",
                      new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int which) {
  
                          }
                      });
              }
      }
  }
  </code></pre>