Android font size on different screens - android

I want to use different font size for different screen sizes.
I read about this many articles, but I'm not sure about usage. Is correct to use different dimens resource file for different screen dimensions like code below:
res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-xlarge/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">18sp</dimen>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">24sp</dimen>
</resources>
...
I also read that correct way is to use sp for font size, but this doesn't fit font in different screen dimensions as I expect.
If there, what are the disadvantages of using different dimens for every size?
Thanks

The best way is to create different layout resources for each of the screens you wish to support. Place each of the layouts in a separate folder that designates the width of the screen. For example, normal sized layouts go in your res/layout folder, and a layout resource for a 7 inch tablet (600 pixel width) would go in the res/layout-sw600dp folder. Make the resource names identical, but adjust your font sizes accordingly.

#up Not, it isn't good way.
#topic You can gets width & height of screen (and w&h of View). Next, you can set font, for example 2% of width screen. If you have content 1260x720, 0.02*1260=24,6 px (you can use also (int)24.6 to convert double to int)

I do something very similar and it's worked fine for me. Some people put the values in the various layouts such as Fietser suggested, but if all of your layouts end up being the exact same except for the font size, your approach is better. That way you can have a single layout and only modify the font sizes. But sometimes you might have changes in the actual layout xml, so then it's probably a wash between the two approaches.

Related

Bigger menus for bigger screens

I am setting my mobile app to work in tablets, one of the many things that need to be tweaked is the fon t size of menus which in an 10" screen are too small. So I'm looking for a global variable that change that font size like you can do with textViews and similar things with
<dimen name="size_font_view_tex">21sp</dimen>
inside dimens-wXXXdp/dimens.xml
Anybody know the name (if it is) of the dimen resource that changes the size of menus?
Bonus: List of all dimen resources
You have to use the different layouts for different size of screen.As you stated the 10" screen you have to make the folder layout-large in your res folder and make the layout according to need. Hope this will help you.
form How to set text size of textview dynamically for different screens
You should use the resource folders such as
values-ldpi
values-mdpi
values-hdpi
And write the text size in 'dimensions.xml' file for each range.
And in the java code you can set the text size with
textView.setTextSize(getResources().getDimension(R.dimen.textsize));
Sample dimensions.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textsize">15sp</dimen>
</resources>
When you create a new layout with right click, you can manage this creation with a "available qualifers".
Here for size, keep the qualifer size and set the screen size that you want.
For example you can set x-large to create a tablet layout for the same screen that you already created before.
more explain : https://developer.android.com/guide/practices/screens_support.html

Supporting multiple screen in Android without using multiple layout directory

