I have been trying to set the textsize of the text of a button in my code but the problem is that the text appears normal on my Galaxy tab s2 (I am using this device for testing) but when I open the app on my xperia MP4 dual, the text gets cramped and the text (text is "CHOSEN") splits into two lines (CHOS in one, and EN in the other). I have added the supports screens block in my manifest file but it doesn't work. Moreover, I read something about creating different dimens.xml folders for each screen size (Link: Text size and different android screen sizes), but I am confused as to how to use them in my code so that the code dynamically adjusts the textsize.
Asked
Active
Viewed 46 times
1 Answers
0
create values folder for different screens
res/values/dimens.xml(default)
res/values-ldpi/dimens.xml (240x320 and nearer resolution)
res/values-mdpi/dimens.xml (320x480 and nearer resolution)
res/values-hdpi/dimens.xml (480x800, 540x960 and nearer resolution)
res/values-xhdpi/dimens.xml (720x1280 - Samsung S3, Micromax Canvas HD, etc)
res/values-xxhdpi/dimens.xml (1080x1920 - Samsung S4, HTC one, etc)
res/values-large/dimens.xml (480x800)
res/values-large-mdpi/dimens.xml (600x1024)
res/values-sw600dp/dimens.xml (600x1024)
res/values-sw720dp/dimens.xml (800x1280)
res/values-xlarge-xhdpi/dimens.xml (2560x1600 - Nexus 10")
res/values-large-xhdpi/dimens.xml (1200x1920 - Nexus 7"(latest))
Or you can do that by putting
View.setTextSize(size in px);
In onCreate method you can also set text size as a function of display size by
Display d=getWindowManager().getDefaultDisplay();
int w=d.getWidth();
int h=d.getHeight();
View.setTextSize(Math.sqrt(w*w+h*h)/(edit here));

Manesidlapy
- 39
- 1
- 7
-
Thanks for the answer , but could you please explain the last line of code??(Math.sqrt(w*w+h*h)/(edit here)) – Snehil Aug 23 '16 at 16:51
-
I am calculating the length of diagonal of screen and then setting the textsize according to that – Manesidlapy Aug 23 '16 at 16:53
-
-
Is the View referring to the class 'View' or the buttonview? And whats 'edit here'? – Snehil Aug 23 '16 at 16:57
-
You can use any View(Button,EditText,TextView) there and you have to put a number in place of edit here like if you find (diagonal length/50) as the best fit then put 50 at the place of edit here – Manesidlapy Aug 23 '16 at 17:03
-
So this display class will get me the length of the diagonal of any view i want ,like text button etc.? – Snehil Aug 23 '16 at 17:06
-
Display class is not returning the length of view it is returning the length of display like if your screen resolution is 1600×2560 then h will be 1600 in landscape mode and 2560 in portrait mode – Manesidlapy Aug 23 '16 at 17:17
-
If you found my answer helpful can you please up vote my answer as I'm blocked for asking questions because of too many negative votes on my silly questions – Manesidlapy Aug 23 '16 at 17:21
-
-
I did do that but it won't publicly be shown since i am new to stackoverflow. And could u do the same fr my question since i have had a similar experience XP xD – Snehil Aug 23 '16 at 17:24
-