DrkStr
DrkStr

Reputation: 1932

Trouble with changing the font size

HI everyone, I am trying to allow my user to change the font size(increase or decrease) in my activity's EditText by pushing a button in the action bar. I have gotten the font size increase to work but for some reason the font size decrease button makes the font size increase as well.

I am attaching (what I think is) the relevant code. Let me know if you need to see anyother buts of code.

public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
        case R.id.TEXT_UP:
            doTextUp();
            break;
        case R.id.TEXT_DOWN:
            doTextDown();
            break;
         default:
                return super.onOptionsItemSelected(item);
            }
            return false;
        }




private void doTextDown() {
    mBodyText = (EditText) findViewById(R.id.body);
    float Textsize = mBodyText.getTextSize() - 1;
    mBodyText.setTextSize(Textsize);
    Toast.makeText(getApplicationContext(), "in the text down",Toast.LENGTH_SHORT).show();

}

private void doTextUp() {
    mBodyText = (EditText) findViewById(R.id.body);
    float Textsize = mBodyText.getTextSize() + 1;
    mBodyText.setTextSize(Textsize);
    Toast.makeText(getApplicationContext(), "in the text up",Toast.LENGTH_SHORT).show();

}


Any suggestion?

Upvotes: 3

Views: 173

Answers (2)

user2497624
user2497624

Reputation: 159

From your description of the problem, i can not finda problem in the piece of code you provided. I am pretty sure, that your problem comes from a copy past (we programmers are usualy lazy... when we do a copy past it often happens that we forget to change a variable name and we end up with these problems). Look at the event listener on your buttons and see if youdid set up both item cases you have (text_up) and (text_down).

Upvotes: 0

Ken Wolf
Ken Wolf

Reputation: 23269

Try changing both to:

mBodyText.setTextSize(TypedValue.COMPLEX_UNIT_PX, Textsize);

getTextSize() returns the size in pixels but setTextSize(float size) interprets it as a "scaled pixel" (sp) unit. To specify pixels you need to use setTextSize (int unit, float size)

http://developer.android.com/reference/android/widget/TextView.html#getTextSize() http://developer.android.com/reference/android/widget/TextView.html#setTextSize(float)

Upvotes: 3

Related Questions