Skip to content

Using attributes

ZieIony edited this page May 10, 2020 · 2 revisions

Each style uses attributes to provide values to views. Here's an example XML with a view declaration:

<LinearLayout>
    <ImageView
        style="@style/ImageView.Icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/ic_airplanemode_on_24px" />
</LinearLayout>

The enclosing layout is invalid because it needs at least layout_width and layout_height attributes to be parsed correctly. Let's forget about for the sake of simplicity. This view has an inline style with four attributes:

  • style="@style/ImageView.Icon" - style attribute is a special attribute parsed by the framework. It's used to change apply a style defined in an external XML file. There's a couple of special tags and attributes without any prefixes: layout, fragment, include, data and probably a couple more.

  • android:layout_width/_height="wrap_content" - attribute usages prefixed with android are defined by the Android framework. You can reuse them in your custom views if you wish. Attribute names prefixed with layout_ by convention are used by enclosing layouts. You can learn how to add your own layout attributes here. wrap_content is an enum - one of many types of attributes. Attributes can reference values, resources, styles and many more.

  • app:srcCompat="@drawable/ic_airplanemode_on_24px" - this is an attribute added by a library. The app prefix is declared somewhere in the layout and can have any name. srcCompat is an attribute of type drawable and is used here to add support for vector drawables. It references a file from res/drawable folder.

Another example:

<layout>
    <TextView
        android:layout_width="120dp"
        android:layout_height="32dp"
        guide_htmlText="Select Dates"
        android:textColor="?android:attr/textColorPrimary"
        android:textAppearance="@style/TextAppearance.Body1" />
</layout>

This view is enclosed in layout tag - it's a data binding declaration. This tag has some special meaning and features.

  • This view also uses layout_width and layout_height, but this time with values of type dimension. The dp means that the value is expressed in device independent pixels. Some of the attributes have more than one type.
  • guide_htmlText="Select Dates" - this is a library attribute and is used without any namespace prefix. It's a data binding attribute handled by a binding adapter.
  • android:textColor="?android:attr/textColorPrimary" - this attribute has a theme reference value. The question mark starts a theme reference. The android: part is a namespace and is empty for custom attributes. The attr/textColorPrimary part declares an attribute present in the current theme to take a value from.
  • android:textAppearance="@style/TextAppearance.Body1" - a framework's attribute of reference type. Is used to change text style of text views. Here it references a style resource of name TextAppearance.Body1.

Resources are a very broad topic. Even a very simple application usually needs at least a theme, a couple of drawables (icons) and uses styles and colors declared somewhere. Read more about resources on developer.android.com.

Clone this wiki locally