Padding value in android - 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

Related

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 Studio sample programm Error

I have this error when working in Android Studio. Please help me. I am new to android studio.
I suppose you copied all these lines from another project that you found online.
In the project where these lines originally come from there is a file dimens.xml in res folder which contains all these #dimen/... values. These are numeric values and they are used to set the padding of your Layout
You must copy this file in your project too or just delete the first 4 red underlined lines.
As for the 5th line you may leave it like this:
tools.context=".MainActivity"
1) create dimens file It's located in res/values/dimens , here's what a sample of the file look like:
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
2) Make activity context like this:
tools:context=".MainActivity"

Cannot resolve symbols for dimen, mipmap, string although the corresponding xml files exist in res folder

android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.android.datafrominternet.MainActivity">
I am new to android development.
Above is part of my XML file.The system says cannot resolve symbol..... for all these. I do have all the dimen, string, written in the res folder already. I am not able to access them (both anything from dimen.xml or string.xml) in my XML file.
Similarly in the AndroidManifest.XML I am not able to access #mipmap/ic_launcher. Although I do have it in my res folder.
I have tried hard and not able to remove the error.
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
this is how my dimen.xml looks like. similarly string.xml is well defined too but still it says cannot resolve symbols
Goto res folder in your project structure, go to values directory and check whether dimen.xml file is exist or not. If exists then initialize your values like below
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen> // 16dp is customizable as per your requirement

dimens.xml -> R not generating

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>

Storing dimensions in xml file in 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.

Categories

Resources