Dynamic Text size change according to tablet

37,223

Solution 1

I was Having Same Problem but what i realized sp/db are not efficient for dealing with this kind of problem, I handled my alignment and font size related stuff programtically.

here are the steps, how i handled this problem.

  • Calculate Screen Width and Height of Your device
  • Use Set TextView.setTextSize(unit, size), where the unit parameter is TypedValue.COMPLEX_UNIT_PX and the size parameter is percent of the total width of your screen. Normally, for me, .5% of total width for font is suitable. You can experiment by trying other total width percentages, such as 0.4% or 0.3%.

This way your font size will be always suitable for each and every text/screen size.

Solution 2

After checking some resources I finally came up with the following solution: Within the layout-xml I define the size of the button-text with a dimension declaration:

android:textSize="@dimen/campaign_textfontsize"

I created a dimens.xml in the "/values"-subdirectory with the corresponding value

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="campaign_textfontsize">18dp</dimen>
</resources>

then I created a second values-folder "/values-sw720dp" and copied the old dimens.xml into it. In this dimens.xml I adapted the fontsize value:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="campaign_textfontsize">24dp</dimen>
</resources>

Based on the current screensize android selects the proper dimens-value. So without any code, font is adapted to the display- (and display related button-) size.

Hope this helps...

Solution 3

no need to change textsize dynamically use sp for textsize it will automatically arrange the text size depending on the screen resolutions..like in phone.

android:textSize="10sp"
Share:
37,223
Android
Author by

Android

I like programming, specially where it need more of logic and then actually implementing the thoughts. Reading books and playing video games are other important part of my life. Otherwise my life is simple and so do I.

Updated on December 08, 2020

Comments

  • Android
    Android over 3 years

    With all effort,I finally reached to the end of my first app in android. And thanks to all. But after coming to end, I realized one thing that my app text size is common in all tablet sizes. So I will re frame my question.

    Problem: I used my own custom text size in entire app. and some what satisfied with 7 inch tablet. But when I am looking the same thing in 8.9 inches and 10.1 inch tablet its containing lots of white spaces and text size are also relatively small. So is there some way that I can change the text size according to my tablet size??? It may look novice question but I am struggling with this because the same app which look wonderful in 7 inches, is loosing its essence in 8.9 and 10.1 inches. Or there is something else which I could do with my app for the same. Probable Solution:- As my topic indicates is there some way to change the text size as tablet size changes. Either dynamically or any other way.

    EDITED:: As per Cheeta answer, approach is correct but I can't do it for each layout buttons and textfields and other field. There are hundreds of field like this in single app. Can I do it in some one place and call it required attribute. Is custom tag is approach which I am looking for.Is it possible????

    Any help will be appreciated. Thanks in advance.

    NOTE I have not reached to a answer but cheetah answer is somewhat leading to correct way. I am marking his answer as correct although there are few alignment issue with his answer. Any other answer is always welcome. Thanks

  • Android
    Android about 12 years
    thanks for replying. But what I am doing is dividing the screen into two portion with one side as menu and other is fragment part which keeps changing as menu choice. Will this work here also.???
  • Yahya Arshad
    Yahya Arshad about 12 years
    Yes as i mentioned you have to give your screen size in percent .05 of total screen width or what ever.. which will be suitable for each and every screen but you have to play little first assigning percentage
  • Android
    Android about 12 years
    I have not got what I was looking for, still just to inquire, is there some way that I declare according to your method at one place and is reflected in entire app.????
  • Yahya Arshad
    Yahya Arshad about 12 years
    if from entire app you mean all the textview have same size if passed to one TextView Object, then no you have to do same with each and every Object of your TextView
  • Android
    Android about 12 years
    there are more then three hundred text view, button with text, edit text. Setting the property is quite a cumbersome task and there are chances that lot more get added. Do you think this the right approach ?????
  • Yahya Arshad
    Yahya Arshad about 12 years
    okz there is one more solution for this but first of all tell me one thing does your All TextView will have same size if yes then you can extend Android TextView and use your customized TextView. other wise there is no solution you have to assign size to each TextView explicitly
  • Android
    Android over 11 years
    Hey Kai. Thanks a lot for the answer. Although its been a long time I used that project but a definitely I will give this a try. Thanks again for taking pain for answering such an old question. - Devil
  • Steve
    Steve over 11 years
    If you have a lot of controls then abstract this solution to ease implementation. Place a function to do this in a globally available scope/script, have the function able to accept object references and/or arrays of object references, and set the function parameters to allow whatever information you need - such as scale %. Then you call the function in the code that initializes the UI. You could even setup a loop that looks at all elements and adjusts them based on whatever properties you need.
  • user3316561
    user3316561 over 8 years
    sp will only arrange the text according to screen resolution, not the size. An 8'' tablet might have a lower resolution than a 5.7'' phone. So the text will end up looking smaller in tablets.
  • Avi Levin
    Avi Levin over 8 years
    guys, Raja is right! According to google, "Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference".
  • tccpg288
    tccpg288 about 8 years
    Do you have to enter the % in the parameter? I am entering a simple whole number, like 1.
  • tccpg288
    tccpg288 about 8 years
    Why would you use dp and not sp when dealing with a font size?