Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different text size for different hardware

Tags:

android

I have a edit text layout as below and I want to know, is there any way to provide different size for it for different hardware depending on its screen size? For screens below 4 inches I want to give the below layout

 <EditText
    android:id="@+id/entry"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/editbox_background"
    android:textColor="#800080"
    android:text=""
    android:hint=" No Operator Precednce yet"
    android:cursorVisible="false" 
    android:textSize="40dip"

 />

and then for others

 <EditText
    android:id="@+id/entry"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/editbox_background"
    android:textColor="#800080"
    android:text=""
    android:hint=" No Operator Precednce yet"
    android:cursorVisible="false" 
    android:textSize="30dip"

 />
like image 915
Vins Avatar asked Aug 03 '11 13:08

Vins


3 Answers

Create folders in /res for

layout

layout-small

layout-normal

layout-large

layout-xlarge

then create a layout file for each layout, android will choose the right one on runtime. so you can change the complete layout for each screensize.

If you really only want to set the size, you can create this folders

values

values-small

values-normal

values-large

values-xlarge

then put a dimen.xml in each of this folders like this :

<?xml version="1.0" encoding="utf-8"?>

<resources>

<dimen name="textSizeLarge">22dp</dimen>

</resources>

just change the value (22dp) in each xml file.

then replace your value with

android:textSize="@dimen/textSizeLarge"

Very good documantation : http://developer.android.com/guide/practices/screens_support.html

like image 125
Christoph Avatar answered Oct 23 '22 10:10

Christoph


If you use <dimen> in a configuration dependent way, you will override default size for all devices (22dp for smaller, 40dp for larger devices). If you want to leave textSize as it was by default for smaller devices, and only override it for larger devices, you can use styles:

res/values/styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="EditText" />      

</resources>

res/values-large/styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="EditText" >
      <item name="android:textSize">40sp</item>
    </style>    
</resources>

And in the layout:

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

This way textSize is left untouched for smaller devices, and default value from current theme is used.

like image 24
inazaruk Avatar answered Oct 23 '22 09:10

inazaruk


EDIT - the best way of doing this is to define the text sizes as a dimen and vary the sizes depending on what hardware you are targeting by putting different values in the dimens.xml file in your different dpi (ldpi, mdpi, hdpi etc) bucket folders.

Old (correct but not great) answer

2 ways of doing this :

  1. Remove the definition from the XML and set the text size programatically

    ((EditText) findViewById(R.id.entry)).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 40)
    
  2. implement different layouts for the different screen sizes - this will involve you writing a new xml layout for each of the different screen sizes you support in your application.

like image 2
Martyn Avatar answered Oct 23 '22 09:10

Martyn