2

I'm trying to use a custom layout file for an alert dialog on android (support library version 26.1.0)

I've tried using the following:

<style name="MyTheme"...>
  <item name="alertDialogTheme">@style/MyTheme.AlertDialog</item>
</styles>

<style name="MyTheme.AlertDialog" parent="Base.AlertDialog.AppCompat">
    <item name="android:layout">@layout/custom_abc_alert_dialog_material</item>
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">fill_parent</item>
    <item name="android:windowBackground">@color/white</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
    <item name="buttonBarNegativeButtonStyle">@style/MyTheme.Button.Negative</item>
    <item name="buttonBarNeutralButtonStyle">@style/MyTheme.Button.Neutral</item>
    <item name="buttonBarPositiveButtonStyle">@style/MyTheme.Button.Positive</item>
</style>

I can tell it's picking up the actual theme, as the button styles are updated. However, it's not picking up the custom layout file. I see this code in AlertController in the support library:

    mAlertDialogLayout = a.getResourceId(R.styleable.AlertDialog_android_layout, 0);

which translates back to the source:

<declare-styleable name="AlertDialog">
  <attr name="android:layout"/>
  ...
</declare-styleable>

I also see this in the support library values.xml file:

<style name="Base.AlertDialog.AppCompat" parent="android:Widget">
    <item name="android:layout">@layout/abc_alert_dialog_material</item>

I suspect my usage of android:layout is not being picked up due to the above styleable declaration. I just have no idea how to overcome that and properly specify the layout.

I realize I could use a custom view and use setView on the dialog, but that's intrusive when all I want to do is re-arrange a few things on the layout (which can't be controlled with theme attributes sadly enough)

I also realize I could just replace abc_alert_dialog_material.xml entirely, but then it's replaced for all themes, not just the one I'm working with.

Does anyone have any idea what I might be missing here to properly use my custom layout file? ?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Matt
  • 11,523
  • 2
  • 23
  • 33
  • Possible duplicate: https://stackoverflow.com/questions/28484621/custom-alert-dialog-android – DigitalNinja Dec 13 '17 at 17:35
  • This is not a duplicate. I want an alert dialog, not a DialogFragment. The AlertDialog class is a Dialog subclass, not a fragment. DialogFragment has a bunch of baggage that i really don't want to deal with. Plus I want exactly the functionality that AlertDialog has, just slightly altered visuals. – Matt Dec 13 '17 at 18:50
  • The `DialogFragment` part doesn't matter. The answer still shows how to use a `style` with a custom dialog layout. – DigitalNinja Dec 13 '17 at 18:56
  • If that particular answer doesn't do it for you there are probably about 20 other questions related to the custom alert dialogs. It looks like you're trying to combine the style with the layout, they should be separate. There's no need to mess with the support library files. – DigitalNinja Dec 13 '17 at 19:06
  • I understand how to set the style, and as I mentioned, it is set appropriately, otherwise my button styles would not have changed. However, what's not working is changing the layout via the style, which in theory should work, provided i can find the right attribute to put in there so that this statement gives me back my layout: a.getResourceId(R.styleable.AlertDialog_android_layout, 0) – Matt Dec 13 '17 at 20:57
  • And based on the xml provided in the support library android:layout should be the right attribute. This is the crux of my question. Maybe that only works because it's within the support library itself, I don't know. That's why i'm asking. – Matt Dec 13 '17 at 20:58
  • any solution? where this custom styleable available? – Star Feb 02 '21 at 08:47

0 Answers0