Size of textview in different screens Android - android

I'm getting sick to find the best away to put the same scale for textView's in different screens density and resolution.
I tried:
Create different folders, for different density and put "dimensions". Example, folder values-xhdpi and dimensions.xml, values-ldpi and dimensions.xml too. On my code, programmatically i get textView.setTextSize(getResources().getDimensions(R.id.myDimension);
In xml, 20sp
like example above, but put this textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensions(R.id.myDimension);
Add textAppearence , like textView.setTextAppearance(this,android.R.style.TextAppearance_Large);
But all examples, i got what i wasnt expected, because got texts biggers in biggers density. like in image below
this happen except if i change my dimension in appropriate xml. But, what structure should i follow?
For an example, in drawbles, people reccomend to use scale like 3:4:6:8, but here i didnt follow a structre.
What are the best pratices ? What people normally do..?
What i want? this..

You have to use a pixel independent unit for your text size. Make dimensions with sp unit and load them programmatically.
For supporting multiple screens take a look in android documentation
For the sp unit says :
Similarly, you should prefer the sp (scale-independent pixel) to define text sizes.
The sp scale factor depends on a user setting and the system scales the size
the same as it does for dp.
In other words you have two
You have to declare in values file dimension elements. If you don't like the result and you want to declare for each screen category specific text size you have to do the following :
You can create a values folder for each dpi. For instance , the folder name can be : values-hdpi , values-xhdpi etc.
Inside the values file you can place a dimens.xml file in which you will declare as
<resources>
<dimen name="small_font_size"=10sp></dimen>
<dimen name="large_font_size"=20sp></dimen>
</resources>

It's up to you. You can create different dimensions files for different screen sizes. For example:
values-sw720dp 10.1” tablet 1280x800 mdpi and bigger
values-sw600dp 7.0” tablet 1024x600 mdpi and bigger
values-sw480dp 5.4” 480x854 mdpi and bigger
And take the value in sp for your TextView from your dimensions files.

Related

Android studio declaring Text size and Button sizes for different screen sizes

I made my first Android App. I read a lot about the images and made separate sizes for mdpi hdpi xhdpi and xxhpi.
Although i noticed i'm still having some issues on smaller screens 3.7" and 4.0" inches. What i noticed is that i'm using fixed values for things like buttons TextViews and Text sizes.
I came across and read about the dimens files but i'm still a little confused as to how should the folder structure should of the values.
To be more specific
drawable
drawable-hdpi
drawable-mdpi
drawable-xhdpi
drawable-xxhdpi
How is the values folder structure should be ?
I've read somewhere that it should look like values-sw600dp.
But what does the 600dp represents ?
600dpi and up ??
Can i use custom values there or are there some fixed ones??
Thank you
after version 3.2 sw600dp is used for multi-layout panes for devices whose dp is greater than 600, where dp is :
px = dp * (dpi / 160)
This is irrelevant to your requirement (unless you want to make apps compatible for tablets).
Just make sure you put proportional image sizes in xhdpi,xxhdpi,mdpi and ldpi folders and use wrap_content for images. You can also use constant sizes, just make sure that you use dp for buttons and sp for text size.

Android : the mystical technique to set different dimensions for different device / screen sizes

My first Android App. And, am trying to get this sorted -> setting different text sizes, layout sizes for different screen / device sizes. Now, I Googl'ed a lot , then read several Stackoverflow threads, & am doing this:
Created different folders under 'res' like
values-ldpi
values-mdpi
values-hdpi
values-xhdpi
values-xxhdpi
values-xxxhdpi
Then I put a dimens.xml & styles.xml in each folder above.
Phew! But this doesn't suffice. I want to have different textsizes (and other dimensions, am not speaking about images here) for phones & different ones for tablets. I know there are these 'values-sw600dp, and 'values-sw720dp' which they say is aimed at tablets. But even within each tablet size, I need to differentiate the different densities right? For example, within 'values-sw600dp', I need to have different textsizes for, say, hdpi, xhdpi, ... How do I do this? Any help please?
Here is an example of what am doing:
TextView textView = (TextView) findViewById(R.id.text_interference);
textView.setHeight(layoutHt);
textView.setWidth((layoutWd-20)/2);
int dimensControlButtonTextSize = getResources().getInteger(R.integer.CONTROL_BUTTON_TEXT_SIZE);
textView.setTextSize(dimensControlButtonTextSize);
Then in dimens.xml, it would be:
17
This value, i.e. 17 here, will differ in each device i.e. it is given a different value in each dimens.xml under each values- folder. I hope am clear.
Drawable resources do not need to be duplicated per screen configuration, except for special cases like you have a full screen splash that would be displayed differently for landscape and portrait. Normally for icons, specifying once in the main resources folder (under hdpi, mdpi, xhdpi, xxhdpi...) should be sufficient.
I would find screen configuration specific resources to be useful for dimen values, like padding, margin, width of items, etc...

Different font size for diffrent screen densities

I am having a trouble with text size
I am using the sp unit
hdpi and mdpi the font looks great and readable
on x & xxhdpi the font is too small
I tried to put a larger font for the later 2 in values-large , values-xhdpi , values-sw320dp
what happens is either the new sizes is applied to all screens or to none but never to the screens I want
if anyone knows where to put the dimens for this two screen size I'd appreciate it or even another way to handle this font behavior
and thanks in advance
You can create different layouts and put them into different folder.
layout-small
layout-normal
layout-large
layout-xlarge
In that layouts you can put different text sizes according to the screen size.
you can use dp instead of sp .. its the density pixel.. it will keep the ratio same for all the screen..
Or you can add this line to ur textview
android:textAppearance="?android:attr/textAppearanceLarge"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textAppearance="?android:attr/textAppearanceSmall"
It will automatically adjust the text size according to the screen size.. depending on the line u added..

Galaxy S4 taking values from MDPI?

I want to use separate values for each density, so I created different values folders for each one,
values
values-ldpi
values-mdpi
values-hdpi
values-xhdpi
values-xxhdpi
in each of the strings.xml file I wrote the density (for values-xxhdpi i have a string with the text "xxhdpi"), but when I run this on GS4, I get the "mdpi" string,
I also did create separate layout folders, but always taking layout-mdpi density
any clue of what might be going on ?
It looks a bit funny to me to define layouts according to density and not size. Drawable and other similar resources depend on density, but layouts depend on size.
See here : http://developer.android.com/guide/practices/screens_support.html
For values, you could give it a try with values-normal, values-large, etc.
I write this although what you did is supposed to work according to this thread : https://stackoverflow.com/a/9639280/693752
Also, this resource is interesting to you :
http://developer.android.com/guide/topics/resources/providing-resources.html#BestMatch
---- UPDATE ----
Look at this answer from GodFather : https://stackoverflow.com/a/20521720/693752
(Note that density qualifiers are even fuzzier -- -mdpi will be used
for any device density, in the absence of a better match, as density
qualifiers are designed to be used only on drawable directories, where
Android can apply resampling algorithms. Using density qualifiers on
anything other than drawables and maybe dimension resources is a code
smell.)

What should be the text size ratio in android for multiple density device?

I am making an Android application for multiple screen device.
I want to know what should be the text size scale ratio for different density device?
example : if my text size is 28sp on mdpi device then what should be on hdpi device?
I made values-ldpi, values-mdpi, values-hdpi etc. and define text size in dimens of each respective values folder, but it did not look similar on all device.
You dont need to know the ratios, and you shouldnt directly use them since the amount of densities keep changin.
If you use it in the resources, using the sp unit will do it work fine in every density, without diferent value folders.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="type1">28sp</dimen>
</resources>
If you do it from code, probably it will look diferent depending on howyou asign the values. You have to send the correct values to the setTextSizeFunction, for example:
setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.type1));
//he documentation method that COMPLEX_UNIT_PX returns a Resource dimension value multiplied by the appropriate metric.
or with hardcoded numbers:
setTextSize(TypedValue.COMPLEX_UNIT_SP, 9);
Use below formula for declaration in dimens.xml
base mdpi= 1X
HDPI= 1.5X
XHDPI=2.0X
XXHDP=2.75X
above formula for relation b/W all Resolution

Categories

Resources