-3

How to show a custom shape as background behind the selected item inside a recyclerview. I've tried several things but all failed:

activity_main.xml:

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/nav_drawer_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/drawer_background"
                android:theme="@style/drawer"></android.support.v7.widget.RecyclerView>

        </LinearLayout>

drawer_background.xml:

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/drawable_shape_selected" android:state_checked="true"></item>
    <item android:drawable="@android:color/transparent"></item>
</selector>

drawable_shape_selected.xml:

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

</shape>

custom_layout.xml:For RecyclerView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/TextViewDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello" />

</LinearLayout>

Update:

itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                Toast.makeText(context, "Position " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
            }
        });
Sumit Shukla
  • 4,116
  • 5
  • 38
  • 57

1 Answers1

0

Change your drawer_background.xml like this

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/drawable_shape_selected"
          android:state_pressed="true" />
    <item android:drawable="@drawable/drawable_shape_selected"
          android:state_focused="true" />
    <item android:drawable="@android:color/transparent" />
</selector>

Also add android:focusableInTouchMode="true" to the linear Layout of custom_layout.xml

This is because by default when the view is touched it will take click instead of focus.If you want view to be focused and change its background when pressed add this to your view i.e LinearLayout in xml.

EDIT:focusableInTouchMode makes the view touch mode for the first time disabling click.So to make button click happen for the first time add focus listener in java class for the button and onFocuschange add view.performClick() to make button takes click.

Button button = view.findViewById(R.id.buttonPanel2);
button.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean focus) {
                if(focus){
                   view.performClick();
                }
             }
        });
Sharath kumar
  • 4,064
  • 1
  • 14
  • 20