androidxmltexttext-sizenexus-5

Adjusting Text for Screen Sizes - Google Nexus 5 Example


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


Solution

  • 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);