3

I have a dynamic list view:

<ListView android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fastScrollEnabled="true"
    android:choiceMode="singleChoice"
    android:dividerHeight="1dp"
    android:divider="#1f000000"
    android:background="@android:color/background_light"
    android:drawSelectorOnTop="true"/>

With cell:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/list_ripple">

With ripple:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/list_selected" android:state_activated="true"/>
</selector>

This works perfectly.

My problem is on another list that I am creating. Its a static list so I am just defining the list in xml using TableLayout and TableRow. Is there a way that I can get the same 'selected' state to show. And also the ripple to show?

    <TableLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="0">

        <TableRow android:id="@+id/about_row"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="16dp"
            android:paddingLeft="16dp"
            android:paddingBottom="20dp"
            android:clickable="true"
            android:background="@drawable/list_ripple">

I tried to put the ripple on the background element but that didn't work. Again best case would be to get the grey selected state showing that the table row is selected and the ripple if possible.

lostintranslation
  • 23,756
  • 50
  • 159
  • 262
  • Best I have found so far is to use a ListView and implement BaseAdapter to style static rows however I want. At that point I get list view ripple and active behavior. – lostintranslation May 30 '15 at 04:14

2 Answers2

1

i done it by adding ripple dynamically you can try both static and dynamic.(v21)

your ripple file :

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="?android:textColorHintInverse" />
        </shape>
    </item>
</ripple>

make your row.setClickable(true) and

on your click listener set background to ripple drawable like below:

tableRow.setOnClickListener(new View.OnClickListener() {
                @TargetApi(Build.VERSION_CODES.LOLLIPOP)
                @Override
                public void onClick(View v) {
                    tableRow.setBackground(mContext.getDrawable(R.drawable.list_ripple));
                    Snackbar.make(v, "row Clicked", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });

hope to be helpfull

Javad Karbasian
  • 93
  • 1
  • 1
  • 6
1

There's (now?) a simpler way.

Just add the following line to the layout for which you want the ripple effect.

android:background="?attr/selectableItemBackground"

Source: https://stackoverflow.com/a/54628991/1504065

J.P.
  • 518
  • 5
  • 10