3

I am currently creating an android application with different options. One of the option would be to have a button that would show "Activate" as default. When the application would be running, clicking on it would change it to "Disable" and then to "activate" if clicked again. I believe that all I have to do is to .getText with a string variable then use this variable in a if statement but it seems like it is not reacting to any of my conditions...

        final Button button = (Button) findViewById(R.id.bSensor);


    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click

            String buttonText = button.getText().toString();    
            if (buttonText == "@string/Disable") {
                button.setText(R.string.Enable);
            }
            else if (buttonText == "@string/Enable"){
                button.setText(R.string.Disable);
            }

        }
    });

Thanks for help

Phyzikk

Phyziik
  • 515
  • 1
  • 5
  • 8

2 Answers2

2

You shouldn't use the == operator when comparing strings in Java. Source

You should either use the .equals() method of the string, or alternatively you could keep a global boolean state flag to determine which value is set. This way you won't need to do a string compare every time you need to figure out if it's active or disabled.

Community
  • 1
  • 1
JoshBramlett
  • 723
  • 1
  • 6
  • 11
  • make sure when you use the `.equals` you are calling it on the literal string. ie `"Enable".equals(buttonText)` instead of `buttonText.equals("Enable")`. It will prevent NullPointerExceptions. – toadzky Nov 08 '12 at 19:50
  • I'm not sure how i could do a boolean state flag :S and toadzky, how am i suppose to do this "Enable".equals(buttonText) line, Enable is a string res – Phyziik Nov 08 '12 at 20:17
  • Note the quotation marks. "Enable" is a string literal, R.string.Enable is your resource. – JoshBramlett Nov 08 '12 at 20:51
  • Thanks to you, I was trying to work with the references to strings that I made but for some reason, the only way I found was to bring up the content of the string to replace the "Enable", which was "Activate Sensor". Thank you for the help! – Phyziik Nov 09 '12 at 01:36
1

Use .equals to compare strings. You wont need the @String/ prefix as this is not part of what the button displays.

final Button button = (Button) findViewById(R.id.bSensor);

button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Perform action on click

        String buttonText = button.getText().toString(); 

        if (buttonText.equals(getResources().getText(R.string.Disable)) {
            button.setText(R.string.Enable);
        }
        else if (buttonText.equals(getResources().getText(R.string.Enable)){
            button.setText(R.string.Disable);
      }

    }
});
IAmGroot
  • 13,760
  • 18
  • 84
  • 154
  • I tried this way, and I wouldn't get any reaction from the button. – Phyziik Nov 08 '12 at 20:29
  • @Phyziik Have you debugged to find out what buttonText holds? Maybe add `.trim` if there is whitespace.. – IAmGroot Nov 08 '12 at 22:19
  • Sadly, I didn't get the chance to understand the debugg mode yet, went around, videos and tutorial and still seems pretty hard to understand. I've used this kind of tools in other programs but can't get to understand how the variables work and stuffs work. Thanks tho! – Phyziik Nov 09 '12 at 01:38
  • @Phyziik From your comments it is clear that your resource doesnt contain the text we both assumed. See my editted code for a reliable way of programming a check against the string resource. This will ensure exact comparison. – IAmGroot Nov 09 '12 at 07:58