Iura
Iura

Reputation: 95

Button for text size works after 2 clicks

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

Answers (2)

Savastaro Vegas
Savastaro Vegas

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

J. One
J. One

Reputation: 91

Try to use ++texSize instead of using texSize++

Upvotes: 0

Related Questions