Different font size for diffrent screen densities - android

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..

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.

Size of textview in different screens 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.

How to have different layouts for same size category devices?

I have 3 devices
1) galaxy nexus 720*1280 (xhdpi, normal size category )
2) motorola 480*854 (hdpi, normal size category)
3) htc 320*480 (mdpi, normal size category)
I have my xmls in my layout folder. In my xmls I need to fix sizes to my textviews,Edittext(it will expand with the text entering).
Because of this,I am getting problems in my motorolo,htc devices.I cannot able to prepare different layouts because these 3 devices are comes under layout-normal(layout).
I have Textviews in Rectangular background,with the text increases rect box is getting expanded.
Is there any option to prepare different layouts for each of the devices.
please suggest
Thanks
You can target densities by using a layout folder like layout-mdpi, layout-hdpi and a layout-xhdpi. Why would you want to do this? using dips for everything will be fine.
Edit: suggestions based on the comments you gave:
by using a value like 200dp, the boxes on all devices kinda have the same size. Not in pixels but in actual size on the screen. This is what dips (the dp unit) is made for.
The screens are supposed to look kinda the same on the different device sizes. Read the description on http://developer.android.com/guide/practices/screens_support.html especially the section named "Density independence".
After reading that and if you still really, and I strongly suggest you don't do that, want them to look different on different screen sizes do this.
In your layout xml define the width as android:layout_width="#dimen/the_width"
In the folder values, create a file values.xml. In there define a dimension like this <dimen name="the_width">200dp</dimen>. 200dp is an example and will be the default value.
Override this value in the values-hdpi, values-xhdpi and/or values-mdpi folder, by creating the same values.xml, but with a different value for the_width.
This will make the edittext have a different size on different screen sizes.

Android button and the size of text

I have a layout with a few buttons that takes the space of the entire layout using weight properties.
Each button has some text on it with a fixed size which is set by android:textSize="30dp" so on a screen size of a small mobile device (A phone) the text's size if about the size of the button. But on a large device (A tablet) the button size expands yet the text size remains the same (30dp).
I want the button size and text size ratio to be maintained in every resolution, can it be done using the layout XML code? I would like to avoid doing it programmatically.
The solution is very simple
You should use a dimension.xml with diferent sizes each density/size and place it on:
values-mdpi
Values-hdpi
values-xhdpi
values-large (5.1' 5.4' y nexus7 tablets/phones )
values-xlarge (bigger tablets)
You can use values-sw600dp (nexus 7) and values-sw720dp(10') too.
Ah! And use sp for text, not dp. SP put the same size for all the fonts. Text Allways with SP
Remember, you allways can create values/drawables/resources in general for a lot of separate configurations: Landscape portrati, langauge, sizes, densities, night or day, version of android .... And you can merge it!
http://developer.android.com/intl/es/guide/topics/resources/providing-resources.html#SmallestScreenWidthQualifier
text size should be mentioned in sp, not in dp try like this
android:textSize="30sp"
Size of the text. Recommended dimension type for text is "sp" for scaled-pixels.
make different xml layout file for tabs and phone and try this for button
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Large"
you can fix the size of buttons by giving weight and for the text of a button you can do this to auto size them:
android:autoSizeTextType="uniform"
android:autoSizeMaxTextSize="24sp"
android:autoSizeMinTextSize="20sp"
Set the min and max size what you need and put these codes inside a button body:)

Resolutions and densities

I am developing for android and my code runs perfect on nexus or any 3.7 inch device.
If I run it on tablet (7 or 10.1 inch) then the fonts and buttons are very small and the spacing is way too big.
If I run it on 2.7 QVGA then the buttons are too big and overlap.
I am using dp and sp all over the layout so I am surprised it didn't adjust according to different screen size/density.
Does that mean that I have to wrte the same XML layout in 3 layout folders (large, medium and small) and put my drawable in 3 different density folders?
Or am I amissing something here. Yes I read the "Supporting multiple screen" document and it just loses me with the details (but I did follow the tips of using dp and sp). I am looking for more of a summary/general approach answer.
dp and sp adjusts according to screen density. Therefore, a screen with 500 dpi and 4000 dpi but with the same SCREEN DIMENTIONS will looks the same.
If they have different screen sizes, it won't fit as you want. If you want to support multiple screen sizes, you'd have to use weights or percentages.
The key is that dp = device independent pixel and it only adjusts according to density, not screen size.
In the most cases if you don't make a tablet app, you don't need to make more than one XML layout for different densities/sizes. All you must do is define dimensions with DP and SP as you said.
But is necessary to provide different images in the drawable folder to show one or other in function on screen size/density.
If you put all images in drawable folder (without specify hdi,ldi or mdi) Android will try to adjust the images but not always works fine.
Hope it helps.

Categories

Resources