Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using MaterialAlertDialogBuilder with a material theme

Tags:

android

I have set the android application to use the material theme

<style name="AppTheme" parent="AppTheme.Base"></style>

<style name="AppTheme.Base" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
        <item name="android:textColorSecondary">@android:color/white</item>
        <item name="android:textColorHint">@android:color/darker_gray</item>
        <item name="searchViewStyle">@style/SearchViewStyle</item>
</style>

In my activity/fragment, I am trying to invoke the material alert dialog

new MaterialAlertDialogBuilder(mAppContext)
                            .setTitle("Title")
                            .setMessage("Message")
                            .setPositiveButton("Ok", null)
                            .show();

At runtime this is throwing the following IllegalArgumentException exception

java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.AppCompat (or a descendant).
        at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:240)
        at com.google.android.material.internal.ThemeEnforcement.checkAppCompatTheme(ThemeEnforcement.java:211)
        at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:146)
        at com.google.android.material.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:78)
        at com.google.android.material.dialog.MaterialDialogs.getDialogBackgroundInsets(MaterialDialogs.java:55)
        at com.google.android.material.dialog.MaterialAlertDialogBuilder.<init>(MaterialAlertDialogBuilder.java:116)
        at com.google.android.material.dialog.MaterialAlertDialogBuilder.<init>(MaterialAlertDialogBuilder.java:102)
        at com.syl.app.fragments.AlarmNotificationDetailFragment$2.onClick(UserFragment.java:265)
        at android.view.View.performClick(View.java:5207)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:941)
        at android.view.View$PerformClick.run(View.java:21177)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5441)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)

Isn't this contradictory? A material component requires AppCompat theme whereas the application uses a material theme.

The android documentation also says the same

MaterialAlertDialogBuilder requires that your application use a Material Components theme (e.g., Theme.MaterialComponents.Light). Using a Material Components theme with MaterialAlertDialogBuilder will result in an AlertDialog that matches your appplication’s color, typography, and shape theming.

How do I get the alert dialog to work in this case where I am using a material theme?

like image 919
Avi Avatar asked Jul 18 '19 18:07

Avi


1 Answers

below is working for me

style.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColorSecondary">@android:color/white</item>
        <item name="android:textColorHint">@android:color/darker_gray</item>
        <item name="drawerArrowStyle">@android:style/DeviceDefault.ButtonBar</item>
        <item name="searchViewStyle">@android:style/Widget.Material.SearchView</item>
    </style>


</resources>

Androidmanifest.xml

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

Make usre you do not have multiple style.xml file for diff apis.

like image 162
Hardik Bambhania Avatar answered Sep 28 '22 19:09

Hardik Bambhania