Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting textSize in custom view results in huge text

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.

like image 717
loeschg Avatar asked Oct 20 '14 18:10

loeschg


People also ask

Which method is used to change the text size of the text view?

Use sp for text size… because but it is scaled by the user's font size preference. Use dp for everything else.

How do I change font size in XML?

In the layout XML file, set the fontFamily attribute to the font file you want to access. Open the Properties window to set the font for the TextView . Select a view to open the Properties window. Note: The Properties window is available only when the design editor is open.

Which attribute increases or decreases the font size of TextView?

app:autoSizeMinTextSize=”10sp” using this attribute the TextView will be resized up to the size of 10sp and app:autoSizeStepGranularity=”2sp” using this attribute we are uniformly reducing the size of the TextView as 2sp when it goes out of the screen.


2 Answers

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); } 
like image 130
plackemacher Avatar answered Sep 28 '22 16:09

plackemacher


@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); } 
like image 23
Daniel Luche Avatar answered Sep 28 '22 17:09

Daniel Luche