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
Related
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
When I add the file dimens.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="smart_eyeglass_controll_width">419</dimen>
<dimen name="smart_eyeglass_controll_height">138</dimen>
</resources>
to my values folder, R will not be generated.
What is wrong?
The file is OK - the rest of the project to. Cleaning the project up in Eclipse won't work.
Why it Showing
Because there are some compile error (or bug?) regarding to the xml file in res, so R is not genetared .
So,basically just Add unit beside your dimen value Like dp
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="smart_eyeglass_controll_width">419dp</dimen>
<dimen name="smart_eyeglass_controll_height">138dp</dimen>
</resources>
Then rebuild and clean your project
because you can't create dimens whiteout indicating that whether its a dip or sp or... if you want to create an integer value use:
<integer name="something">100</integer>
and if it has a unit put it..
<dimen name="width">100dip</dimen>
you have to specify an unit for your value, such dp. E.g
<resources>
<dimen name="smart_eyeglass_controll_width">419dp</dimen>
<dimen name="smart_eyeglass_controll_height">138dp</dimen>
</resources>
Based on here on XML Attributes section I specify following in my dimens.xml:
<dimen name="match_parent">-1dp</dimen>
<dimen name="main_left_menu_user_account_width">#dimen/match_parent</dimen>
<dimen name="main_left_menu_user_account_height">#dimen/match_parent</dimen>
Then I use the both dimensions in my layout:
<ImageView
android:id="#+id/userAccountImage"
android:background="#drawable/user_account"
android:layout_width="#dimen/main_left_menu_user_account_width"
android:layout_height="#dimen/main_left_menu_user_account_height" />
Then, when I preview to Graphical Layout, it complains:
You must supply a layout_width attribute.
You must supply a layout_height attribute.
Actually can I define a value equals to match_parent in dimens.xml?
Update:
I also tried this but the preview still complains:
<dimen name="main_left_menu_user_account_width">-1dp</dimen>
<dimen name="main_left_menu_user_account_height">-1dp</dimen>
I successfully use wrap_content (the Graphical Layout doesn't complain at all):
<dimen name="wrap_content">-2dp</dimen>
<dimen name="main_right_menu_width">#dimen/wrap_content</dimen>
<dimen name="main_right_menu_height">#dimen/wrap_content</dimen>
Use this, it works for me
<dimen name="custom_wrap_content">-2px</dimen>
<dimen name="horizontal_border_height">#dimen /custom_wrap_content</dimen>
<dimen name="custom_match_parent">-1px</dimen>
<dimen name="vertical_border_height">#dimen /custom_match_parent</dimen>
And the Reason why match_parent doesn't run. You cannot supply a build in keyword like match_parent
Edit: Use px instead of dp as suggested by Jarett Millard in the comments.
First create attribs.xml:
<resources>
<item name="match_parent" type="dimen">-1</item>
<item name="wrap_content" type="dimen">-2</item>
</resources>
Second use your dimens:
<dimen name="account_width">#dimen/match_parent</dimen>
<dimen name="account_height">#dimen/wrap_content</dimen>
Depending on why you want to define match_parent in a #dimen, this use case could help you:
Instead of defining the width and height in dimen.xml, you can define it as a style in the styles.xml
I use
//res/values/styles.xml
<style name="IntroLayout">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
and
//res/values-sw600dp/styles.xml
<style name="IntroLayout">
<item name="android:layout_width">520dp</item>
<item name="android:layout_height">wrap_content</item>
</style>
and use it like
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_gravity="center"
style="#style/IntroLayout">
which allows me to dynamically set the width and height attributes for different sized devices without having to write any code and you can use match_parent/wrap_content fine. you can use any #dimen that you have defined previously in the style as well if you want.
I use this because the layout for phone and tablet is the same, except i want to fix the width on tablet but fill the parent on phone, so it saves having to have 2 different layouts with basically the same xml
For HTC devices use this to achieve match_parent:
<dimen name="my_match_parent">-1.0px</dimen>
You can also achieve this using integers.xml file
integers.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="match_parent">-1</integer>
<integer name="wrap_content">-2</integer>
</resources>
Use in dimens.xml:
<dimen name="main_right_menu_width">#integer/wrap_content</dimen>
You might also get lint warning, to suppress it use:
<dimen name="main_right_menu_width" tools:ignore="ReferenceType">#integer/wrap_content</dimen>
I don't think so. #dimen/match_parent is a specific length with unit while match_parent is a special flag.
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.
<?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?