Setting textSize in custom view results in huge text

AndroidAndroid LayoutAndroid Custom-View

Android Problem Overview


I'm calling the following in the constructor of my custom view:

private void style(Resources.Theme theme, AttributeSet attrs) {
    TypedArray a = theme.obtainStyledAttributes(
            attrs,
            R.styleable.StackedTextView,
            0, 0);

    try {
        DebugTool.assertTrue(holdr != null, "View holder has not been properly intialized.");
        String line1 = a.getString(R.styleable.StackedTextView_line1);
        setLine1(line1);
        String line2 = a.getString(R.styleable.StackedTextView_line2);
        setLine2(line2);

        line1Size = a.getDimension(R.styleable.StackedTextView_line1_textSize, 20);
        line2Size = a.getDimension(R.styleable.StackedTextView_line2_textSize, 20);
        if (line1Size > 0) {
            holdr.textLine1.setTextSize(line1Size);
        }
        if (line2Size > 0) {
            holdr.textLine2.setTextSize(line2Size);
        }

    } finally {
        a.recycle();
    }
}

It's supposed to set text and text size for 2 textfields.

I have the following in my attr.xml in addition to the string formats for the text content (which works fine).

    <attr name="line1_textSize" format="dimension" />
    <attr name="line2_textSize" format="dimension" />

When I use this view and set the text size using a dimension via xml,

        <com.me.app.view.component.StackedTextView
            android:id="@+id/overview_total_reviews"
            app:line1="40"
            app:line2="Rating"
            style="@style/OverviewStackedText"
            app:line1_textSize="10sp"
            app:line2_textSize="12sp"
            />

the text ends up being significantly larger than expected. I'm only setting 10 and 12sp respectively, and the text sizes are closer to like 30sp.

Can anyone see what I'm doing wrong? Do I need to do something with DisplayMetrics to make sure things are scaled properly?

Edit: Adding some clarification

The dimension IS getting picked up. The text does change when I set the different text sizes using my custom attribute (in xml). I've also tried using getDimensionPixelSize.

It's as if the calculation/dimension retrieval is wrong. 1sp (or dp) change results in a significant change.

Android Solutions


Solution 1 - Android

Try the following instead:

line1Size = a.getDimensionPixelSize(R.styleable.StackedTextView_line1_textSize, 0);
line2Size = a.getDimensionPixelSize(R.styleable.StackedTextView_line2_textSize, 0);

if (line1Size > 0) {
    holdr.textLine1.setTextSize(TypedValue.COMPLEX_UNIT_PX, line1Size);
}
if (line2Size > 0) {
    holdr.textLine2.setTextSize(TypedValue.COMPLEX_UNIT_PX, line2Size);
}

Solution 2 - Android

@loeschg I had the same problem, but I finally managed to solve using @plackemacher 's reply more that link

Method .getDimensionPixelSizealways() returns the value in pixel, so you have to use convertPixelsToDp method to get the dp value.

Here is the code that can help you:

 /**
 * This method converts device specific pixels to density independent pixels.
 * 
 * @param px A value in px (pixels) unit. Which we need to convert into db
 * @param context Context to get resources and device specific display metrics
 * @return A float value to represent dp equivalent to px value
 */
public static float convertPixelsToDp(float px, Context context){
    return px / ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT);
}

line1Size = convertPixelsToDp(a.getDimensionPixelSize(R.styleable.StackedTextView_line1_textSize, 0), some_context);
line2Size = convertPixelsToDp(a.getDimensionPixelSize(R.styleable.StackedTextView_line2_textSize, 0), some_context);

if (line1Size > 0) {
    holdr.textLine1.setTextSize(TypedValue.COMPLEX_UNIT_PX, line1Size);
}
if (line2Size > 0) {
    holdr.textLine2.setTextSize(TypedValue.COMPLEX_UNIT_PX, line2Size);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionloeschgView Question on Stackoverflow
Solution 1 - AndroidplackemacherView Answer on Stackoverflow
Solution 2 - AndroidDaniel LucheView Answer on Stackoverflow