Same view size on all screens - android

I need to display view with exact size in milimeters. The problem I face is that on many android devices when i apply view size im mm, the output differs. Is there any way to achieve the exact same view size on all screens?
I tried with:
pixels = (mm * dpi) / 25.4
context.getResources().getDimensionPixelOffset(R.dimen.size) (100mm)
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 100, displayMetrics);
and nothing

You have to concentrate on Device Compatability to all screens:
add this dependency to your build.gradle
compile 'com.intuit.sdp:sdp-android:1.0.3'
for eg : dimens.xml
<dimen name="_1sdp">1.20dp</dimen>
<dimen name="_2sdp">2.40dp</dimen>
<dimen name="_3sdp">3.60dp</dimen>
<dimen name="_4sdp">4.80dp</dimen>
<dimen name="_5sdp">6.00dp</dimen>
<dimen name="_6sdp">7.20dp</dimen>
<dimen name="_7sdp">8.40dp</dimen>
<dimen name="_8sdp">9.60dp</dimen>
<dimen name="_9sdp">10.80dp</dimen>
<dimen name="_10sdp">12.00dp</dimen>
<dimen name="_11sdp">13.20dp</dimen>
<dimen name="_12sdp">14.40dp</dimen>
<dimen name="_13sdp">15.60dp</dimen>
<dimen name="_14sdp">16.80dp</dimen>
<dimen name="_15sdp">18.00dp</dimen>
<dimen name="_16sdp">19.20dp</dimen>
<dimen name="_17sdp">20.40dp</dimen>
<dimen name="_18sdp">21.60dp</dimen>
<dimen name="_19sdp">22.80dp</dimen>
<dimen name="_20sdp">24.00dp</dimen>
<dimen name="_21sdp">25.20dp</dimen>
<dimen name="_28sdp">33.60dp</dimen>
<dimen name="_30sdp">36.00dp</dimen>
<dimen name="_31sdp">37.20dp</dimen>
<dimen name="_32sdp">38.40dp</dimen>
<dimen name="_33sdp">39.60dp</dimen>
<dimen name="_34sdp">40.80dp</dimen>
<dimen name="_40sdp">48.00dp</dimen>
<dimen name="_41sdp">49.20dp</dimen>
<dimen name="_42sdp">50.40dp</dimen>
for eg: <TextView
android:layout_width="match_parent"
android:layout_height="#dimen/_10sdp"
android:layout_gravity="center"
android:gravity="center"/>
will support all screen sizes.

Related

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.

Adjusting Text for Screen Sizes - Google Nexus 5 Example

I recently ran some testing on the AWS device farm and was shocked to find that my text sizes are not scaling appropriately.
Currently, I have different folders for my text sizes:
values
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="fab_margin">16dp</dimen>
<dimen name="center_indentation">80dp</dimen>
<dimen name="poll_question_text_size">14dp</dimen>
<dimen name="margin_left">80dp</dimen>
<dimen name="margin_bottom">8dp</dimen>
<dimen name="radio_button_answer_text_size">12dp</dimen>
<dimen name="answer_label_text_size">6dp</dimen>
<dimen name="answer_value_label_text_size">8dp</dimen>
<dimen name="vote_count_label_text_size">11dp</dimen>
values-sw600dp
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="fab_margin">16dp</dimen>
<dimen name="center_indentation">80dp</dimen>
<dimen name="poll_question_text_size">22dp</dimen>
<dimen name="margin_left">160dp</dimen>
<dimen name="radio_button_answer_text_size">14dp</dimen>
<dimen name="margin_bottom">16dp</dimen>
<dimen name="answer_label_text_size">9dp</dimen>
<dimen name="answer_value_label_text_size">10dp</dimen>
<dimen name="vote_count_label_text_size">13dp</dimen>
values-sw720dp
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="fab_margin">16dp</dimen>
<dimen name="center_indentation">80dp</dimen>
<dimen name="poll_question_text_size">30dp</dimen>
<dimen name="margin_left">185dp</dimen>
<dimen name="margin_bottom">20dp</dimen>
<dimen name="radio_button_answer_text_size">18dp</dimen>
<dimen name="answer_label_text_size">10dp</dimen>
<dimen name="answer_value_label_text_size">12dp</dimen>
<dimen name="vote_count_label_text_size">14dp</dimen>
In each folder, I create a dimens.xml file that I used to pull the appropriate text size for a given screen.
I went to the device metrics website located here and found was specifically looking at the Google Nexus 5. It appears that the Nexus 5, after I run my emulator, is pulling from my sw600dp folder. I am not sure why, and it is generating a text size that I would hope to see on a 7" tablet and not a 4.5" handheld device.
I have read countless posts on StackOverflow but cannot find any way to properly account for all devices. I understand that there are 6", 7", 8", 9", and 10" devices, and I want to ensure that I am accounting for each size.
Here is how my text looked for my Google Nexus 5, note I am referring to the radio_button_ansewr_text_size item:
When specifying text size, always use sp:
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
or
<dimen name="text_size">13sp</dimen>
http://developer.android.com/training/multiscreen/screendensities.html
If you need to set text sizes in java code, do it this way:
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);

error: Error: Integer types not allowed (at 'Dimens' with value '1')

<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">17dp</dimen>
<dimen name="activity_vertical_margin">17dp</dimen>
<dimen name="button_margin">0dp</dimen>
<dimen name="button_size">0dp</dimen>
<dimen name="general_weight">1</dimen>
<dimen name="button_text_size">22.5sp</dimen>
<dimen name="fab_margin">18dp</dimen>
<dimen name="short_display_text_size">50dp</dimen>
<dimen name="long_display_text_size">25dp</dimen>
</resources>
android studio show this
Error:(7, 5) Integer types not allowed (at 'general_weight' with value '1').
how can i manage weight properties using dimens file?
how can i manage weight properties using dimens file?
Weight, at least as associated with LinearLayout, is an integer, not a dimension.
Change:
<dimen name="general_weight">1</dimen>
to:
<integer name="general_weight">1</integer>
and refer to it as #integer/general_weight in your android:layout_weight attributes.

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