Abbas
Abbas

Reputation: 3331

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.

Upvotes: 0

Views: 1030

Answers (2)

Edalat Feizi
Edalat Feizi

Reputation: 1401

you have also another way to do that...

after set a background for your RecyclerView set background to your RecyclerView items

like this

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_most_trending"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/recyclerView_background"
    >

</android.support.v7.widget.RecyclerView>
<?xml version="1.0" encoding="utf-8"?>

set background to RecyclerView item

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@drawable/recyclerView_item_bg">
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="right"
      android:padding="10dp"
      />
 </LinearLayout>

Upvotes: 0

Abbas
Abbas

Reputation: 3331

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>

Upvotes: 2

Related Questions