kstar
kstar

Reputation: 11

How to set colorControlHighlight (ripple color) for all the icons inside toolbar programmatically?

I know there is a way to set theme to toolbar in xml like this:

<androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:theme="@style/ToolBarTheme"/>

and in the styles:

<style name="ToolBarTheme">
            <item name="android:colorControlHighlight">@color/white</item>
        </style>

But is there a way to set it programmatically or using main app theme specifically for toolbar, not just override colorControlHighlight in the main theme(because it will affect other Views as well)?

Upvotes: 0

Views: 318

Answers (1)

kstar
kstar

Reputation: 11

Found a solution that works for me. I have a CustomToolbar class that extends Toolbar class. And in constructor I am using ContextThemeWrapper with my toolbar theme:

class CustomToolBar : Toolbar {

    constructor(context: Context?) : super(ContextThemeWrapper(context, R.style.ToolBarTheme))

    constructor(context: Context?, attrs: AttributeSet?) : super(ContextThemeWrapper(context, R.style.ToolBarTheme), attrs) 

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(ContextThemeWrapper(context, R.style.ToolBarTheme), attrs, defStyleAttr)

}

Upvotes: 1

Related Questions