Search code examples
androidxmlandroid-layoutxml-drawable

Add Rounded corners to StateSelected Background of TextView using XML


I have RecyclerView which holds some TextViews. I have set background of the RecyclerView to the following recycler_view_background.xml.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#000000" />
    <corners android:topLeftRadius="@dimen/player_category_radius" android:topRightRadius="@dimen/player_category_radius"
        android:bottomLeftRadius="@dimen/player_category_radius" android:bottomRightRadius="@dimen/player_category_radius"/>
    <stroke android:color="#D3D3D3" android:width="1dp" />
</shape>

And it works fine, I get rounded corners on my RecyclerView.

Here's the catch, when I try to add a background selector to any one of the TextViews they don't show rounded corners. Here's the background xml for each item category_item_selector.xml.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@drawable/controlbar_gradient">
        <shape>
            <corners android:topLeftRadius="@dimen/player_category_radius" android:topRightRadius="@dimen/player_category_radius"
                android:bottomLeftRadius="@dimen/player_category_radius" android:bottomRightRadius="@dimen/player_category_radius"/>
            <stroke android:color="#D3D3D3" android:width="1dp" />
        </shape>
    </item>
</selector>

I am programmatically setting TextView's selected to true in my RecyclerView.Adapter.ViewHolder's OnClickListener (Selector is working fine or else I wouldn't have reddish background to the selected item).

Here are the snippets from the application.

enter image description here enter image description here

RecyclerView has rounded corners but TextView's background is drawing over it. So when the selected View is on Top or Bottom.

Rounded corners are no longer visible even though it shouldn't when I have added rounded corners on selected views.

I keep getting similar solutions when searching for this. And mine is correct according to this.


Solution

  • So while posting this question here. I revised and found my mistake. So here it is.

    In my category_item_selector.xml I had set background to @drawable/controlbar_gradient which was defined as

    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <gradient
    
            android:angle="90"
            android:type="linear"
            android:startColor="#7e0809"
            android:endColor=  "#fa0000" />
    
    </shape>
    

    And later again in my category_item_selector.xml I was again adding shape. So the compiler took the first shape it found and rendered that. So all I had to do was change category_item_selector.xml to

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true">
            <shape>
                <gradient
    
                    android:angle="90"
                    android:type="linear"
                    android:startColor="#7e0809"
                    android:endColor=  "#fa0000" />
    
                <corners android:topLeftRadius="@dimen/player_category_radius" android:topRightRadius="@dimen/player_category_radius"
                    android:bottomLeftRadius="@dimen/player_category_radius" android:bottomRightRadius="@dimen/player_category_radius"/>
                <stroke android:color="#D3D3D3" android:width="1dp" />
            </shape>
        </item>
    </selector>