bsr
bsr

Reputation: 58682

Android constants in attrs.xml

I see dimension constants declared in android attrs.xml (data/res/values/attrs.xml) such as

<attr name="padding" format="dimension" />
<!-- Sets the padding, in pixels, of the left edge; see {@link android.R.attr#padding}. -->
<attr name="paddingLeft" format="dimension" />
<!-- Sets the padding, in pixels, of the top edge; see {@link android.R.attr#padding}. -->
<attr name="paddingTop" format="dimension" />
<!-- Sets the padding, in pixels, of the right edge; see {@link android.R.attr#padding}. -->
<attr name="paddingRight" format="dimension" />

If I am correct, the value is defined in ./data/res/values/styles.xml as <item name="android:paddingLeft">20dp</item> etc.

Are these some kind of "preferred" dimension values. Are these values relatively stable between release so that the apperance wont be affect if one directly use these constants.

Thanks.

EDIT:

To clarify a bit, I was wondering the implication of using the these "android" defined values in my view layout, like.

<TextView android:text="@string/text"
    android:layout_width="wrap_content"
    android:layout_height="?android:attr/listPreferredItemHeightSmall"
    android:paddingStart="?android:attr/expandableListPreferredItemPaddingLeft"/>

Instead of me defining these values, I am referring it in the android namespace. Is it safe to do so?

Upvotes: 0

Views: 1757

Answers (2)

Chintan Rathod
Chintan Rathod

Reputation: 26034

As you can create Drawables with different postfixes, Layouts with postfixes, you can create values directory by different postfixes like

values
values-large "or" values-sw600dp
values-xlarge "or" values-sw720dp

In this, you can create your constant. This will helpful while creating dynamic layouts.

Suppose you need to create a layout for normal screen and tablet, both requires different spacing. So what will you do? Create two different layouts? or simply add two entries in values directory?

Answer is values directory. Its efficient.

Edit

layout.xml

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/margin_left">

And you have defined margin_left inside values and values-sw600dp directory like

<dimen name="margin_left">20dp</dimen>

<dimen name="margin_left">40dp</dimen>

respectively. This will give you lot more idea to create one layout and efficient handling.

Upvotes: 2

Michał Z.
Michał Z.

Reputation: 4119

I'm not sure if i correctly understand the question but if you use independent units like dp, dip, sp etc instead of raw values in pixels then it will be looking the same on different devices. Here you can read about it: http://developer.android.com/guide/practices/screens_support.html

Upvotes: 0

Related Questions