text_size in multiple screen support - android

I'm trying to decrease the text size of a textview depending on the device the app runs. However I can't make it work.
I created the folder values-sw320dp (also tried values-w320dp) and put inside the dimens.xml file
<resources>
<dimen name="text_size">25sp</dimen>
</resources>
and my values/dimens.xml file contains
<dimen name="text_size">18sp</dimen>
What I expect is to load the app in Nexus One (emulator), which is 482 x 320 dp, and get the text size 18sp. However I get text size 25sp.
Does anyone know what is happening?
Thank you

Create folder as values-hdpi
dimens.xml for values-hdpi
<resources>
<dimen name="text_size">18sp</dimen>
</resources>
And Create folder as values-xhdpi
dimens.xml for values-xhdpi
<resources>
<dimen name="text_size">25sp</dimen>
</resources>
And use like this-
android:textSize="#dimen/text_size"

I use values-hdpi, values-mdpi, values-ldpi, values-xlarge

Create folder as values-sw600dp and values-sw720dp in res folder.
dimens.xml for values-sw600dp
<resources>
<dimen name="text_size">4sp</dimen>
</resources>
dimens.xml for values-sw720dp
<resources>
<dimen name="text_size">6sp</dimen>
</resources>

Nexus One has dimension 480 x 800, so try
values-sw360dp

Related

Different XML values for SmartPhone vs Tablet

I am trying to have different XML values for imageview height's and imageview width's for my app. The problem is that i Need different values for the height's and width's for the smartphone version and the tablet version. Would there a way to do this? Thanks in advance.
You can use Resource Qualifiers in Android Check Here
Define the image width & height in dimens file
values/dimens.xml
<resources>
<dimen name="image_width">100dp</dimen>
</resources>
values-sw600dp/dimens.xml
<resources>
<dimen name="image_width">150dp</dimen>
</resources>
In the activity/fragment xml
<ImageView
android:width="#dimen/image_width"
android:height="#dimen/image_height" />

Android dimens resources selection

I am a bit confused how android decides what resources to choose. I have the following folders with the following files:
--
|-values
|-dimens.xml
|-values-hdpi
|-dimens.xml
|-values-land
|-dimens.xml
And the files content is the following:
First:
<resources>
<dimen name="data_dimen1">1dp</dimen>
<dimen name="data_dimen2">1dp</dimen>
</resources>
Second:
<resources>
<dimen name="data_dimen1">2dp</dimen>
</resources>
Third:
<resources>
<dimen name="data_dimen2">4dp</dimen>
</resources>
I have a device HDPI and in landscape. What will be data_dimen2 and data_dimen1 values?
data_dimen1 will be used. because values-land is for mdpi devices.

Setting font size in application

