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" />
Related
I know this question might sound a bit odd. I am a newbie in Flutter Development. While making my app responsive I always face an issue while scaling the text. If I keep the font size constant (say 10 or 20) it sometimes looks too small on devices with large resolution or too big on small phones. Then I tried scaling it with respect to screen dimensions (like 4% or screen width etc) but then the question remains to scale it with respect to screen height or width ? Hence I want to know what is the ideal way for scaling the text in general.
Thankyou in advance for answering.
You can create dimension files and the sizes defined in these files will be used according to the device's dpi.
You will have to create:
res/values-mdpi/dimens.xml
res/values-hdpi/dimens.xml
res/values-xhdpi/dimens.xml
res/values-xxhdpi/dimens.xml
res/values-xxxhdpi/dimens.xml
Following are the ratios you would want to use for different screen size:
3:4:6:8:12 (m:h:xh:xxh:xxxh)
Let's say you have a device(hdpi), you want to set the text size to 12sp.
So you will set text sizes as:
mdpi - 9sp
hdpi - 12sp
xhdpi - 18sp
xxhdpi - 24sp
xxxhdpi - 36sp
Make sure the name should be same in all 'dimens.xml'
res/values-mdpi/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="title">12sp</dimen>
<dimen name="paragraph">9sp</dimen>
</resources>
res/values-hdpi/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="title">16sp</dimen>
<dimen name="paragraph">12sp</dimen>
</resources>
res/values-xhdpi/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="title">24sp</dimen>
<dimen name="paragraph">18sp</dimen>
</resources>
res/values-xxhdpi/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="title">32sp</dimen>
<dimen name="paragraph">24sp</dimen>
</resources>
res/values-xxxhdpi/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="title">48sp</dimen>
<dimen name="paragraph">36sp</dimen>
</resources>
I have 4 different values folder in my res folder with different dimens.xml inside it. Each dimens.xml file have different values. The folders i got was the following:
values/dimens.xml
values-sw320dp/dimens.xml
values-sw480dp/dimens.xml
values-sw600dp/dimens.xml
The problem is when I run my app, it seems that the value used is the dimens.xml values stored in values-sw320dp/dimens.xml even when I run it in my Google Nexus 5 emulator.
Example usage is this:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Account "
android:textSize="#dimen/common_textSize_n" />
What am I doing wrong?
Or what is the proper way to use different dimens values for different screen sizes?
Any help would be greatly appreciated. Thank you very much!
I apologize that this question seems have multiple duplicates. I just didn't find the answer to the already asked questions.
You have to put different value in each folder.and dimens.xml will be the default one,if any of the dimension is missing it will take form there. and all other cases it will take the values according to the screen size. what you are doing is correct ,also you can add more dimens for mdpi,xhdpi,hdpi,xhdpi etc.
Rename the folder names to values-w max_size dp
E.g. :-
values-w320dp
values-w600dp
values-w820dp
Every folder will have one file namely dimens.xml.
Lets say an example if you have "feed_title_size" dimen, using in any layout file.
You want to set the textSize with this dimen but for different screen resolutions. then you need to create dimens.xml file in every resolution folder(e.g. values-w320dp)
and create a dimen attribute in it with "feed_title_size" name inside the resources tag.
So "feed_title_size" will present in multiple dimens.xml file and the appropriate value for it will be selected automatically on the basis of the screen resolution.
Here is the dimens.xml in values-w320dp folder looks like:
<resources>
<dimen name="feed_name_handle_width">100dp</dimen>
</resources>
Here is the dimens.xml in values-w640dp folder looks like:
<resources>
<dimen name="feed_name_handle_width">150dp</dimen>
</resources>
Here is the original dimens.xml file which contains in values folder. This file is default file.
<resources>
<dimen name="feed_text_width">50dp</dimen>
</resources>
Here is your layout file which contains your TextView
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/feed_text_width" />
I've been searching at this but can't find a solution. I have a custom TextView which goes into an ArrayAdapter, the size of the text is 18sp. I've tested this on two emulators: 7" mdpi and 4.7" xhdpi and the difference is pretty noticeable. It basically ruins my layout. I'm developing for version 4+.
The smaller one is too small for the mdpi display and the first one is too big. How can I make the text display equal (or with very minimal discrepancy) between these two devices? The font settings are set to normal.
You can use res/values/dimens.xml to define your text size like so:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">18sp</dimen>
</resources>
And then you can use multiple configuration qualifiers for the res/values folder to provide a different dimens.xml for each screen size. For example:
res/values-small/dimens.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">24sp</dimen>
</resources>
res/values-large/dimens.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">14sp</dimen>
</resources>
And apply your dimension like this in a layout:
<TextView android:id="#android:id/title"
android:textSize="#dimen/text_size"/>
Optional:
Refer to your dimension in your res/values/styles.xml like so:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="my_text">
<item name="android:textSize">#dimen/text_size</item>
</style>
<resources>
And apply your style like this in a layout:
<TextView android:id="#android:id/title"
style="#style/my_text"/>
Creating different values directories for specific densities and have the values set in dimen.xml.
For example:
Create values-mdpi and values-xhdpi and each of this directories will contain dimen.xml in which you specify the size you want to use when the appropriate density is chosen.
I need to be able to use relative sizes for paddings - to be bigger or smaller depending on the device's resolution and dpi.
For example, if my view is wide 100px, I'd like to have 10px padding left and 10px padding right.
But, if it runs on a higher density screen, and it is say 250px, I need the left and right padding to be both 25pixels each.
Hardcoding pixels or dp doesn't seem reasonable, also I prefer to avoid code-behind scaling logic if I can get away with it. I also prefer NOT to use any additional weighted empty views to gain the same effect. Not sure how I can simulate padding with it either way.
Is there a way to do this from the xml? Or do I have to scale them based on the device resolution/dpi from the code behind?
It's not really "relative sizes for paddings" but I think using an Android dimension value defined in XML will do what you want. It's hard to guess from your question as you want to avoid "hardcoding pixels" but also "do this from the xml" ;)
The link above gives this example;
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textview_height">25dp</dimen>
<dimen name="textview_width">150dp</dimen>
<dimen name="ball_radius">30dp</dimen>
<dimen name="font_size">16sp</dimen>
</resources>
Then;
<TextView
android:layout_height="#dimen/textview_height"
android:layout_width="#dimen/textview_width"
android:textSize="#dimen/font_size"/>
The Android SDK itself uses this approach;
In e.g. values/dimens.xml
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
values-sw600dp/dimens.xml
<resources>
<!--
Customize dimensions originally defined in res/values/dimens.xml (such as
screen margins) for sw600dp devices (e.g. 7" tablets) here.
-->
</resources>
You can target values-FOO at API levels, screen sizes, portrait vs landscape, combinations of the above "and more" when you are providing resources by using "Configuration qualifier names".
I am creating an app that will be used for all screen sizes.. I have different layouts for small, normal, large, and x-large.. However, I also want my webview to adjust its sizes (text/images) depending on the size of the screen. I tried researching for viewport and values/dimens.xml but had no luck..Here is dimens.xml but I do not know how to apply it:
(This is for xlarge)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="body_padding_large"> 20dp </dimen>
<dimen name="text_size_xlarge"> 32sp </dimen>
<dimen name="speaker_image_size"> 64dp </dimen>
</resources>