0

I cannot figure this one out, but I cannot seem to get android to ever cooperate with simple math. I have created a ZoomControls that changes text size of a TextView. Seems simple. I increase the font size no problem. Subtracting just adds it for some weird reason.

zoom.setOnZoomOutClickListener(new View.OnClickListener() {
    public void onClick(View v) {
       float val = songtext.getTextSize();
       float test = (val - 1);
       songtext.setTextSize(test);
    }
});

Judging by this simple code, the font size should be subtracted by 1 everytime the button is pressed. Instead it increases by 1. ??? I am ripping my hair out.

mskfisher
  • 3,291
  • 4
  • 35
  • 48
NJGUY
  • 2,045
  • 3
  • 23
  • 43

1 Answers1

0

The difference here is that in the setTextSize(int size) method, the unit type by default is "sp" or "scaled pixels". This value will be a different pixel dimension for each screen density (ldpi, mdpi, hdpi).

getTextSize(), on the other hand, returns the actual pixel dimensions of the text.

You can use setTextSize(int unit, int size) to specify a unit type. The values for this can be found in the TypedValue class, but some of them are:

TypedValue.COMPLEX_UNIT_PX : Pixels

TypedValue.COMPLEX_UNIT_SP : Scaled Pixels

TypedValue.COMPLEX_UNIT_DIP : Device Independent Pixels

As explained at TextView.setTextSize behaves abnormally - How to set text size of textview dynamically for different screens by kcoppock

I simply used the below:

textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,size + increaseTextBy);

Community
  • 1
  • 1