I've been developing apps for months and I've read that using layout-small, layout-large, layout-xlarge and so on to support multiple screen resolutions is deprecated started in Android 3.0(correct me if I'm wrong). Hence, I'm still using multiple layout directories but it's a lot of work when designing an Application.
My questions are:
Is there any way to design a layout to support multiple screens all at once without using multiple layout-size.xml files?
What is the alternative solution when using layout-size directory is deprecated?
You can use dimensions. Basically, you'll have the same thing, with multiple directories, but for the values directory, thus a values, values-sw600dp, values-sw600dp-land etc.
In these, you can have a dimens.xml, with the following content:
<!-- In values/dimens.xml -->
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="my_specific_dimension">150dp</dimen>
//More items...
</resources>
<!-- In values/dimens.xml -->
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="my_specific_dimension">150dp</dimen>
//More items...
</resources>
You can now use #dimen/my_specific_dimension in a single layout file, for example for a width or a height, and it will use, just like for the multiple layouts situation, the correct dimension based on the screen category.
That works only if the layout is basically the same, with different sizes. There's little customisation with that solution. If you want to use different layout, you still have to write multiple layout files. I believe the deprecation of x-large, etc. was after they introduce layout-sw600dp, layout-land, layout-sw600dp-land. Check the documentation for how to declare different layout for different sizes. It explains how to use the sw<N>dp qualifiers.
Unfortunately, there is not a standard layout that supports multiple screens.
For instance Normal layout can display with several inches, and accordingly may be among them some larger height or width. However, it should define be unique layout for each screen.

Font size of text

I have created different layouts (layout, layout-small, layout-normal, layout-large, layout-xlarge) and for values (values, values-ldpi, values-mdpi, values-nodpi, values-hdpi, values-xhdpi). I have one activity in my app to show text. I have scroll on text. And I set the values of text size in values-mdpi. But when I run my app on emulator 3.2"QVGA(ADP2)(320 x 480:mdpi) scroll on text work. But when I run my app on emulator 5.1"WVGA(480 x 800:mdpi) all text on half screen and the size of text is small. I think android picking layout depending upon size and text size depending upon values-mdpi. I want size of text big on large screen although they belong mdpi.
My whole app is in portrait mode. And also same case with ldpi and hdpi. Please provide general solution.
Create folder like values-mdpi , values-w360dp-mdpi in res folder.
create dimens.xml on both folder.
and paste it below code in values-mdpi dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textsize">14sp</dimen>
</resources>
and paste it below code in values-w360dp-mdpi dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textsize">18sp</dimen>
</resources>
then apply text size on your activity
android:textSize="#dimen/textsize"
you are using only portrait version, if you are using same structure of design for all devices you don't need to add four different layout for each pixel density.
create android values folders that supports different screen sizes.
Here first folder for 7inch android devices that have ANDROID version 3.1 or less. second one is for 7inch devices that have android version greater than 3.1.
naming convention sw600 means smallest width of 600. so this folder works for width size 600 to 720. So create values-sw480dp and values-sw320 add values to dimens.xml file.
Android runtime automatically picks values from appropriate folder.
Just create layout folders like layout, layout-small, layout-large, layout-xlarge then create values folders like values, values-ldpi, values-mdpi, values-hdpi, values-xhdpi. Then you can create values folder depending upon height or width and belongs to ldpi or mdpi i.e values-w360dp-mdpi or values-h600dp-mdpi. Then android automatically picks layout depending upon screen size and values on depending height or width.
Try to add this in your AndroidManifest.xml
<supports-screens android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true"/>
Just instead yourvalues (values, values-ldpi, values-mdpi, values-nodpi, values-hdpi, values-xhdpi),
which are specifying the text font sizes for each density.
Use text size specification based on screen size, like this:values (values, values-small, values-normal, values-large, values-xlarge)
Alternatively to obtain better results for various devices you can combine density with screen size, like this:values-normal-mdpi
That's all
Try like this
STEP : 1
first in res folder => values => dimensions.xml=> create dimensions.xml file to values folder in that create like below code depending on textsize
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textsize">8sp</dimen>
<dimen name="text">10sp</dimen>
<dimen name="comments">7sp</dimen>
</resources>
STEP : 2
in java file write this line
TextView textviewtwo = (TextView)findViewById(R.id.sponsertwo_txt);
textviewtwo.setText("brought to you by");
textviewtwo.setTextSize(TypedValue.COMPLEX_UNIT_PX,
getResources().getDimension(R.dimen.textsize));
// in place of textsize use text and comments what ever we want depending on size. use like text size adjust automatically in all devices
Actually you don't need to create all these layout directories to handle text size. Just stick with the normal one (layout) and create different values directories with different text size for each resolution you want to support. so you have to add text size in other values directories.
At run-time Android will pick the correct text size.
Use this code, it works based on screen size:
Display display = getWindowManager().getDefaultDisplay();
int displayWidth = display.getWidth();
yourTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX,(float)(displayWidth * 6/100));

How to use different font sizes on 480x800 and 1280x760 screen?

I have read Different font sizes for different screen sizes, but I found that is not working well for me.
I created a values-hdpi to define styles, but I found both 480x800 and 1280x760 use it -- the screen size is so different!
Is there any way to let 480x800 screen use one font size and 1280x760 use another one?
Yes.
Create screen-specific dimensions files, like this,
values-sw720dp/dimens.xml
values-sw600dp/dimens.xml
values/dimens.xml
The first is for 10" tablets, the second is for 7" tablet, and the last is for phones. If that doesn't give you the gradations you want, read this page to find out other ways of targeting different screens.
In those files, define a dimension,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen
name="my_size">40sp</dimen>
</resources>
adjust the dimension value in each of the dimens.xml files to your liking.
Now in your layout,
<TextView ...
android:textSize="#dimen/my_size"/>

