Font size of text - android

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));

Related

Why is there a layout preview difference between Pixel and Pixel 2 in Android Studio even though they have same screen size and pixel density

I have been trying to understand why these two devices even though having same screen size and pixel density behave differently.
Please Note: The Font size and Display size are set to "Default" on both the devices.
EDIT 1: I have created the following folders in the layout folder.
layout-hdpi
layout-xhdpi
layout-xxhdpi
layout-xxxhdpi
Each folder has the activity's layout with the dimension and image size fixed. My point is, if the two mobiles are using xxhdpi layouts or the Google Pixel is using xxxhdpi and Google Pixel 2 is using xxhdpi?
I think it depends on DPI of your phone. Two phones may have different dpi. You can change dpi if phone is rooted. I dont know if stock supports.
Please Note: The Font size and Display size are set to "Default" on both the devices.
No , You have to using dimens.xml for different devices of android for Example see Below structure :
res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-large/dimens.xml
res/values-xlarge/dimens.xml
dimens.xml file contains
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">18sp</dimen>
</resources>
In Textview add this
android:textSize="#dimen/text_size"

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

Android font size on different screens

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.

How to make font size bigger in tablet PC?

tv.setTextSize(14);
I set TextView font size to 14sp.
When I run my app in Nexus One, font size is ok.
But, when I run it in Nexus 7, font size is so small.
This is because Android scales font size according to density.
Nexus 7 density is lower than Nexus One, Android scaled font size down.
How can I adjust font size proportional to screen size?
Use an XML resource. In an XML resource file (I usually call mine 'dimens.xml'), define a dimension resource called something like font_size. Create a 'dimens.xml' file in the folders 'values-normal' and 'values-large'. The font size in 'values' will be used by default, but a large screen (like the Nexus 7) will use the size in 'values-large'. You could also define it in 'values-xlarge' if you wanted to explicitly support 10" tablets.
Settings the size can be done in XML with textSize="#dimen/font_size" and from code with tv.setTextSize(getResources().getDimension(R.dimen.font_size)).
Your dimens.xml file should look like this:
<resources>
<dimen name="font_size">14sp</dimen>
</resources>
Will be better if you use the dimens.xml approach, and generate different versions for phone and tablet in folder:
res/values/dimens.xml -> For default sizes
res/values-normal/dimens.xml -> For phone sizes
res/values-large/dimens.xml -> For tablet sizes (mind setting bigger values for font-size, may paddings, and so on...
You can set it from your dimes.xml
Define a dimension in the dimens.xml. Eg:
<dimen name="my_text_size">14sp</dimen>
And then use that in your code:
tv.setTextSize(getResources().getDimension(R.dimen.my_text_size));

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"/>

Categories

Resources