Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – How do I display a MaterialAlertDialog?

Posted on November 11, 2022 By

Solution 1 :

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.

Problem :

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?

Comments

Comment posted by Gabriele Mariotti

Which version of material components are you using?

Comment posted by 92AlanC

1.1.0. Just included it in the question

READ  [FIXED] android - java.lang.RuntimeException: Cannot create an instance of class com.example.homeactivity.activities.editprofile.EditProfileViewModel
Powered by Inline Related Posts
Android Tags:android, android-alertdialog, material-components, themes

Post navigation

Previous Post: [FIXED] Xamarin.forms – Trouble behaviour between ListView & keyboard
Next Post: [FIXED] android – two elements in linear layout are packed to center instead of to the sides

Related Posts

[FIXED] java – Iterating over R.string.name Android
[FIXED] java – constructor in class cannot be applied to given types android studio Android
[FIXED] java – TextView not updating after creating new Activity Android
[FIXED] Android Room Database, retrieve specific value of the latest record entered Android
[FIXED] android – java.lang.NoSuchMethodError: No virtual method setUpdateMode while trying to render a Sceneform object Android
[FIXED] android – Checkbox listview scrolling issue Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is the rising action in Julius Caesar?
  • How do you secure a rope to itself?
  • Does waterproof laminate scratch easily?
  • What makes a building prewar?
  • What can you learn in a month without alcohol?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme