Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with changing the font size

Tags:

java

android

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?

like image 215
DrkStr Avatar asked Dec 07 '25 09:12

DrkStr


1 Answers

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)

like image 132
Ken Wolf Avatar answered Dec 09 '25 23:12

Ken Wolf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!