How to set font for diffrent devices - android

i am creating simple app to show notifications. i want large font on large devices and small fonts on small devices. Please Help.
Following is screen shot of tablet of dpi - 213
Following Screen shot of Moto G3 phone have dpi - 320

You need to create different values folder as per required screen sizes and resolution and put 'dimens.xml' file in each folder.
For example:
For HDPI device- you need to create folder with this name
"values-sw320dp-hdpi" under this folder put "dimens.xml"
For Large HDPI device- you need to create folder with this name
"values-large-hdpi-1024x600" under this folder put "dimens.xml"
For MDPI device- you need to create folder with this name
"values-sw320dp-mdpi" under this folder put "dimens.xml"
Similarlily you can create different values folders for other devices too under "res" folder.
Now, what next to do and How it works:
Say, you have TextView defined in your xml and you need different font sizes on different devices:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/textSize" // This is the important line
android:text="Hello"/>
Now, here 'dimens.xml' file comes into picture. You need to specify different size value of "textSize" under different dimens.xml w.r.t screen sizes and resolution
For Example:
For HDPI device: res>values-sw320dp-hdpi>dimens.xml
<dimen name="textSize">19sp</dimen> // Setting size of Text for HDPI devices
For MDPI device: res>values-sw320dp-mdpi>dimens.xml
<dimen name="textSize">15sp</dimen> // Setting size of Text for MDPI devices
Similarly, you can define sizes for other screen too.

You should use styles, then you can have separate folders "values" (default) "values-hdpi" (high density) "values-mdpi" (medium density) and so on and put your style file with correct textSize values in each folder as needed.
Then, when you are in medium density device it will pick the file in "values-mdpi" folder if exists or in "values" if not, and the same for high density etc...
This same principle applies to al "res" subfolders

Related

All devices using hdpi xml files

