0

I'm new to android, just trying to get my head around it, but I can't seem to get this to work: I want when a user taps a button a method will be called to see if the 2 password boxed contain the same text, if they do n AlertDialog box will pop up saying that the password has been set. However the alertdialog box never pops up at all for somereason, thanks for your help.

package com.example.jhonti.test2;

import android.app.Activity;
import android.app.AlertDialog;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MyActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);


    // It works when I put it here
    //AlertDialog.Builder altDialog = new AlertDialog.Builder(this);
    //altDialog.setMessage("Password set"); // here add your message
    //altDialog.show();


    final EditText a = (EditText) findViewById(R.id.editText);
    final EditText c = (EditText) findViewById(R.id.editText2);

    Button b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {

            if(a.getText().toString() == c.getText().toString())
            {
                setPass(a.getText().toString(), c.getText().toString());
            }
        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

private void setPass(String i, String o)
{
    if((i == o) && (i != ""))
    {
        AlertDialog.Builder altDialog = new AlertDialog.Builder(this);
        altDialog.setMessage("Password set"); // here add your message
        altDialog.show();

    }
}

}

user3537381
  • 89
  • 1
  • 2
  • 8
  • Change `if(a.getText().toString() == c.getText().toString())` to `if(a.getText().toString().equals(c.getText().toString()))` and change `if((i == o) && (i != ""))` to `if((i.equals(o)) && (!"".equals(i)))` Because when you use `==` as an operator, you are checking if the two objects are the same by reference, but `equals` for a String actually matches the two contents. – EpicPandaForce Jul 29 '14 at 10:04
  • Also if you're curious about how to use Fragments (you'll eventually have to learn them later on), you should check out http://codereview.stackexchange.com/questions/57543/why-does-the-new-adt-create-a-static-inner-class-fragment-by-default or http://stackoverflow.com/questions/24840509/why-does-the-new-adt-create-a-static-inner-class-fragment-by-default-simple-fr (they are the same) – EpicPandaForce Jul 29 '14 at 10:06

3 Answers3

2

Try this used .equals() method for String comparison

 if(a.getText().toString().equals(c.getText().toString()))
M D
  • 47,665
  • 9
  • 93
  • 114
0

in your code instead of

a.getText().toString() == c.getText().toString()

use

a.getText().toString().equals(c.getText().toString())

you can't compare two strings the way are you doing

Pramod Yadav
  • 2,316
  • 2
  • 23
  • 36
-1

You should remove final. Instead use nothing or public

JoppeD
  • 95
  • 1
  • 7