Text size doesn't keep aspect ratio in android studio - android

phone image
tablet image
I am using this line of code in android studio
btn_play_game1.setTextSize(16 * getResources().getDisplayMetrics().density );
to set the text size to a button (that has an image on it). The text is "Clear The Way" but on phone emulator looks very big and smaller on tablet.
Why the text is not as adapt to the screen size and keep the aspect ratio?
Thank you!

I think instead of multiplying your font with display density you should use the resource declared in dimens.xml file like getResources().getDimension(R.dimen.text_size_medium) this way it would not only take care of the device density but also take into account the font size set by the device user.

Related

Same text size but it larger on other devices

I have a TextView in which I programmatically setup the text size in 'SP'.
tv.setTextSize(spToPx(5, context));
public static int spToPx(float sp, Context context) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.getResources().getDisplayMetrics());
}
First screenshot shows how it should look (and how it looks on my Xiaomi Redmi and Doogee HT7
Second screenshot shows how it looks on other devices with higher or the same dpi.
Normally, if I understand correct, text with the same size in 'dp' or 'sp' should looks the same on other devices (with the same screen size) because it automatically converts 'dp' or 'sp' to the needed amount of pixels depending on dpi.
After reading Android Developer Guideline about devices with different density I was expecting that text would be smaller if I use the same text size on devices with higher dpi. But now I have a situation that I really don't understand.
If you are using values in sp and the user changes the default text size for his device from Settings--->Display---->TextSize, your TextViews will be affected. If on the other hand you use dp values, then no matter what text size the user sets as default, the TextViews in your app will remain the same. This is ofcourse discouraged by Google as bad practice, since the user should be able to change text sizes but it would be what you are looking for.
So, use (TypedValue.COMPLEX_UNIT_DIP,number of your choosing) instead of (TypedValue.COMPLEX_UNIT_SP, sp)
You can add dimens file to your resources . thats how I do it
heres the link where I got the answer
Try this, Add this property in your xml. It will change textsize based on all phone.
style="#android:style/TextAppearance.DeviceDefault.Medium" /*for normal text */
style="#android:style/TextAppearance.DeviceDefault.Small" /*for small text */
style="#android:style/TextAppearance.DeviceDefault.Large" /*for large text */
Hope your problem will solve.
The primary difference between an sp and a dp is that sp’s preserve a
user's font settings. Users who have larger text settings for
accessibility will see font sizes match their text size preferences.
so you could check the settings of two devices and see if there is a difference between them.
I still didn't find right explanation to this issue. The only theory I have is next:
There are density buckets (120 dpi, 160 dpi, 240 dpi, 320 dpi, 480 dpi, 640 dpi). Screen density of your device will be determinated as the closest bucket. For example, if your device has 401 dpi it will be counted as 480 dpi. So, when I create and add view with size of 20 dp it will convert into more pixels than needed. It the hardest for devices that have density right between two buckets. I also checked density of all devices that had wrong image display (like on image 2) and it was almost right between two buckets...
I still don't know why this happens only when I create and add view dynamycally, but the only way to solve this problem was the creating of separate layout with all presetted views instead of adding these views dynamically.

how to use vector images for different android screen sizes

how to use vector images which scale up and down according to the screen sizes?
I m using a vector image in my project but it is not scaling in big screens.
what you mean by being scaled ? maybe you are misunderstanding.
if you mean the image size would be changed in various screen sizes such thing wont happen. the image size is what you provide in your imageview (or any otherview using the image).
the only different between vector assets and normal asset is that vector asset are redrawn every time so you wont have a low quality image no matter how big it is and which device it is being shown in. so you wont need to save various size of image for various size of screens.
if you want to change the size of image just change the imageView height and width. you can use dp (density independant) unit.
you dont have to do anything for support vector images for diffrent screens its already scale for the relevant dp, Can you give more details about your problem when you say "it is not scaling in big screens?"
maybe you want the image on tablets will be bigger?

android system is taking layout xml files only from layout-320

I am developing app.I have only layout folder with xml files,it is showing properly in more pixel screen ,but when i see my app in less pixel screen the text is big not same as more pixel screen As per android developer site, I came across layout-sw320dp ,layout-sw480dp. So I have added the xml files into layout-sw320dp,layout-480dp and made changes like text size and margin,then when i run them with sw-320dp in less pixel screen i can see text size same as more pixel screen,but when i run with sw-320dp the same in more pixel screen, the text is small ,it is taking layout from only sw320 actually it should take from layout folder.I d'not know where I am wrong can any one tell me.
screens with only layout folder
[![screen in less pixel density[![screen with more pixel][1]][1]][2]
screens with layout-sw320sp
[![screen in less pixel [![screen with more pixel density][3]][3]density][4]][4]
[1]: http://i.stack.imgur.com/6rTNw.png
[2]: http://i.stack.imgur.com/uHRkx.png
[3]: http://i.stack.imgur.com/qhcmk.png
[4]: http://i.stack.imgur.com/Z5PE0.png
You are not supposed to pick what resource size is shown, android does that depending on the device used at runtime. Your job is to set the resource in the correct folder for example res/layout/mylayout.xml and res/layout-sw480dp/mylayout.xml res/layout-sw320dp/mylayout.xml sw is smallest width of screen, 320 is a number, dp can be thought of as pixels. This concept of putting resources in folders with special names and allowing android to pick the most sutable special name/folder for the job works for any resource. Strings/languages, images/for device screen size,night/day,orientation-|.

Text Size Ratio Between Mobile and Tablet Android

What is recommended text size ratio between mobile and tablet device?
I am currently working on the application for supporting different screen sizes .I am facing issue with text sizes ,after some reasearch , I found that creating different values folder will solve my problem .
But I didn't know multiplication ratio which I can use to best decide on text size for a Tablet and 7" Device Screen .
if have a text with size of 20SP in normal phone screen
then what is the corresponding text size for tablet
How can I calculate the corresponding value for tablet any formula ?

different devices with different image and font size

I am creating an app for different devices and I added images with different sizes in drawable-small, drawable-medium... to add the same image for different devices but I need to increase the size of the image when the size of the screen increases is this the correct way?
plus I need to increase the font size when the screen size increases how can I do that and if I used the layout-small... folders what font size will be the perfect size for the devices with small screen size (portrait and landscape) and for the medium and large..?where can I find this info as I am trying and trying different font sizes but there must be another way!!
thanks.
This is a very rookie question but here goes:
You set the text and image sized in dp (density pixels) so screen size and resolution is irrelevant.
As for making multiple drawables for each screen size is a good idea but not a necessity since you can choose how much of the screen each picture will take, but on bigger screens make sure the resolution of your images are high enough to look good.
For scaling a particular image to all screen size you can use nine patch image. There will be drawninepatch.bat file in the tools folder of your sdk. Open that , drag and drop the image , and you want to expand the image based on your needs. Now after the nine patch is created put that in the drawable folder. This image will expand accordingto the screen resolution and densities.

Categories

Resources