Allen Edwards
Allen Edwards

Reputation: 1552

making fonts get larger on larger screens

I have searched Stackoverflow and google and what I find is answers for people who want fonts to remain the same size but I want them to get bigger when the screen gets bigger.

Specifically, I want very large fonts in my application. On my phone, they are 1/2 inch high - perfect. My problem is that on my 7 inch tablet they are also 1/2 inch high and I want them to scale up and be about 1 inch high. I have tried sp and dp modes and both just keep the fonts the same physical height, which is not what I want. I see there is something new for tablets with 3.2 and higher but I want to support devices from 2.3 and higher. I have seen complex code that says it auto scales fonts to fit the text in a width but that is not what I need. I want the fonts to be the equivalent of say 100sp on a phone and 200sp on the tablet. I have no graphics and simple relative layouts with very few elements on the screen. I just need to make a couple of the textsizes larger for large screens. I would think it would be simple but I can't find it.

Here is a typical text view entry

    <TextView
    android:id="@+id/textDistance"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/labelDistance"
    android:layout_centerHorizontal="true"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#ffffff"
    android:textSize="100sp" />

Is there a relatively simple straight forward way to do this?

Upvotes: 5

Views: 4928

Answers (2)

Allen Edwards
Allen Edwards

Reputation: 1552

I went with the method Here: http://developer.android.com/guide/topics/resources/more-resources.html#Dimension and added one line to the dimens.xls files:

<resources>

<dimen name="padding_small">8dp</dimen>
<dimen name="padding_medium">16dp</dimen>
<dimen name="padding_large">16dp</dimen>
<dimen name="font_size">200sp</dimen>

The above is the file in views-large. In the views file I have 100sp instead of 200sp.

Now the font is large on my tablet. According to the documentation, this may be a problem with some of the new phones, the 5 inch ones and that is why there is some way to deal with this in the newer versions of Android but as I want this to work on older phones and 7 inch tablets, this will solve my problem. The solution above, which led me to this, would also work I am sure, I just went with this as it seemed simpler and was pretty well documented by Google.

In my layout file, I just changed the specific callout of font size to this:

    <TextView
    android:id="@+id/textDistance"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/labelDistance"
    android:layout_centerHorizontal="true"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#ffffff"
    android:textSize="@dimen/font_size" />

Upvotes: 3

Jin35
Jin35

Reputation: 8612

'dp' or 'dip' dimensions - specially made for been the same on different screens. So, there is not any special dimension for you task.

For implementing different text sizes you have to create several styles, and put them in folders values-xlarge, values-large, values-normal and values-small.

One style will looks like this:

<style name="BigTextStyle">
    <item name="android:textSize">50dp</item> <!-- different values in different folders  -->
</style>

And in your text view just provide style reference:

<TextView 
    style="@style/BigTextStyle"
    ...
/>

Upvotes: 5

Related Questions