Reputation: 295
In my calculator app, I want an Inverse button, which when clicked changes the text of other buttons. Like sin to sin inverse etc. Is this possible?
Upvotes: 0
Views: 1036
Reputation: 22080
While the other answers are completely right showing you to how rename the buttons, I suggest another solution with a cleaner design: Have different buttons for "sin" and "sin inverse", and just make them visible/invisible when clicking the "Inverse" button. That way you can write clean click handlers and don't have to use a lot of "if (isInverseMode()...)".
To make that work correctly, you just declare some additional buttons for the inverse operations in your XML layout file and set them to android:visibility="gone"
.
If you then set one the visible buttons to invisible and the next insivible button besides it to visible in the code, then the effect for the user looks like you exchanged one button by the other (so he only notices the text of the button changing).
Upvotes: 1
Reputation: 7071
This code may help you..
final Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btn.setText(new StringBuilder(btn.getText().toString().trim()).reverse());
}
});
Upvotes: -1
Reputation: 452
You can just change the text of a button in that button onclick event. Say For example
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(btn.getText().toString().trim().equals("sin")){
btn.setText("sin inverse");
}else if(btn.getText().toString().trim().equals("sin inverse")){
btn.setText("sin");
}
}
});
I think this will help you
Upvotes: 2
Reputation: 16393
It's possible.
Just re-set the button text in your onClick method for the button.
buttonId.setText("Your button text");
From what you're saying though it sounds like you want to change the buttons function as well as the text... in which case you need to put an if statement in your onClick method to handle the two button states.
Upvotes: 0