paxcow
paxcow

Reputation: 1941

Creating a MaterialButton programatically, theme error

Trying to create a MaterialButton programatically:

private val button = MaterialButton(context, null, R.style.Widget_MaterialComponents_Button)

But I am getting the following error when it tries to render:

Failed to find '@attr/textAppearanceButton' in current theme.

I tried to pass in a context that uses my App theme, which is extending from.

"Theme.AppCompat.Light.NoActionBar"

Tried multiple styles while initialising, no change in the result.

No Luck, any ideas what might i be doing wrong?

Upvotes: 0

Views: 178

Answers (1)

Kevin Coppock
Kevin Coppock

Reputation: 134704

You'll need to update your theme to inherit from one of the MaterialComponents themes, like:

Theme.MaterialComponents.Light.NoActionBar

See https://material.io/develop/android/docs/getting-started

Note that you should be able to just initialize via the single-argument constructor:

private val button = MaterialButton(context)

Provided that the Context you're using is themed (either the Activity, or the Application if you've set the theme on the <application> tag).

Upvotes: 1

Related Questions