Adding ripple effect for View in onClick
Asked Answered
M

7

50

Hello I am trying to add a ripple effect onClick method for View, but this one no working. All my items having an ID, but I don't know how to call it

Here is a code.

 @Override
public void onClick(View v) {
    int[] attrs = new int[]{R.attr.selectableItemBackground};
    TypedArray typedArray = getActivity().obtainStyledAttributes(attrs);
    int backgroundResource = typedArray.getResourceId(0, 0);
    v.setBackgroundResource(backgroundResource);

    switch (v.getId()) {
        case ACTION_PLAY_ID:
            Log.d(MainActivity.TAG, getString(R.string.detail_action_play));
            v.setBackgroundResource(backgroundResource);
            Intent intent = new Intent(getActivity(), PlayerActivity.class);
            intent.putExtra(Video.VIDEO_TAG, videoModel);
            startActivity(intent);

            break;
        case ACTION_BOOKMARK_ID:
            if (bookmarked) {
                v.setBackgroundResource(backgroundResource);
                deleteFromBookmarks();
                ((ImageView) v).setImageDrawable(res.getDrawable(R.drawable.star_outline));
            } else {
                v.setBackgroundResource(backgroundResource);
                addToBookmarks();
                ((ImageView) v).setImageDrawable(res.getDrawable(R.drawable.star));
            }
            break;
        case ACTION_REMINDER_ID:
            if (!isReminderSet) {
                createReminderDialog((ImageView) v);
            } else {
                cancelReminder(liveTvProgram.getProgramId());
                ((ImageView) v).setImageDrawable(res.getDrawable(R.drawable.alarm));
            }
            break;
    }
}

For Lubomir

i have something like this but not working too:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
        savedInstanceState) {
    View view = inflater.inflate(R.layout.item_detail, container, false);
    ButterKnife.bind(this, view);

    View myView = view.findViewById(R.id.actions_container);
    int[] attrs = new int[]{R.attr.selectableItemBackground};
    TypedArray typedArray = getActivity().obtainStyledAttributes(attrs);
    int backgroundResource = typedArray.getResourceId(0, 0);
    myView.setBackgroundResource(backgroundResource);

    loadImage();
    init();

    return view;
}

ImageViews(actionbuttons) is creating in java for LinearLayout actions_container

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <ImageView
        android:id="@+id/header_image"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="@dimen/detail_image_1_state"
        android:elevation="8dp"/>

    <RelativeLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="@dimen/detail_bottom_margin"
        android:layout_marginTop="@dimen/detail_top_margin"
        android:background="@color/primary_color">

        <LinearLayout
            android:id="@+id/actions_container"
            android:layout_width="match_parent"
            android:layout_height="@dimen/detail_actions_height"
            android:layout_alignParentTop="true"
            android:background="@drawable/ripple_effect_image"
            android:elevation="2dp"
            android:orientation="horizontal"
            android:paddingLeft="300dp"
            android:paddingStart="300dp"/>

        <LinearLayout
            android:id="@+id/content_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/actions_container"
            android:orientation="vertical"
            android:paddingLeft="300dp"
            android:paddingStart="300dp">

            <TextView
                android:id="@+id/title"
                style="@style/TextTitleStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <TextView
                android:id="@+id/subtitle"
                style="@style/TextSubtitleStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="gone"/>

            <TextView
                android:id="@+id/duration"
                style="@style/TextSubtitleStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <TextView
                android:id="@+id/season"
                style="@style/TextDescriptionStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="gone"/>

            <TextView
                android:id="@+id/episode"
                style="@style/TextDescriptionStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="gone"/>

            <TextView
                android:id="@+id/description"
                style="@style/TextDescriptionStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:maxLines="7"/>

        </LinearLayout>

        <FrameLayout
            android:id="@+id/recommended_frame"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_alignParentBottom="true">

            <android.support.v17.leanback.widget.HorizontalGridView
                android:id="@+id/recommendation"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipChildren="false"
                android:clipToPadding="false"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"/>
        </FrameLayout>

        <TextView
            android:id="@+id/recommended_text"
            style="@style/TextHeaderStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@id/recommended_frame"
            android:text="@string/related_programs"/>

    </RelativeLayout>


</RelativeLayout>

Also my xml ripple effect file is like:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/dark_primary_color">
    <item>
        <color android:color="@color/dark_primary_color" />
    </item>
    <item android:id="@android:id/mask">
        <shape android:shape="rectangle">
            <solid android:color="?android:colorAccent" />
        </shape>
    </item>
</ripple>
Mondragon answered 28/7, 2016 at 6:28 Comment(2)
github.com/traex/RippleEffect use this libraryFerrer
did you check thatFerrer
C
129

Clickable Views

In general, ripple effect for regular buttons will work by default in API 21 and for other touchable views, it can be achieved by specifying

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