I have two devices of hdpi and xxhdpi screen density. All of my xml files are in the 'layout' folder. Since the hdpi device's screen is small, I copied some xml files to 'layout-hdpi' folder and made the respective changes for the hdpi device. The problem is that the changes I made for the hdpi device are getting reflected even on my xxhdpi device.
What can be the problem or am I doing something wrong?
Since the hdpi device's screen is small, I copied some xml files to 'layout-hdpi' folder
Screen size is not related to screen density. Do not create layout directories based on density. Create layout directories based on screen size (e.g., layout-sw640dp for layout resources to be used on devices whose smallest width is 4" or larger).

Android devices with different height takes same layout folder

I have a Micromax AQ5000 with Screen Resolution- 1280*720 pixels and Motorola Moto G XT1033 with resolution 720 x 1280 pixels.I have a layout folder named layout-sw360dp which I designed for devices like Samsung s4,s3,Micromax canvas etc but this Motorola device is also using the same layout and this creates the images displayed as distorted in it.
How can I create a folder for the small device(Moto g) I tried layout-xhdpi but it doesn't work how can I name layout with height and width.
Well you are right in some sense android should take layout dependent on different densities but some mobile do not fall under specific density. Therefore android will pick up default layout from layout directory.
to support multiple screen resolution provide different layout for different screen sizes, you can make following directories in res directory like this
layout-hdpi
layout-mdpi
layout-xhdpi
layout-xxhdpi
layout-w320dp-h408dp
layout-w480dp-h800dp
layout-w480dp-h854dp
layout-w720dp-h1280dp
layout-w1080dp-h1920dp
when you provide layout in all this directories you will give multiple screen support for different sizes as well
layout-w1440dp-h2560dp
Use "dip" instead they will help you in debugging your layout as they will try to keep a coherent size to multiple screen resolutions,
<ImageView
android:id="#+id/avtar_animation_11"
android:layout_width="45dip"
android:layout_height="45dip"
android:src="#drawable/avtar011"/>
while supporting multiple screen when you give "dp" to dimensions, Actually android expects you to provide different values for different screen resolution. Lets say below is your imagview dimensions make few folders in res folder in your android project like these below
values-hdpi, values-mdpi, values-ldpi, values-xhdpi, values-xxhdpi
and in them make a dimens.xml file each of them and write
<dimen name="image_view_width">28dp</dimen>
<dimen name="image_view_height">28dp</dimen>
now that i have mentioned "dp" here instead of dip android wants me to keep track for different dimensions for different screen resolution, So i will change the image_view_width and image_view_height values are in separate values folders where dimens.xml is located. Make sure your dp values change as per your screen resolution you want your view to fit in.
<ImageView
android:id="#+id/avtar_animation_11"
android:layout_width="#dimen/image_view_width"
android:layout_height="#dimen/image_view_height"
android:src="#drawable/avtar011"/>
hard part is over now android will pick one of dimens.xml values depending on which screen your app is running, Voila now your layout rocks

Custom dimens.xml for mdpi devices - how it can be done?

My application have layout, which looks perfect at all devices, except devices with mdpi screen destiny. Activity just doesn't fit to screen on mdpi devices.
So I want to create special dimens.xml for these type of devices.
I created "values-mdpi" folder next to my "values" folder, created new dimens.xml into it and set dimens values for mdpi devices.
res/values/dimens.xml:
<resources>
<dimen name="logo_block_height">100dp</dimen>
</resources>
res/values-mdpi/dimens.xml:
<resources>
<dimen name="logo_block_height">50dp</dimen>
</resources>
And ImageView that uses that dimen:
<ImageView
android:layout_width="250dp"
android:layout_height="#dimen/logo_block_height"
android:id="#+id/font_logo" android:adjustViewBounds="false" android:src="#drawable/bg_font_logo"
android:layout_marginTop="20dp" android:layout_marginBottom="20dp"/>
The problem is that values now applied for all devices, not only for mdpi, but for hdpi, xhdpi, xxhdpi too.
I want it to work like that:
If device screen destiny is mdpi or lower - use values from values-mdpi folder
If device screen destiny is MORE than mdpi - use default "values" folder.
How it can be done?
So I came up with only one solution for that without creating values folder for each screen density.
When at least one "values" folder with destiny qualifiers created android selects the most suitable folder for device screen destiny.
This is the reason why it selects "values-mdpi" for everything that bigger or smaller than mdpi.
Also, like ataulm said, there are no sense use screen density qualifiers in that situations. I can have phone with 340x480 with 160dpi where content doesn't fit and Tablet with 1280x800 with 160dpi where there is too much free space.
I solved that by creating Values folder with minimal-height qualifiers.
I have folder "values-h480dp" with values for every device with minimum 480 dp availible height and put default values in it. In default "values" folder i put values for devices with small screen.
Now it works like I need:
If device have availible height more than 480dp - it uses values from "values-h480dp" folder.
If device have availible height less than 480dp - it uses values from "values" folder.
I sorry for my bad English and hope that this has helped someone.

android layout on different screen sizes

i have an application that am trying to implement it on a tablet 7'. this that i have done is the follow. on the folder drawable and layout are my defaults for the normal smartphone screen. also i have create the layout-xlarge and drawable-mdpi in which on the first one i have change the sizes and on the second one i add the images with different size. my problem is that the default get the size of images that i have on the drawable-mdpi folder and not from the default drawable folder. what am i doing wrong? also i have nothing declare on the manifest.
mdpi is the default, so drawable and drawable-mdpi are the same thing and I don't know which Android chooses in this case - but you're designing for a tablet and tablets are generally mdpi devices so it correctly gets it from drawable-mdpi. You could use drawable-xlarge-mdpi if you want separate mdpi resources to be used for extra-large screens. Incidentally, remember a 7" 1024x600 tablet is large, not xlarge - so try drawable-large-mdpi and layout-large-mdpi and see if that gives you what you want.

android-how to differentiate the small screen ldpi and mdpi

I implemented the application which will supports the small screen ldpi and hdpi. It is working for only one either ldpi or hdpi. Because my application layout have some distance between the views. so i want differentiate the ldpi and mdpi. how to implement this can any body help me.
For this i am creating the layout-small folder in the res. is any name conventions like layout-small-ldpi (or) layout-small-mdpi or exiting or not. same like normal and large (layout-normal-ldpi or layout-normal-mdpi or layout-normal-hdpi). Please give the naming convention clearly.
thanks
In your res folder you have 2 folders:
drawable and layout.
You can add folders with a postfix with "-hdpi" for hdpi specifics, "-mdpi" for mdpi and "-ldpi" for ldpi.
So you should have these folders:
drawable-hdpi
drawable-mdpi
drawable-ldpi
layout-hdpi
layout-mdpi
layout-ldpi
You can also use specifics for landscape orientation or even SDK versions.
Read http://developer.android.com/guide/topics/resources/providing-resources.html for more info
To seemlessly support landscape / portrait mode, screen with different densities, you need to put appropriate "drawables" with following naming convention # res directory. res directory will be # root level of you application folder. For example LunarLander\res. res has different directories for storing images (drawables), layaout (for UI) and values (for font support).
You need to use the same name for all modes, this name shall be used in the application regardless of the mode.
Nomenclature is something like . (This may not be the exhaustive list) where type would be drawable / layout / values etc.
language would en / fr for english and french respectively. (this list is also not complete)
mode would be land / port for landscape and portrait respectively
density would be hdpi / mdpi / ldpi for high / medium and low density screen respectively.
Example
Suppose I have a background image, which needs to look differently for landscape and portrait mode. Then I will place two different images for background into res\drawable-land and res\drawable-port\ folder under the name background_image.png. I will refer to these in my application (or layout) using background_image as the name. The android system will appropriate choose the image based on the mode.
Same is applicable to ldpi / hdpi or en / fr.
Basically you need to create appropriate folder structure and keep the same name for entities. (string, drawable, layout etc)
Shash

Categories

Resources