I am developing an android application. My designer gave me designs with font size 48PT in photoshop. For android how many DPs should I set in font size?
http://angrytools.com/android/pixelcalc/
This site helps you to convert your values px,dp,sp,pt,etc..
make sure you are using the right Density in the drop down.
Your 48dp equals 107sp and 106dp
Create dimen.xml for your solution.
Directory will be as follow
1. res -> values-ldpi --> dimen.xml
2. res -> values-mdpi --> dimen.xml
3. res -> values-hdpi --> dimen.xml
4. res -> values-xhdpi --> dimen.xml
5. res -> values-xxhdpi --> dimen.xml
in that file create
<resources>
<dimen name="text_size">size in sp</dimen>
</resources>`
Size will be different for all devices set that according to your layout and requirements.
Now Give Your Textview size with
android:textsize="#dimen/text_size"

Android Scaling Density

I am having an issue with scaling Android across multiple screens. According to the documentation Android OS will assume your dimens.xml is created for mdpi and scale accordingly for hdpi, xhdpi, etc...
However, I am not able to see this work. If I have a folder for values-hdpi my SGS4 looks fine with the 1.5 scale ratio applied from the dimens.xml file located in the values-hdpi. However, whenever I remove the values-hdpi folder from the project to allow Android OS to scale it up to hdpi, the SGS4 just runs mdpi values. I'm not sure what I'm doing wrong.
Android Manifest:
<!-- Screen Support -->
<supports-screens android:resizeable="true"
android:smallScreens="false"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true"/>
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="15"/>
Folder structure includes
values
values-es
values-fr
values-sw600dp
values has the mdpi values stored in the dimens.xml file all done in dp and sp.
dimens.xml:
(NOTE*) Whenever I tried to add the xml snippet from the dimens.xml it kept formatting it funny and removing all the dimen name= stuff and just lists out the dp and sp used in it. Not sure why.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="center_text_margin_right">20dp</dimen>
<dimen name="answer_row_top_padding">7dp</dimen>
<dimen name="answer_row_bottom_padding">7dp</dimen>
<dimen name="answer_number_text_size">12sp</dimen>
<dimen name="answer_left_margin_from_answer_number">18dp</dimen>
<dimen name="answer_text_size">12sp</dimen>
<dimen name="answer_points_right_margin">20dp</dimen>
<dimen name="answer_points_left_margin">18dp</dimen>
<dimen name="answer_number_left_margin">60dp</dimen>
<dimen name="center_text_size">12sp</dimen>
<dimen name="center_text_margin_left">20dp</dimen>
<dimen name="center_text_margin_right">20dp</dimen>
<dimen name="answer_title_height">28dp</dimen>
<dimen name="round_circle_margin_left">6dp</dimen>
<dimen name="round_circle_height">6dp</dimen>
<dimen name="round_circle_width">6dp</dimen>
<dimen name="total_score_right_margin">26dp</dimen>
<dimen name="total_score_left_margin">5dp</dimen>
<dimen name="score_text_size">12sp</dimen>
<dimen name="trivia_logo_height">25dp</dimen>
<dimen name="trivia_logo_width">84dp</dimen>
<dimen name="trivia_logo_margin_left">8dp</dimen>
<dimen name="question_margin_left">26dp</dimen>
<dimen name="question_margin_right">26dp</dimen>
<dimen name="question_top_margin">15dp</dimen>
<dimen name="question_margin_bottom">20dp</dimen>
<dimen name="question_text_size">12sp</dimen>
<dimen name="fun_fact_margin_bottom">100dp</dimen>
<dimen name="fun_fact_text_size">12dp</dimen>
<dimen name="answer_row_height">25dp</dimen>
</resources>
If I create a values-hdpi folder with this file
dimens.xml
30.00dp
10.50dp
10.50dp
18.00dp
27.00dp
18.00dp
30.00dp
27.00dp
90.00dp
18.00dp
30.00dp
30.00dp
42.00dp
9.00dp
9.00dp
9.00dp
39.00dp
7.50dp
18.00dp
37.50dp
126.00dp
8dp
39.00dp
39.00dp
22.50dp
30.00dp
18.00dp
150.00dp
18.00dp
37.50dp
everything will work fine
The values-es and values-fr are just folders containing strings.xml NO dimens in there. I reference all throughout my layout xml files with #dimen/answer_row_height for example.
As I stated before it works perfectly if I drop in the values-hdpi with a scaled dimens.xml file in it, but if I remove it Android does not scale it. Has anyone hit this before or know what i'm screwing up?
Values will not be automatically scaled, only drawables will be.
It's up to you to define all the alternative resources (values included) that your app will need, the framework cannot and will not make those decisions for you.
According to the documentation Android OS will assume your dimens.xml
is created for mdpi and scale accordingly for hdpi, xhdpi, etc...
That is incorrect. Whatever you put in dimens.xml is going to be used as is, if the dimensions are in pixels (px). You'll need to put them in density-independent-pixels (dp) to make them scale. dimens.xml should not be based on resolution but on screen size. You might have a dimens.xml in values and in values-large, specifying different spacing, or even different font-sizes for larger screens.
You should not have a dimens.xml in values and values-hdpi, because the dp unit already takes care of density-scaling.
Based on the comments you left in various places, it appears that you are not accessing the dimensions resources correctly. You should be using something like:
getResources().getDimensionPixelSize( R.dimen. round_circle_height );
if retrieving values from code, or
#dimen/round_circle_height
if referencing from xml.

android dimens value for different resolution devices

I want setup different dimen-values for different resolution(dp) devices, how ?
just in my dimens.xml
<resources>
<!-- for 1280x720 dp ,Default screen margins, per the Android Design guidelines. -->
<dimen name="image_thumbnail_size">200dp</dimen>
<dimen name="image_thumbnail_spacing">2dp</dimen>
<dimen name="image_detail_pager_margin">160dp</dimen>
<dimen name="activity_horizontal_margin">80dp</dimen>
<dimen name="activity_vertical_margin">100dp</dimen>
<!-- for 640x360 dp -->
<!--
<dimen name="image_thumbnail_size">100dp</dimen>
<dimen name="image_thumbnail_spacing">2dp</dimen>
<dimen name="image_detail_pager_margin">80dp</dimen>
<dimen name="activity_horizontal_margin">40dp</dimen>
<dimen name="activity_vertical_margin">30dp</dimen>
-->
add:
I have two devices, the resolution is 1280x720 ,but my phone dp is 640x360 , and another is 1280x720
Create seperate values folders, each with a different suffex. Some examples:
values-normal (for phone versions)
values-large (for 7" tablets)
values-xlarge (for large tablets)
Place a dimensions.xml file in each of these folders, in each one, specify different values for each variable.
Hope this helps.
Sorry to get this to you late but you can use the same folder combinations that you use for drawables with the values folder. Here is an example
values-ldpi/dimens.xml
values-hdpi/dimens.xml
values-xhdpi/dimens.xml

Categories

Resources