-3

I have a problem in my code in android studio, I created it to say "Hello" when the person "abc" writes but that didn't work . can u plz help me. here is my codes

final Button butt=(Button)findViewById(R.id.butt);

butt.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
          final EditText frag =(EditText)findViewById(R.id.frag);
final TextView hello=(TextView)findViewById(R.id.hello);
      String verb =frag.getText().toString();
if (verb=="abc"){
            hello.setText("Hello");
lrleon
  • 2,610
  • 3
  • 25
  • 38
Mhd Ari
  • 13
  • 1

2 Answers2

1

Because the condition in if block returns false Use this code instead.

final Button butt = (Button) findViewById(R.id.butt);

butt.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       final EditText frag = (EditText) findViewById(R.id.frag); 
       final TextView hello = (TextView) findViewById(R.id.hello); 
       String verb = frag.getText().toString();
       if ("abc".equals(verb)) {
           hello.setText("Hello");
       }
    }
}
Son Truong
  • 13,661
  • 5
  • 32
  • 58
0

When comparing string, always use .equals method

Example:

String str1 = "yourstring1";
String str2 = "yourstring2";

if(str1.equals(str2))//return false
{...}
francis
  • 1
  • 2