In code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);

    View myView = findViewById(R.id.myView);
    int[] attrs = new int[]{R.attr.selectableItemBackground};
    TypedArray typedArray = getActivity().obtainStyledAttributes(attrs);
    int backgroundResource = typedArray.getResourceId(0, 0);
    myView.setBackgroundResource(backgroundResource);
}
Customable answered 28/7, 2016 at 6:31 Comment(9)
Yea i know about ?android:attr/selectableItemBackground, but problem is that my items is generic in java code and addinglike addActonMondragon
how to call View item for making effect with java code?Mondragon
Just set this java code I wrote in your onCreate method, not in onClick. I edit my post. :)Customable
i eddited my main post with cde please check itMondragon
change this : android:background="@drawable/ripple_effect_image" to this : android:background="?attr/selectableItemBackground"Customable
problem is that i cant change it for this background becouse my colors of backgrounds going totally in wrong way.Mondragon
I had to add android:clickable="true" to see an effect of android:background="?android:attr/selectableItemBackground"Sequel
You should add also: android:clickable="true" android:focusable="true"Cantle
Is there any way to apply ripple effect only on Double Tap? I have implemented the Double Click Listener already. Is it possible to use the ripple effect programmatically?Kirwin
R
66

As stated in Lubomir Babev's answer, adding android:background="?android:attr/selectableItemBackground" does the trick.

However, if your view already has a background, you can use the same on the android:foreground attribute instead:

android:background="@color/anyColor"
android:foreground="?android:attr/selectableItemBackground"

android:foreground is only supported by API 23+ though.

Rustcolored answered 19/3, 2019 at 21:12 Comment(0)
B
9

create ripple background

view_background.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/blue" >
    <item android:drawable="@drawable/view_normal">
    </item>
</ripple>

view_noraml.xml //this is how you view appears in normal

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

    <corners
        android:radius="@dimen/button_corner"/>
    <solid
        android:color="@android:color/transparent"/>
    <stroke
        android:width="0.5dp"
        android:color="@color/white"/>

</shape>

now set the view_background to your view

example

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:foreground="@drawable/view_background"
    android:clickable="true"
    android:focusable="true"
    >
    <ImageView
        android:id="@+id/grid_item_imageView"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_gravity="center"
        android:scaleType="centerInside"
        />
</FrameLayout>
Betray answered 28/7, 2016 at 6:57 Comment(0)
I
3

I know this is a pretty old thread but just in case the above answers didn't work, I'd recommend you to try the below code as it worked for me:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

The key thing to notice are the clickable and focusable.

Inconsistency answered 21/1, 2021 at 10:5 Comment(1)
clickable should be enough in my experience. Focusable tends to make items stay selected after clicking themShanitashank
M
2

The solution for this is simple easy in my side.

Here is ripple effect:

    <?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#BFB3F7">
    <item android:id="@android:id/mask">
        <shape android:shape="oval">
            <solid android:color="@color/button_background_color" />
        </shape>
    </item>
</ripple>

and next on the class i need to search function setBackground

Then i need declare a drawable item to it. something like this:

 @Override
public void onFocusChange(View v, boolean hasFocus) {

    if (hasFocus) {
        v.setBackground(res.getDrawable(R.drawable.ripple_effect_for_buttons));
        scrollContainer(false);
    } else {
        v.setBackground(null);
        if (recommendation.getFocusedChild() != null) {
            scrollContainer(true);
        }

    }
}

And YUPII its working

Mondragon answered 28/7, 2016 at 10:44 Comment(1)
You can use android:color="?android:attr/colorControlHighlight as a ripple color.Suffrage
U
1

You can add:

 <ImageView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:clickable="true"
  android:background="@drawable/ripple"/>   
Ultramicrometer answered 28/7, 2016 at 6:34 Comment(4)
Yea i add this: <?xml version="1.0" encoding="utf-8"?> <ripple xmlns:android="schemas.android.com/apk/res/android" android:color="@color/dark_primary_color"> <item> <color android:color="@color/dark_primary_color" /> </item> <item android:id="@android:id/mask"> <shape android:shape="rectangle"> <solid android:color="?android:colorAccent" /> </shape> </item> </ripple> But this not work with effectMondragon
what is the api versionFerrer
compileSdkVersion 23 buildToolsVersion "23.0.3"Mondragon
please post layout xml file here. P/S: Ripple only working on device higher api 21Ultramicrometer
M
0

In Kotlin it could be easily done with an extension functions.

fun TypedArray.use(block: TypedArray.() -> Unit) {
    try {
        block()
    } finally {
        this.recycle()
    }
}
    
fun Context.getStyledAttributes(@StyleableRes attrs: IntArray, block: TypedArray.() -> Unit) =
        this.obtainStyledAttributes(attrs).use(block)

fun View.setClickableRipple() {
    val attrs = intArrayOf(R.attr.selectableItemBackground)
    context.getStyledAttributes(attrs) {
        val backgroundResource = getResourceId(0, 0)
        setBackgroundResource(backgroundResource)
    }
}
Moulin answered 21/8, 2020 at 7:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.