1

I need to check if the text size on a button have the size 37sp. I found a method to get the size with: .getTextSize(), but the size with I receive is: 126.0, and not 48sp. The code off button:

<Button
    android:id="@+id/buttoneql"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="="
    android:textColor="@android:color/black"
    android:textSize="48sp" />

The test:

buttoneql = (Button) currentActivity.findViewById(R.id.buttoneql);
assertEquals( 48.0f, buttoneql.getTextSize() );

How I should do to recive the ral value of the text size?

Roberto Pinheiro
  • 1,260
  • 13
  • 30

3 Answers3

4

getTextSize() return px float

This is code for the convert PX to SP format.

view.setTextSize(TypedValue.COMPLEX_UNIT_PX, 24);
Saurabh Bhandari
  • 2,438
  • 4
  • 26
  • 33
amralsaidy
  • 183
  • 1
  • 15
2
     float oldSize=buttoneql.getTextSize();

text size in float

    Log.d("size in float",""+oldSize);
    float dp= getResources().getDisplayMetrics().scaledDensity;

float value of scale density

    float newSize=oldSize/dp;

textsize in sp = new size

    Log.d("newSize in sp-->",""+newSize);

compare float value of text size and new size

    if(48.0==newSize){
        Log.d("equal size-->","true");
    }
    else{
        Log.d("equal size-->","false");
    }
Gurupreet
  • 39
  • 5
1

I solved the problem by multiplying the value I need (128.0) by 2.625 and rounding it up correctly to make it an integer.

int size = (int) Math.round (48.0 * 2.625);

And after, comparing to the receive value:

assertEquals(size, (int) buttoneql.getTextSize());
Roberto Pinheiro
  • 1,260
  • 13
  • 30