user3677009
user3677009

Reputation: 15

How to compare strings using textview and buttons

I am new to Java and having some difficulty comparing two strings inside a textView. Basically when a user clicks the fillfive button I want the method to set the text to whatever argument is relevant.

For example the default string is "0" so when the user clicks the fillfive button if the string is "0" then it will set the string to "5" else if the string is already "5" it will set the string back to "0".

Any pointers in the right direction would be most welcome, thanks.

Currently in the code below this does not work because I only seem to ever get 0 so I'm guessing for some reason it's not checking the if statement properly.

public void fillfive(View v) {
    if (v.toString().equals("0")){
        TextView view = (TextView) findViewById(R.id.txt_fivecup);
        view.setText("5");
    }
    else{
        TextView view = (TextView) findViewById(R.id.txt_fivecup);
        view.setText("0");
    }
}

Upvotes: 0

Views: 1764

Answers (1)

user184994
user184994

Reputation: 18271

When you call v.toString(), you're not actually getting any user input. You need to replace the View parameter with a TextView

public void fillfive(View v) {
    TextView textViewA = (TextView) findViewById(R.id.the_text_view_you_want_to_check);
    TextView textViewB = (TextView) findViewById(R.id.txt_fivecup);     

    if (textViewA.getText().toString().equals("0")){
        textViewB.setText("5");
    }
    else{
        textViewB.setText("0");
    }
}

Upvotes: 1

Related Questions