I'm changing my app's theme from AppCompat
to MaterialComponents
and everything except for the alert dialogues is working. I've already set the app theme to MaterialComponents
both in styles and in the manifest.
styles
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">@color/grey</item>
<item name="colorPrimaryDark">@color/black</item>
<item name="colorAccent">@color/red</item>
<item name="colorSurface">@color/white</item>
<item name="colorOnSecondary">@color/white</item>
<item name="colorSecondary">@color/red</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar"/>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.MaterialComponents.Light"/>
build.gradle
implementation 'com.google.android.material:material:1.1.0'
AndroidManifest
<application
android:allowBackup="true"
android:fullBackupContent="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
This is the code I'm trying to display my dialogue with:
MaterialAlertDialogBuilder(context)
.setTitle(R.string.tip_title)
.setMessage(R.string.tip)
.setNeutralButton(R.string.ok, null)
.setPositiveButton(R.string.do_not_show_again) { _, _ ->
preferencesHelper.disableTip()
}.show()
And finally, the error I'm getting is
java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.AppCompat (or a descendant).
How can I fix this error?
The function that displayed the dialogue was taking Context
as its input parameter, which in turn was the application context injected to the class where the function is.
I managed to fix the issue by changing the input parameter type in the function to Activity
, which is a way to enforce the right theme.