Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnClick event on TextView stops RippleEffect on CardView

I have a TextView inside a CardView. When enabling the ripple effect for Lollipop on the CardView by adding a OnClick event and adding the property:

android:foreground="?android:attr/selectableItemBackground"

It works fine. But after adding a OnClick event to the TextView, the ripple effect still shows up when I click outside of the TextView, but it won't show when clicking on the TextView area.

Is there away to show the ripple even when I click the TextView?

Here is xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".MainActivity">

    <android.support.v7.widget.CardView
        android:id="@+id/news_card"
        android:foreground="?android:attr/selectableItemBackground"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/text"
            android:text="Test String"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </android.support.v7.widget.CardView>

</RelativeLayout>

Here is the code:

protected void onCreate (Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
    setContentView (R.layout.activity_main);

    View v = findViewById (R.id.news_card);
    v.setOnClickListener (new View.OnClickListener () {
        @Override public void onClick (final View v) {

        }
    });

    View textView = findViewById (R.id.text);
    textView.setOnClickListener (new View.OnClickListener () {
        @Override public void onClick (final View v) {

        }
    });
}
like image 236
Comtaler Avatar asked Jan 27 '15 23:01

Comtaler


1 Answers

One option is to forward the pressed state and hotspot position through to the parent view.

// Since the host view actually supports clicks, you can return false
// from your touch listener and continue to receive events.
myTextView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent e) {
        // Convert to card view coordinates. Assumes the host view is
        // a direct child and the card view is not scrollable.
        float x = e.getX() + v.getLeft();
        float y = e.getY() + v.getTop();

        // Simulate motion on the card view.
        myCardView.drawableHotspotChanged(x, y);

        // Simulate pressed state on the card view.
        switch (e.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                myCardView.setPressed(true);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                myCardView.setPressed(false);
                break;
        }

        // Pass all events through to the host view.
        return false;
    }
});
like image 131
alanv Avatar answered Oct 02 '22 15:10

alanv