How to resize webview objects/texts depending on screensize - android

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>

Related

What is the ideal way to scale font size in App Development?

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>

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" />

Make the size of a TextView equal across devices and density

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.

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

Android weight with margins and paddings

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

Categories

Resources