Storing dimensions in xml file in Android - android

I am trying to consistently use the same dimension in all the views in my android app (e.g., a left margin of 20dp). If I were using HTML, I would simply use a CSS file, but I am at a loss of how to do this on Android.
Is there a way I can store this value in a xml file inside res/values and then use it in layouts?
e.g., I thought of storing them in strings.xml like
<string name="app_wide_left_padding">20dp</string>
and then using the following text in layout.xml
android:paddingLeft="#string/app_wide_left_padding"
but I am not sure it will work. Am I on the right track?
Thanks in advance for your answers.

Create a file called res/values/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="icon_width">55dip</dimen>
<dimen name="icon_height">55dip</dimen>
<dimen name="photo_width">170dip</dimen>
<dimen name="photo_height">155dip</dimen>
</resources>
Then reference them in your other xml:
android:padding="#dimen/icon_width">

You can use styles (res/values). This article will be a good start.

Related

When to use attrs.xml, when dimens.xml?

I like to integrate ads with AppLovin into my Android App.
In their documentation they say:
Declare the base banner height of 50dp in res/values/attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="banner_height">50dp</dimen>
</resources>
I also have a dimens.xml present with other <dimen... value's for my app. They work from there as well.
What is the difference between attrs.xml and dimens.xml? What to use in a situation like this?
attrs.xml is a file that allows you to define custom attributes for your views in XML layout files. These attributes can be used to customize the appearance and behavior of your views, and can be accessed programmatically in your Java code.
dimens.xml on the other hand, is a file that allows you to define dimension values for use in your app. These values can be used to set the size and layout of views in your XML layout files, and can also be accessed programmatically in your Java code.
In this situation, you should use dimens.xml to define the banner_height dimension, because you will use this value to set the height of the banner ad view in your layout.
You could also use attrs.xml to define the banner_height attribute, but since you are defining a dimension value, not an attribute, it would be better to use dimens.xml instead.
It doesn't actually matter - <attr> and <dimen> are both value resources, and you can put them in any file in the res/values folder. They'll all be combined into the same set of resources, so the actual file you use is up to you! So you can do this if you want:
# res/values/whatever.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="someDimension">48dp</dimen>
<attr name="someColour" format="color"></attr>
</resources>
and it'll work exactly the same as if you used attrs.xml and dimens.xml. It's just convention to put attrs and declare-styleable things in a file called attrs.xml, all your dimension resources in dimens.xml, all your strings in strings.xml...
But you don't have to do that, it's up to you! For example, you might want certain resource strings to be stored in a different file, for organisation (maybe they shouldn't be translated). Or maybe some values are common to all configs and you want those in a single file, and you want a separate file for the qualified stuff (that goes in folders based on API level, night theme etc)

How can I share a int value globally (xml and code)?

In my Android app, I use LinearLayout's weight at multiple point in my app. Those weight are both used in xml and in code as I change them dynamically.
To reuse the same values everywhere, I thought I could declare them in my dimens.xml file like this:
<resources>
<dimen name="total_weight">5</dimen>
<dimen name="expanded_weight">3</dimen>
<dimen name="collapsed_weight">2</dimen>
</resources>
But it doesn't work as dimen only accept dp, px or sp units.
What workaround could I use?
you can use
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="integer_name">0</integer>
</resources>
access by
resources.getInteger(R.integer.integer_name)
you can take a look at https://developer.android.com/guide/topics/resources/more-resources

Android is ignoring /res/values-large/strings.xml

I want to use a different string resource on small screens but Android always uses the default string. Am I doing it wrong?
/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="continue">Continue</string>
</resources>
/res/values-large/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="continue">Continue to Next Page</string>
</resources>
#string/continue always resolves to "Continue", even on large screens. It does, however use the dimensions in /res/values-large/dimens.xml so I'm sure I'm using the right resource qualifier. I also tried /res/values-w550dp/strings.xml and it didn't work either.
Am I doing something wrong or is this a limitation of Android?
values-large is only available for dimens and styles, but not for string resources.
one could possibly add custom string-arrays and then look them up accordingly.
see Android Resource Types.

Padding value in android

What is the meaning of this line of code?
android padding Right = "# dimen /activity_horizontal_margin".
What is #dimen referring to?
you use the dimens file like this so that you can easily set many things to the same value (like a constant in code), or so you can override the value easily for different screen sizes.
dimen is located in / res / values / in studio project like below
dimen.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="action_button_min_width">56dp</dimen>
<dimen name="indeterminate_progress_size">32dp</dimen>
</resources>
Learn more about project structure in android studio
https://developer.android.com/studio/projects/index.html
dimen.xml is the file which you can set constant values for dimensions.
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
It is usually created inside /res/values folder. If you don't find it there you can create and add values.
You may refer the following link for more details.
https://developer.android.com/samples/BasicSyncAdapter/res/values/dimen.html

Is "sp" also accepted in the dimens.xml for a Kotlin Android app?

<?xml version="1.0" encoding="utf-8"?> <resources>
<dimen name="min_text_height">48dp</dimen> </resources>
In the place of 48dp, can sp also be used as a viable size?
Yes it can.
You might also find useful:
How to use dimens.xml in Android?

Categories

Resources