Android weight with margins and paddings - android

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

Related

How to make the android app fit all Screen Sizes?

I have a customized application , i install the app for select users . My application has separate drawable for each sizes (no problems with the drawables) , but my textViews and other content sizes does not change as per screen size . i have dimens values and i use only these 3 values across my 60 layouts, but Each time i install the app in client device, i change to values in dimens.xml according to the device inches and then build multiple apks for multiple screen size.
How do i solve this problem and make app fit all screen sizes ?
My dimens.xml
< // screen 6 inch <dimen name="radio_button_text_size" tools:ignore="MissingDefaultResource">13dp</dimen>
<dimen name="title_size" tools:ignore="MissingDefaultResource">13dp</dimen>
<dimen name="value_size" tools:ignore="MissingDefaultResource">15dp</dimen>
-->```
"'//screen 6+ inches
<dimen name="radio_button_text_size" tools:ignore="MissingDefaultResource">15dp</dimen>
<dimen name="title_size" tools:ignore="MissingDefaultResource">17dp</dimen>
<dimen name="value_size" tools:ignore="MissingDefaultResource">18dp</dimen>
"'
<!-- // screen 5inch
<dimen name="radio_button_text_size" tools:ignore="MissingDefaultResource">7dp</dimen>
<dimen name="title_size" tools:ignore="MissingDefaultResource">9dp</dimen>
<dimen name="value_size" tools:ignore="MissingDefaultResource">11dp</dimen>
-->
This happened to me as well a while back. I'm assuming you're using padding to position your views. Instead, you're gonna want to use a constraint layout and then use "horizontal bias" and "vertical bias" (the little scroll bars under the "Constraint Layout" section of the design view) to position your views. This will look the same on most screen sizes. Hope this helps!

Android widget Textview size for smaller screens

In my widget layout file, I have set textview text sizes and all my content fits inside the widget. When I launched on a smaller phone (320x480) a lot of the text was cut off and was not visible on the widget. How can I account for smaller phone sizes and define smaller textview sizes?
You should use dimens to set textsize based on phone's screensize.
Look in above image I made 4 dimens for different screen sizes.
In dimens(w820dp) I wrote following code.
<resources>
<!--include 7" and 10" devices -->
<dimen name="activity_horizontal_margin">64dp</dimen>
<dimen name="item_image_size">60dp</dimen>
</resources>
While in dimens.xml(sw600dp) I wrote following code as per requirements.
<resources>
<!-- For 7” tablets (600dp wide and bigger) -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="item_image_size">54dp</dimen>
</resources>
Refer this answer and
add
android:maxWidth="size" //size- the number more than 1
this in your textview layout

Best practice for text size dimen declarations

I'd like to declare some dimensions for naming some common text sizes in my app. Are there any agreed upon standards or recommended best practices for the various res categories? Or, have there been any clever solutions for basing common text sizes dynamically at runtime on the current system default? For example, default text size is considered normal text. A smaller size is declared for minor text/descriptions, a slightly larger size for minor headlines, a much larger size for major headlines, etc.
In our code, We often set 3 or 4 steps for text size to keep it looks uniform, such as
<dimen name="textview_small_size">11sp</dimen>
<dimen name="textview_middle_size">13sp</dimen>
<dimen name="textview_big_0_size">21sp</dimen>
<dimen name="textview_big_1_size">30sp</dimen>
And for perfect UI appearance, You should define separate dimens in different DPIs and screen size like values-sw800dp-large-port.
So, just set the text size to make it looks perfect and keep a uniform rule.
There is no difference between set size in xml and in runtime, there're basically the same.
This might be a late answer but I feel it would still be helpful.
For different screen sizes, it is recommended to use dedicated resource files as mentioned here.
However, structuring content in a given resource file also proves useful (The same conventions can be used for text and dimension resources). Here, I use conventions similar to material colors
<resources>
<dimen name="normal_100">16dp</dimen>
<dimen name="normal_125">20dp</dimen>
<dimen name="normal_150">24dp</dimen>
<dimen name="normal_175">28dp</dimen>
<dimen name="normal_200">32dp</dimen>
<dimen name="normal_225">36dp</dimen>
<dimen name="normal_250">40dp</dimen>
<dimen name="normal_275">44dp</dimen>
</resources>
This greatly increases reusability and avoids declaring write-once-use-once dimension literals like
<dimen name="home_screen_banner_textLeftPadding">16dp</dimen>
For a complete descriptive article, see this.
Hope this helps! :)
You need to maintain different values folder for different device size and you can mention your text size in those values folder. Android will take appropriate text size as per device. For reference you can check pdf provided by GOOGLE IO. You will find how they are suggesting for using dimens.xml for different devices of android. For Example see the structure given below:
res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-xlarge/dimens.xml
For Example
In values/dimens.xml
<dimen name="text_size">10dp</dimen>
In values-xlarge/dimens.xml
<dimen name="text_size">20dp</dimen>

Android font size on different screens

I want to use different font size for different screen sizes.
I read about this many articles, but I'm not sure about usage. Is correct to use different dimens resource file for different screen dimensions like code below:
res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-xlarge/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">18sp</dimen>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">24sp</dimen>
</resources>
...
I also read that correct way is to use sp for font size, but this doesn't fit font in different screen dimensions as I expect.
If there, what are the disadvantages of using different dimens for every size?
Thanks
The best way is to create different layout resources for each of the screens you wish to support. Place each of the layouts in a separate folder that designates the width of the screen. For example, normal sized layouts go in your res/layout folder, and a layout resource for a 7 inch tablet (600 pixel width) would go in the res/layout-sw600dp folder. Make the resource names identical, but adjust your font sizes accordingly.
#up Not, it isn't good way.
#topic You can gets width & height of screen (and w&h of View). Next, you can set font, for example 2% of width screen. If you have content 1260x720, 0.02*1260=24,6 px (you can use also (int)24.6 to convert double to int)
I do something very similar and it's worked fine for me. Some people put the values in the various layouts such as Fietser suggested, but if all of your layouts end up being the exact same except for the font size, your approach is better. That way you can have a single layout and only modify the font sizes. But sometimes you might have changes in the actual layout xml, so then it's probably a wash between the two approaches.

How to resize webview objects/texts depending on screensize

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>

Categories

Resources