0

I've got 3 TextView, and I want to create a custom selector drawable which:

  • creates a rectangular border with round corners (done)
  • gets selected when the user presses one of them

The TextViews:

<LinearLayout android:id="@+id/info" android:layout_marginTop="68dp"
        android:layout_width="wrap_content" android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal">
        <TextView android:id="@+id/tour1"
            android:paddingLeft="10dp" android:paddingRight="10dp"
            android:paddingTop="8dp" android:paddingBottom="8dp"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:background="@drawable/back"
            android:gravity="center"
            android:text="Tour \n disponibili  \n 21"/>

        <TextView android:id="@+id/tour2" android:layout_toRightOf="@+id/tour1"
            android:paddingLeft="10dp" android:paddingRight="10dp"
            android:paddingTop="8dp" android:paddingBottom="8dp"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:background="@drawable/back"
            android:gravity="center"
            android:text="Tour \n prenotabili  \n 16"
            android:layout_marginLeft="16dp"/>

        <TextView android:id="@+id/tour3" android:layout_toRightOf="@+id/tour2"
            android:paddingLeft="10dp" android:paddingRight="10dp"
            android:paddingTop="8dp" android:paddingBottom="8dp"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:background="@drawable/back"
            android:gravity="center"
            android:text="Tour \n preferiti  \n 3"
            android:layout_marginLeft="16dp"/>
</LinearLayout>

My Drawable selector file, back.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" >

        <stroke android:width="1dip"
            android:color="#4fa5d5"/>
        <corners android:radius="7dp"/>
    </shape>
</item>
<item  android:state_pressed="true">
    <shape android:shape="rectangle" >
        <stroke android:width="5dip"
            android:color="#ff0000"/>
        <corners android:radius="7dip" />
    </shape>
</item>

I would like to know how:

  • to successfully apply the attribute "?android:attr/selectableItemBackground"
  • or, as I just tried to do, to apply a custom one.

Unfortunately, I wasn't able to implement any of the above. In my example, the TextViews always show the first item (the one without the android:state_pressed="true" attribute) selected and when I press them they never switch to the item with the android:state_pressed="true" attribute.

Where are the flaws in my code?

1
  • Try to use Button instead Textview. Commented May 27, 2015 at 9:15

2 Answers 2

2

Try swapping the order of the two items, I think that matters:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item  android:state_pressed="true">
        <shape android:shape="rectangle" >
        <stroke android:width="5dip"
            android:color="#ff0000"/>
        <corners android:radius="7dip" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle" >

        <stroke android:width="1dip"
            android:color="#4fa5d5"/>
        <corners android:radius="7dp"/>
        </shape>
    </item>
</selector>

edit

In fact, you may need to put the descriptions of the items in separate files:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/pressed"/>
    <item android:drawable="@drawable/unpressed"/>
</selector>
Sign up to request clarification or add additional context in comments.

2 Comments

@CrisBenois yeah, I thought of another thing that's tripped me up before and added it as an edit
Try using <item android:state_pressed="false"> for the non-pressed state.
1

After applying roarster's suggestion, you have to make TextViews clickable using android:clickable="true"

The order of the items in the selector is important, because the selector will pick the first (from the top) item that matches the state of the view, therefore, the "default" state (with no android:state_* defined) should be last, otherwise it will always match.

2 Comments

thank you, it worked!! and how do I apply the attribute ?android:attr/selectableItemBackground?
Set the background for your view: android:background="?android:attr/selectableItemBackground" But, since you want to provide a custom drawable for selectableItemBackground, you need to override it in your Theme (and ensure the Activity that loads your layout is using the theme) using <item name="android:selectableItemBackground">@drawable/back</item>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.