Stack Overflow
Stack Overflow

Reputation: 2968

What is the difference between <selector> and <shape> in android?

What is the difference between <selector> and <shape> in android? When to use <selector>, and <shape>?

Upvotes: 2

Views: 891

Answers (2)

Pavel Poley
Pavel Poley

Reputation: 5587

<selector> element describes ColorStateList in XML file, ColorStateList is an object that can apply as a color, but will change colors, depending on the state of the View.

Available states:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:color="hex_color"
        android:state_pressed=["true" | "false"]
        android:state_focused=["true" | "false"]
        android:state_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_window_focused=["true" | "false"] />
</selector>

<shape> defines a geometric shape, including colors and gradients for a Drawable.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006819

What is the difference between <selector> and <shape> in android?

Other than being XML used in layout resources, they are unrelated.

When to use <selector>, and <shape>?

<shape> is a way to define a ShapeDrawable in a drawable resource, for representing rectangles and ovals with various borders and fills.

<selector> is a way to define a StateListDrawable in a drawable resource, for indicating other drawables to use based on the state of the widget that is applying this StateListDrawable.

So, in the context of a button, the <selector> would say what to use for the normal background, the pressed background, the disabled background, and so on. <shape> would be used to provide the actual background for one of those states.

You can read more about the different drawable resource types in the documentation.

Upvotes: 4

Related Questions