Sophie
Sophie

Reputation: 2634

Compare EditText Value with String

I just wanna compare value of EditText, and want to show Toast according to case, in my program i simply want if edittext value equals to abc or ABC then show done in Toast else need to show error, and for that i have written few lines of easy code, but don't why its always showing error, even i have entered correct value (i.e:- ABC or abc)

public class MainActivity extends ActionBarActivity {

EditText editTextInput;
String strInput;
Button btnInput;

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

        editTextInput = (EditText) findViewById(R.id.editInput);
        strInput = editTextInput.getText().toString();

        btnInput = (Button) findViewById(R.id.button1);
        btnInput.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                if(strInput.equalsIgnoreCase("ABC"))
                {
                    Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_LONG).show();
                }
                else {
                    Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();         
                }
            }
        });
    }

Upvotes: 0

Views: 13783

Answers (5)

Pratik Dasa
Pratik Dasa

Reputation: 7439

Using equalsIgnoreCase method, will reduce one OR condition as per @priyanka's answer.

 btnInput.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            strInput = editTextInput.getText().toString();
            if(strInput.equalsIgnoreCase("ABC"))
                Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_LONG).show();
             else 
                Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();         
            }
        });

Upvotes: 0

Hariharan
Hariharan

Reputation: 24853

Try this.

Get the EditText text inside ClickListener like below

btnInput.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            strInput = editTextInput.getText().toString();

EDIT

Initialize it with empty like below

String strInput = "";

and Button click add like below append it

strInput = strInput+","+editTextInput.getText().toString();

and set the strInput into TextView like below

textViewResult.setText(strInput);

EDIT 1:

btnInput.setOnClickListener(new OnClickListener() {

       @Override
       public void onClick(View arg0) {
             String check = editTextInput.getText().toString();

              if(check.equalsIgnoreCase("ABC"))
              {
                        strInput = strInput+","+check;
                        Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_LONG).show();
                        textViewResult.setText(strInput);
                        editTextInput.setText("");
              }
              else {
                        Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();                    
              }
       }
});

Upvotes: 1

Priyanka
Priyanka

Reputation: 675

Just get the value of EditText inside ClickListener.

Try like below-

btnInput.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        strInput = editTextInput.getText().toString();
        if(strInput.equals("ABC")||strInput.equals("abc"))
            Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_LONG).show();
         else 
            Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();         
        }
    });

Upvotes: 1

Sachin C
Sachin C

Reputation: 141

it is check when you add text in edit text

EditText input = new EditText(context);

input.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
if((input.getText().toString()).equal(comprator)) {
showToast();
} }
@Override
public void onTextChanged(CharSequence s, int st, int b, int c) {

       }  

public void beforeTextChanged(CharSequence s, int st,int c, int a) {

                  }
              });

Upvotes: 0

Volodymyr Kulyk
Volodymyr Kulyk

Reputation: 6546

if (editTextInput.getText().toString().equals("ABC"))
{
//...
}

Upvotes: 0

Related Questions