How to make font size bigger in tablet PC? - android

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

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"

Android - Dynamic text scaling for same dpi but different screen size

In my app I create multiple TextViews dynamically and the text size for each one I derive from the value stored in dimens.xml under values folder. On one hand I have the Nexus 4 and on the other hand I have Nexus 10. Both have the same "xhdpi" density bucket but one has the screen size of 4.7 inches and other has a screen size of 10.05 inches. I can have the same value for textsize due to their same density bucket but the text appearing fine on Nexus 4 appears too small on Nexus 10. How can I have my text appear appropriately large on such devices ? Let me know if any files need to be included.
In your dimens.xml file use sp instead of dp.
Before
<dimen name="text_size">80dp</dimen>
After
<dimen name="text_size">80sp</dimen>
Create values-sw600dp and values-sw720dp and check in which one your tablet falls in. Set the dimension larger for the same key name in values-sw600dp and values-sw720dp based on your requirement.
After doing a lot more research I found out you can use both size and resolution specifiers together. I simply used values-xlarge-mdpi for Nexus 10 and values-large-mdpi for Nexus 4.

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

Issues with font sizes on different dpis

I specify all font sizes in sp. The text looks good on several devices including Nexus 7, Galaxy S4 mini etc. However, on Galaxy S 3, the font is too large. Is this because the DPI of the S3 is so much more? If so, how do I adjust for this so that fonts display at relatively similar sizes?
DP/SP seems to be working properly. The larger font sizes I got was because of setting text sizes programmatically, using resource values which were already returned scaled - so essentially fonts were being double scaled. This could have been prevented by proper documentation by Android.
On my project i use different "dimens.xml" file for every type of density folder, for example:
values-mdpi
values-hdpi
values-xhdpi
dimens.xml has:
<!-- Text dimension for activity album preview -->
<dimen name="text_album_title">20sp</dimen>
<dimen name="text_album_author">18sp</dimen>
<dimen name="text_album_info">13sp</dimen>
<!-- Text dimension for row -->
<dimen name="text_track_title">16sp</dimen>
<dimen name="text_track_length">14sp</dimen>
And this is different for every file in folder. Is up to you to decide the size, you will see the difference between each dimension by using a layout with a TextView. Give it a try.
EDIT
I was totally wrong on this subject, thanks to Runloop and 323go.

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