6

I have a button on which (like) is written from string resource, on click I want to toggle like to unlike & vice verse.

How to compare button contain like or unlike from string resource?

My string.xml contains

<string name="like">Like</string>
<string name="unlike>unlike</string>
0

6 Answers 6

11

Try this..

bt.setText(getString(R.string.txt1));
bt.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        myStr = bt.getText().toString();
        if((myStr.equals(getString(R.string.txt1)))) {
            bt.setText(getString(R.string.txt2));
        } else if ((myStr.equals(getString(R.string.txt2)))) {
            bt.setText(getString(R.string.txt1));
        }
    }
});

In string.xml

<string name="txt1">11111111111</string>
<string name="txt2">2222222222222222</string>
Sign up to request clarification or add additional context in comments.

Comments

3

You can use getString method

 if(btn1.getText().toString().compareTo(getResources().getString(R.string.app_name)) == 0)
    {
      // do some code here
    }    

1 Comment

say button id is btn1 & in string resource string name are like & unlike then
2

use below code

 Button button = (Button)findViewById(R.id.btn1);

if(button.getText().toString().equals(getString(R.string.like)))
{
    button.setText(getString(R.string.unlike));
}
else
{
    button.setText(getString(R.string.like));
}

Comments

0

try as:

String text = getResources().getString(R.string.texttest);
Button button = (Button)this.findViewById(R.id.btntest);
button.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
    if(button.getText().toString().equals(text))
    {
      // do some code here
    }
  }
});

Comments

0

You can retrieve the label (Text on Button) in this manner.

Button button=(Button) findViewById(R.id.button1);
Log.d("Text",""+button.getText());

Comments

0

You can do this::

String value=buttonname.getText().toString   

//this Will Give the Text On the Button  

Now compare the value like this Way and change the Name Of the Button::

if(value.equals("Like"))  
 {
   buttonname.setText("UnLike"); 
 }
else
{
  buttonname.setText("Like");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.