0

I've been trying to create a customized AlertDialog and I know that I can use AlertDialog.Builder.setView() to put a custom view inside the dialog, but is it possible to completely replace the default layout?

EDIT: I want to do this because it would allow me to use the builder's setMessage(), setTitle() etc. with a custom layout

Colander
  • 105
  • 1
  • 9
  • Possible duplicate of [AlertDialog.Builder with custom layout and EditText; cannot access view](https://stackoverflow.com/questions/22655599/alertdialog-builder-with-custom-layout-and-edittext-cannot-access-view) – Mr. Roshan Jun 08 '18 at 11:13
  • 1
    You can always fall back to Dialog, and then you can control everything. – Gabe Sechan Jun 08 '18 at 11:13

2 Answers2

4

it is possible to Customize or completely change Dialog or AlertDialog ,You can Customize Dialog as like this

private void customDialog() {
        final Dialog dialog = new Dialog(ActivityUserVideoPlay.this, R.style.MaterialDialogSheet);
        dialog.setContentView(R.layout.your_layout_foor_dialog); // your custom view.
        dialog.setCancelable(false);
        dialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        dialog.show();
    }

And this is styles for dialog

 <style name="MaterialDialogSheet" parent="@android:style/Theme.Dialog">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:windowAnimationStyle">@style/MaterialDialogSheetAnimation</item>
    </style>

Use animation to open or close your dialog as you want otherwise you can remove it.

Hope it helps.

Sandeep Parish
  • 1,888
  • 2
  • 9
  • 20
  • this does not answer the question directly, but creating a custom Dialog instead of hacking the AlertDialog makes sense – Colander Jun 08 '18 at 11:34
  • it's not any Alert Dialog Hack it also works as AlertDialog you can use any of dialog with same properties – Sandeep Parish Jun 08 '18 at 11:44
-1

Yes, you can, you just have to create the .xml and inflate it. Just like that:

final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // Get the layout inflater
        LayoutInflater inflater = this.getLayoutInflater();
        View alertView = inflater.inflate(R.layout.newsletter_dialog, null);

        // Pass null as the parent view because its going in the dialog layout
        builder.setView(alertView);
        final AlertDialog dialog = builder.show();
  • this is exacly what I described I didn't want to do - this places the view inside the AlertDialog's layout – Colander Jun 08 '18 at 11:14