Reputation: 95
I have two Button
for increase and decrease the text size of a TextView
.
They both work, but: if I increase and then decrease, the first click on the decrease button will increase the text, and the second and so on will decrease. And the other way around.
This is my code:
int txtSize = 18;
volumeUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mTextView.setTextSize(txtSize++);
}
});
volumeDown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mTextView.setTextSize(txtSize--);
}
});
Upvotes: 0
Views: 43
Reputation: 46
Man. Try this. Avoid to increase or decrease a variable like that when you use it multiple time. Your code set first txtSize in TextView before increase it. So in the second button, you set the increased value before decrease it.
int txtSize = 18;
volumeUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtSize = txtSize+1
mTextView.setTextSize(txtSize);
}
});
volumeDown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtSize = txtSize-1
mTextView.setTextSize(txtSize);
}
});
Upvotes: 1