3

I recently ran some testing on the AWS device farm and was shocked to find that my text sizes are not scaling appropriately.

Currently, I have different folders for my text sizes:

values
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="fab_margin">16dp</dimen>
<dimen name="center_indentation">80dp</dimen>
<dimen name="poll_question_text_size">14dp</dimen>
<dimen name="margin_left">80dp</dimen>
<dimen name="margin_bottom">8dp</dimen>
<dimen name="radio_button_answer_text_size">12dp</dimen>
<dimen name="answer_label_text_size">6dp</dimen>
<dimen name="answer_value_label_text_size">8dp</dimen>
<dimen name="vote_count_label_text_size">11dp</dimen>

values-sw600dp
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="fab_margin">16dp</dimen>
<dimen name="center_indentation">80dp</dimen>
<dimen name="poll_question_text_size">22dp</dimen>
<dimen name="margin_left">160dp</dimen>
<dimen name="radio_button_answer_text_size">14dp</dimen>
<dimen name="margin_bottom">16dp</dimen>
<dimen name="answer_label_text_size">9dp</dimen>
<dimen name="answer_value_label_text_size">10dp</dimen>
<dimen name="vote_count_label_text_size">13dp</dimen>

values-sw720dp
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="fab_margin">16dp</dimen>
<dimen name="center_indentation">80dp</dimen>
<dimen name="poll_question_text_size">30dp</dimen>
<dimen name="margin_left">185dp</dimen>
<dimen name="margin_bottom">20dp</dimen>
<dimen name="radio_button_answer_text_size">18dp</dimen>
<dimen name="answer_label_text_size">10dp</dimen>
<dimen name="answer_value_label_text_size">12dp</dimen>
<dimen name="vote_count_label_text_size">14dp</dimen>

In each folder, I create a dimens.xml file that I used to pull the appropriate text size for a given screen.

I went to the device metrics website located here and found was specifically looking at the Google Nexus 5. It appears that the Nexus 5, after I run my emulator, is pulling from my sw600dp folder. I am not sure why, and it is generating a text size that I would hope to see on a 7" tablet and not a 4.5" handheld device.

I have read countless posts on StackOverflow but cannot find any way to properly account for all devices. I understand that there are 6", 7", 8", 9", and 10" devices, and I want to ensure that I am accounting for each size.

Here is how my text looked for my Google Nexus 5, note I am referring to the radio_button_ansewr_text_size item:

enter image description here

tccpg288
  • 3,242
  • 5
  • 35
  • 80

1 Answers1

1

When specifying text size, always use sp:

<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />

or

<dimen name="text_size">13sp</dimen>

http://developer.android.com/training/multiscreen/screendensities.html

If you need to set text sizes in java code, do it this way:

textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
Mussa
  • 1,463
  • 21
  • 25
  • I don't think the dimens folder allows for sp references, also I am setting the size programatically – tccpg288 Feb 22 '16 at 21:33
  • I believe **sp** will be allowed in dimens xml files, I was using them for years now, always worked :) – Mussa Feb 22 '16 at 21:34
  • It worked, is the same logic true for margins (COMPLEX_UNIT_DIP)? – tccpg288 Feb 22 '16 at 22:34
  • @tccpg288 Nope, this will not work for margins, paddings, width and height, because android renders UI according to xml file. Changing a view programmatically will be pain in the ars. Also, you better not change text size in java code, because size of a text view will not be changing as well, as it is static. – Mussa Feb 22 '16 at 22:40