How to Create Ripple Effect in Simple Layout

How to create ripple effect in simple layout

It turns out this is working as intended. The standard Material theme colorControlHighlight value is #40ffffff. So on a white background, this will not show up. Changing the highlight color to something else works, and/or changing the background color of the object.

Thanks all (especially Alanv for pointing me in the right direction).

How to make a ripple effect over a linear layout, without overriding the background color on its children?

It is a little bit complicated for that what you need but I don't think there is another way,...

You need to put your ImageView's into a ListView so that every ImageView is a ListItem and then you can set the ripple but you also need to set drawSelectorOnTop="true" otherwise it won't work correctly

Ripple effect of preference switch on SwitchMaterial?

Touches on the ViewGroup whether directly on the ViewGroup or a child flow from the ViewGroup to the child and back down. I find this post and its associated Stack Overflow answer helpful in understanding touch events.

To get the PreferenceScreen ripple effect, set up the layout with the parent layout of the switch as clickable and focusable with a ripple background.

We will want the same response whether the switch or the ViewGroup is directly touched. The switch has its own responsive background, so the background of the switch is set to @null so it is not highlighted on touch events. This can be removed if the highlighting is desired.

<LinearLayout 
android:id="@+id/switchLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:padding="16dp">

<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/switchWidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:clickable="false" />
</LinearLayout>

Since the switch will not receive click events because the parent is capturing them, we must programmatically click the switch when the ViewGroup is clicked:

val switchLayout = findViewById<ViewGroup>(R.id.switchLayout)
switchLayout.setOnClickListener {
findViewById<View>(R.id.switchWidget).performClick()
}

This all will give us the following:

Sample Image

How to give ripple effect above Src image in FAB

You can simply use foreground for your View to achieve clickable effect:
It should be like this:

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:maxImageSize="@dimen/_130sdp"
android:layout_gravity="center|bottom"
android:contentDescription="@string/app_name"
app:srcCompat="@drawable/ic_radial_gradient"
app:backgroundTint="@color/white"
app:elevation="0dp"
app:layout_anchor="@id/bottomAppBar"
app:tint="@null"
android:foreground="?android:attr/selectableItemBackgroundBorderless"
/>

We just have to add this line to the View to show ripple effect when it is clicked.

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

Add dividers and ripple effect for each GridLayout square

you can create a ripple drawable like this if you want it to be customize:

ripple.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorPrimaryDark">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="@color/colorPrimaryLight" />
<!--<corners android:radius="@dimen/button_radius_large" />-->
</shape>
</item>

<item android:id="@android:id/background">
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="@color/background"
android:startColor="@color/background"
android:type="linear" />
<!--<corners android:radius="10dp" />-->
</shape>
</item>

and use it in your layout like this:

android:clickable="true"
android:background="@drawable/ripple"

OR
you can simply do it like this:

android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"


Related Topics



Leave a reply



Submit