Zep
Zep

Reputation: 1

Android - No ripple effect & no tooltip on menu items in toolbar

I have a little problem: the "Tooltip" which is showing on long press on a menu item doesn't come up anymore, neither does the ripple effect on the toolbar menu items. Still, the ripple effect is coming on buttons I got in my layout, but not in the toolbar.

I'm not declaring a theme or style for the toolbar, but I'm often changing its color during runtime (but not directly in onCreate, so this should not matter, because it's also not working without changing colors).

Other solutions like this one did not work out for me...

XML for the toolbar:

<android.support.v7.widget.Toolbar
        android:background="@color/Grey"
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentTop="true"
        android:elevation="4sp"
        android:layout_alignParentStart="true" />

Code for Toolbar:

myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);

Inflating menu:

@Override
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.toolbarmenu, menu);
    invalidateOptionsMenu();
    return true;
}

My theme is a child from AppCOmpatDark (the NoActionBar one) and in onPrepareOptionsMenu I'm often changing the visibility of the menu items & changing their color...

Any help, also just directions what could trigger this error, is very appreciated.

Thanks for the help!

Upvotes: 0

Views: 719

Answers (2)

Vijay E
Vijay E

Reputation: 968

Even I faced similar issue due to AppTheme. I was using Theme.MaterialComponents.NoActionBar as default theme for App and only for toolbar the ripple effect was not working. However I solved it using app:theme.

Please try adding a app:theme="@style/Theme.AppCompat.Light.NoActionBar" to your toolbar. This is test with androidx.appcompat.widget.Toolbar it is working for me.

<androidx.appcompat.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@android:color/white"
    app:layout_collapseMode="pin"
    android:elevation="4dp"
    app:theme="@style/Theme.AppCompat.Light.NoActionBar"
    android:stateListAnimator="@animator/appbar_elevation"
    tools:targetApi="lollipop">

Now all you have to do is add android:background="?attr/selectableItemBackground" to your view to get that ripple/touch effect.

<ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="?attr/selectableItemBackground"
            android:contentDescription="@null"
            android:src="@mipmap/ic_launcher" />

Upvotes: 1

David Wallace
David Wallace

Reputation: 31

I believe I had a similar issue when changing the background colour of certain rows in my application. I figured out that the Ripple Effect on selecting an item was drawing below the background due to changing the colour from the default.

In my ListView, I added XML attribute android:drawSelectorOnTop="true", which brought the Ripple Effect to the Foreground.

Adding this same attribute to your code may fix your issue.

Android Documentation

Upvotes: 0

Related Questions