John Roberts
John Roberts

Reputation: 5966

Font Sizes Not Adjusting To Different Screen Sizes

I have read the Android articles about supporting different screen sizes via using "match_parent" and "wrap_content", as well as "sp" and "dp", and relative layout. I have implemented all those suggestions, but still when I change the size of the screen to say, tablet, in the XML editor of Eclipse, the font sizes of the TextViews and Buttons do not seem to change at all to adapt to the the larger screen size. The same goes for smaller screens (such as 3.2 inch). Does anyone have any suggestions?

Upvotes: 0

Views: 2113

Answers (3)

john.k.doe
john.k.doe

Reputation: 7563

to borrow heavily from @CSmith's answer (but not wanting to edit his answer, because it is valid):

In a layout:

<TextView
 android:id="@+id/yourID"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="My String"
 style="@style/myStyle"
/> 

values/styles.xml:

<?xml version="1.0" encoding="utf-8" ?> 
<resources>
 <style name="myStyle">
  <item name="android:textStyle">bold</item> 
  <item name="android:textColor">#FFFFFFFF</item> 
  <item name="android:textSize">@dimen/my_textsize</item> 
  <item name="android:shadowColor">#FF000000</item> 
  <item name="android:shadowDx">1</item> 
  <item name="android:shadowDy">-1</item> 
  <item name="android:shadowRadius">1</item> 
 </style>
</resources>

values/dimens.xml

<resources>    
    <dimen name="my_textsize">14sp</dimen>
</resources>

values-xlarge/dimens.xml

<resources>    
    <dimen name="my_textsize">20sp</dimen>
</resources>

this will give you a 14sp font on normal devices, 20sp on x-large devices, and cuts down on duplication in separate XML files, since the only thing that's changing is the font-size .

you can put anything that's going to be an sp/dp dimension in a dimens.xml file as a .

Upvotes: 3

CSmith
CSmith

Reputation: 13458

In a layout:

<TextView android:id="@+id/yourID" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="My String" style="@style/myStyle" /> 

values/styles.xml:

<?xml version="1.0" encoding="utf-8" ?> 
<resources>
 <style name="myStyle">
  <item name="android:textStyle">bold</item> 
  <item name="android:textColor">#FFFFFFFF</item> 
  <item name="android:textSize">14sp</item> 
  <item name="android:shadowColor">#FF000000</item> 
  <item name="android:shadowDx">1</item> 
  <item name="android:shadowDy">-1</item> 
  <item name="android:shadowRadius">1</item> 
 </style>
</resources>

values-xlarge/styles.xml:

<?xml version="1.0" encoding="utf-8" ?> 
<resources>
 <style name="myStyle">
  <item name="android:textStyle">bold</item> 
  <item name="android:textColor">#FFFFFFFF</item> 
  <item name="android:textSize">20sp</item> 
  <item name="android:shadowColor">#FF000000</item> 
  <item name="android:shadowDx">1</item> 
  <item name="android:shadowDy">-1</item> 
  <item name="android:shadowRadius">1</item> 
 </style>
</resources>

this will give you a 14sp font on normal devices, 20sp on x-large devices

Upvotes: 2

sdabet
sdabet

Reputation: 18670

The point of using density-independent units such as 'sp' and 'dp' is that the text size actually remains the same from a user point of view, whatever the screen.

So, when you change the screen in the XML editor, the displayed screen area changes but the text keeps the same size.

Upvotes: 1

Related Questions