When should the dimens.xml file be used in Android?

For instance, in a specific layout I have the following XML:
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="3dp"
android:columnWidth="48dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="spacingWidth" />
This grid view is specific to this layout and I don't think I'll be using any other grid views with similar properties. That to say that the dimension values in the code are specific to that grid view.
Should I still move them to a dimens.xml file or it's fine to just leave them like that? If so, should I place values in the dimens.xml file only when that value is used across multiple layouts?
I drop dimension values into a dimens.xml resource typically for three reasons:
Reuse: I need multiple widgets or layouts to use the same value and I only want to change it once when updating or tweaking across the application.
Density Difference: If I need the dimension to be slightly smaller or larger from ldpi -> hdpi or small -> large.
Reading in from code: When I'm instantiating a view in the code and want to apply some static dimensions, putting them in dimens.xml as dp (or dip) allowing me to get a scaled value in Java code with Resources.getDimensionPixelSize().
Supplemental answer
#Devunwired lists 3 reasons to use dimens.xml. Here are the details of how to do that.
1. Reuse
If you set some dp or sp value in dimens.xml once like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textview_padding">16dp</dimen>
<dimen name="large_text_size">30sp</dimen>
</resources>
you can reuse it throughout your app in multiple locations.
<TextView
android:padding="#dimen/textview_padding"
android:textSize="#dimen/large_text_size"
... />
<TextView
android:padding="#dimen/textview_padding"
android:textSize="#dimen/large_text_size"
... />
Then when you need to make a change, you only need to do it in one place.
Notes
This is basically the same effect as using a style or theme.
Be careful not to give two different views the same dimen value if they really shouldn't be. If you need to make changes to one set of views but not another, then you will have to go back to each one individually, which defeats the purpose.
2. Size Difference
#Devunwired called this Density difference, but if you are using dp (density independent pixels), this already takes care are the density difference problem for all but the most minor cases. So in my opinion, screen size is a more important factor for using dimens.xml.
An 8dp padding might look great on a phone, but when the app is run on a tablet, it looks too narrow. You can solve this problem by making two (or more) different versions of dimens.xml.
Right click your res folder and choose New > Value resource file. Then write in dimens and choose Smallest Screen Width. Write in 600 for the width (7” tablet). (There are other ways of choosing the sizes. See the documentation and this answer for more.)
This will make another values folder that will be used for devices whose smallest screen width is 600dp. In the Android view the two dimens.xml files look like this.
Now you can modify them independently.
values/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="my_default_padding">16dp</dimen>
</resources>
values-sw600dp/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="my_default_padding">64dp</dimen>
</resources>
When using your dimen you only have to set it with the name you used in both dimens.xml files.
<LinearLayout
...
android:padding="#dimen/my_default_padding">
</LinearLayout>
The system will automatically choose the right value for you depending on the device the user is using.
3. Reading in from code
Sometimes it is a pain scaling programmatically between px and dp (see this answer for how).
If you have a fixed dp value already defined in dimens.xml like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="my_dp_value">16dp</dimen>
</resources>
Then you can easily get it with
int sizeInPixels = getResources().getDimensionPixelSize(R.dimen.my_dp_value);
and it will already be converted to pixels for whatever density device the user has.
The dimens.xml file is used to keep all the hard-coded pixel values in one place.
Now, although you may not repeatedly use these values right now, it's still a good idea to to place them in dimens.xml for future reference. Besides, following a standard Android programming paradigm helps other developers to understand your code faster. This is much like the strings.xml where we place Strings some of which end up being used only once! :)
I don’t know if it can help you but I wrote a little java programe that allows you to duplicate a dimension xml file with a new desired value so that you no longer have to do it by hand line by line.
https://github.com/Drex-xdev/Dimensions-Scalable-Android

Categories

Resources