Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same effect on TableRow click as ListView click

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.

like image 311
lostintranslation Avatar asked Nov 21 '25 13:11

lostintranslation


1 Answers

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

like image 55
Javad Karbasian Avatar answered Nov 23 '25 03:11

Javad Karbasian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!