0

Here I am trying to get txt from EditText field , and compare it with another string that I defined in the code, after the compare done I am gonna show some messages that sys " they are equal " just I wann use it for another purpose. I tried this code it prints what I write in the EditTex into TextView. but it did not compare i what I type in they r equal or not. Plz help

         final Button btntx = (Button)findViewById(R.id.btn1);
            final EditText edittxt1 = (EditText)findViewById(R.id.edttxt);
            final TextView messageshow= (TextView)findViewById(R.id.txtview);       

            btntx.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Button b= (Button)v; 
                    String data= edittxt1.getText().toString();
                    String takefromtost= "Turn on";

                messageshow.setText("  " + data);

                if (data=="Turn on")
                    {
  Toast.makeText(getBaseContext(),"They are equal", Toast.LENGTH_SHORT).show(); 
                messageshow.setText("They are equal");

                    }


                }
            });
  • Use equals to compare String objects. See http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – copeg Apr 08 '15 at 21:19

1 Answers1

1

You have to use equals():

if (data.equals("Turn on"))
Lawrence Aiello
  • 4,560
  • 5
  • 21
  • 35