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 bel
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.
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 :
Remove the definition from the XML and set the text size programatically
((EditText) findViewById(R.id.entry)).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 40)
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.
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