14

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"

 />
Vins
  • 4,089
  • 2
  • 35
  • 50

3 Answers3

42

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

Community
  • 1
  • 1
Christoph
  • 851
  • 8
  • 14
  • Hi Christoph, I done like that but in my case values are not taken from vaues-xlarge folder. So can you help me where is I am going mistake? – Kalpesh Nov 29 '12 at 06:39
  • can u tell me when the default "value" is chosen to run? i mean the device which is small android choose the "values-small" and like that for large, xlarge etc. but for which case the "value" folder is going to be used? – Kailash Dabhi Jan 04 '13 at 06:50
  • for all other, that you do not have specified. It is useful for example if you only specify values-large and values; for large screens it will use items from values-large, and for all others from "values" – Axarydax Jan 04 '13 at 15:22
  • It also work if have layouts only in layout/ folder and dimens.xml define in all values folders +1 for basic clarification... – MobileEvangelist Apr 15 '13 at 13:39
2

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.

Martyn
  • 16,432
  • 24
  • 71
  • 104
  • Yes, but Android will choose which XML to use at runtime, making it actually more efficient than doing everything programatically. You should *never* have to do anything programatically, unless you absolutely have to. So having multiple XML files for small, normal, large & xlarge are much better. – Connor McFadden Apr 08 '15 at 16:22
  • Another, perhaps better, way - if you can - is to have one layout, but different dimens.xml. Keeping multiple layout files in sync is a pain. – pstorli Jun 14 '18 at 18:24
2

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.

inazaruk
  • 74,247
  • 24
  • 188
  • 156