2

CURRENT UPDATE: Still not solved!

Question: Is it possible that Dialog/AlertDialog/AlertDialogBuilder/MaterialAlaertDialogBuilder can works with combitation of rounded corners and DayNight theme at same time in Android?

I tired a lot, but I don't think that it is possible. Only one thing will work.

You can suggest me using material library.

Rounded corner with theme style is really possible in Dialog?

Code:

public void showFeedbackDialog(int code) {
        exitCode = code;
        dialog = new Dialog(context, R.style.dialogBoxStyle);

        View dialogView = LayoutInflater.from(context).inflate(R.layout.row_feedback_dialog, null);
        name = dialogView.findViewById(R.id.feedback_name);
        email = dialogView.findViewById(R.id.feedback_email);
        content = dialogView.findViewById(R.id.feedback_content);
        TextView nameError = dialogView.findViewById(R.id.nameError);
        emailError = dialogView.findViewById(R.id.emailError);
        feedbackError = dialogView.findViewById(R.id.feedbackError);
        progressBar = dialogView.findViewById(R.id.progress_bar);
        skip = dialogView.findViewById(R.id.skip);
        submit = dialogView.findViewById(R.id.button_positive);
        cancel = dialogView.findViewById(R.id.button_negative);

        dialog.setContentView(dialogView);

        if (code == 101) {
            skip.setVisibility(View.VISIBLE);
        } else if (code == 102) {
            skip.setVisibility(View.GONE);
        }
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                submit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (checkValidation()) {
                            addFeedbackData();
                        }
                    }
                });

                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                skip.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (code == 101) {
                            FeedbackDialog.this.dialog.dismiss();
                            FixedCategorySingleton.getInstance().setNullObject();
                            activity.finishAffinity();
                        }
                    }
                });
            }
        });


        dialog.setCancelable(false);
        dialog.show();
        dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

    }

row_feedback_dialog

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:focusableInTouchMode="true"
    android:background="@drawable/dialog_positive_round"
    app:cardCornerRadius="12dp"
    android:orientation="vertical">

    <include
        layout="@layout/row_progress_bar"
        android:visibility="gone" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dp">


        <TextView
            style="@style/dialog_hint_textview_style"
            android:text="Please share your Feedback"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                style="@style/hint_textview_style"
                android:text="@string/name" />
            <EditText
                android:id="@+id/feedback_name"
                style="@style/edit_text_style"
                android:singleLine="true"
                android:imeOptions="actionNext" />

        </LinearLayout>
        <include
            android:id="@+id/nameError"
            layout="@layout/row_error_textview" />

       <!-- <EditText
            android:id="@+id/feedback_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/colorIconLightGray"
            android:hint="Name (Optional)"
            android:inputType="textPersonName"
            android:textColor="?attr/textcolor" />-->



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                style="@style/hint_textview_style"
                android:text="@string/email" />
            <EditText
                android:id="@+id/feedback_email"
                style="@style/edit_text_style"
                android:singleLine="true"
                android:imeOptions="actionNext" />

        </LinearLayout>
        <include
            android:id="@+id/emailError"
            layout="@layout/row_error_textview" />

       <!-- <EditText
            android:id="@+id/feedback_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/colorIconLightGray"
            android:hint="Email (Optional)"
            android:inputType="textEmailAddress"
            android:textColor="?attr/textcolor" />-->


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                style="@style/hint_textview_style"
                android:text="What would you like to tell us?" />
            <EditText
                android:id="@+id/feedback_content"
                style="@style/edit_text_style"
                android:maxLines="4"
                android:imeOptions="actionDone" />
        </LinearLayout>


       <!-- <EditText
            android:id="@+id/feedback_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/colorIconLightGray"
            android:gravity="top"
            android:hint="What would you like to tell us?"
            android:imeOptions="actionDone"
            android:inputType="textMultiLine"
            android:lines="5"
            android:textColor="?attr/textcolor" />-->

        <include
            android:id="@+id/feedbackError"
            layout="@layout/row_error_textview" />

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?attr/backgroundcolor">


            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <Button
                        android:id="@+id/skip"
                        android:layout_width="70dp"
                        android:layout_height="40dp"
                        android:background="?attr/backgroundcolor"
                        android:text="@string/skip_caps"
                        android:textColor="?attr/textcolor"
                        android:visibility="gone" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:orientation="horizontal">

                    <Button
                        android:id="@+id/button_negative"
                        android:layout_width="70dp"
                        android:layout_height="40dp"
                        android:background="?attr/backgroundcolor"
                        android:text="@string/cancel"
                        android:textColor="?attr/textcolor" />

                    <Button
                        android:id="@+id/button_positive"
                        android:layout_width="70dp"
                        android:layout_height="40dp"
                        android:background="?attr/backgroundcolor"
                        android:text="@string/submit"
                        android:textColor="?attr/textcolor" />
                </LinearLayout>


            </RelativeLayout>
        </RelativeLayout>
    </LinearLayout>

</androidx.cardview.widget.CardView>

style

  <style name="dialogBoxStyle" parent="Theme.AppCompat.DayNight.Dialog.Alert">
        <item name="android:background">?attr/backgroundcolor</item>
        <item name="android:textColor">?attr/textcolor</item>
        <item name="android:textColorAlertDialogListItem">?attr/textcolor</item>
        <item name="android:textColorSecondary">?attr/textcolor</item>
        <item name="android:textColorPrimary">?attr/textcolor</item>
        <item name="colorAccent">?attr/textcolor</item>
        <item name="android:typeface">normal</item>
        <item name="textColorAlertDialogListItem">?attr/textcolor</item>
    </style>

If I don't use theme then everything will work fine. corner are coming in rounded shape because of getWindow line. But, in my app I'm using theme also.

I request you to help me, I finally created this account because of I need solution.

UPDATE:

tried with material library, not giving any effect

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Priyanka Singh
  • 193
  • 1
  • 9

1 Answers1

0

You can use the MaterialAlertDialogBuilder included in the Material Components library.
Use the method .setView(R.layout.dialog_layout) to inflate your custom layout.

Something like:

 new MaterialAlertDialogBuilder(MainActivity.this,
            R.style.MyThemeOverlay_MaterialComponents_MaterialAlertDialog)
              .setTitle("Dialog")
              .setView(R.layout.dialog_layout)
              .setPositiveButton("Ok", null)
              .setNegativeButton("Skip", null)
              .show();

Then use the shapeAppearanceOverlay to define your shape and apply rounded corners (it requires v.1.1.0).

 <!-- Alert Dialog -->
  <style name="MyThemeOverlay.MaterialComponents.MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MyApp.Dialog.Rounded</item>
    <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
    ...
  </style>


  <style name="PositiveButtonStyle" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">#FFFFFF</item>
    <item name="backgroundTint">#00f</item>
  </style>

  <!-- Rounded corners -->
  <style name="ShapeAppearanceOverlay.MyApp.Dialog.Rounded" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">16dp</item>
  </style>

enter image description here

Also you can use the attribute materialThemeOverlay to override the default style/color defined in your app theme, without changing it for all components.

Something like:

<style name="MyThemeOverlay.MaterialComponents.MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="materialThemeOverlay">@style/MyAlThemeOverlay</item>
    ...
</style>

  <style name="MyAlThemeOverlay" parent="ThemeOverlay.MaterialComponents.TextInputEditText.FilledBox">
    <item name="colorPrimary">@color/....</item>
  </style>

Also the Material Components library supports the DayNight theme.
Your app theme has just to inherit from Theme.MaterialComponents.DayNight.

Something like:

<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
    <!-- ... -->
</style>

or:

res/values/themes.xml:

<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light">
    <!-- ...  -->
</style>

res/values-night/themes.xml:

<style name="Theme.MyApp" parent="Theme.MaterialComponents">
    <!-- ... -->
</style>

Without changing your code, the AlertDialog inherits from the app theme.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • will it work for day night theme without using manually if/else condition and no need to create separate styles for day n night? – Priyanka Singh Sep 20 '19 at 07:39
  • @PriyankaSingh Yes. The Material Components library supports [DayNight theme](https://github.com/material-components/material-components-android/blob/master/docs/theming/Dark.md). – Gabriele Mariotti Sep 20 '19 at 07:42
  • @PriyankaSingh Just tried. AlertDialog has a full support for DayNight theme without any changes in the code. Answer updated. – Gabriele Mariotti Sep 20 '19 at 09:17
  • First of all you need to use a [Material Theme in your App theme](https://github.com/material-components/material-components-android/blob/master/docs/getting-started.md#4-change-your-app-theme-to-inherit-from-a-material-components-theme) (not AppCompat). Then check the constructor of MaterialAlertDialog (you have to pass the style if you don't want to change globally) and then check the shapeAppearanceOverlay attribute (use the latest 1.1.0-alpha10). – Gabriele Mariotti Sep 20 '19 at 09:30
  • Please clear me: Should I use MaterialAlertDialogBuilder with alert dialog because I want to show three button with set on showListener. Also theme and rounded corners both will work? if yes, please change your answer according to my question, because, just now I tried and replease my code with MaterialAlertDialogBuilder, theme and round corner both are not giving me effect. Please help and let me accept your answer. – Priyanka Singh Sep 20 '19 at 09:31
  • I tried accroding to your last comment and copied your code too. please check my above comment – Priyanka Singh Sep 20 '19 at 09:32
  • @PriyankaSingh The `MaterialAlertDialogBuilder` helps you to build an `AlertDialog` with a Material Theme and features like shaping. And yes, it works with app theme and you can apply the rounded corners. (For example try an empty app with a very simple Dialog like this [answer](https://stackoverflow.com/questions/12501488/android-alertdialog-with-rounded-corners/57843956#57843956)]. And the google library supports DayNight theme by default. – Gabriele Mariotti Sep 20 '19 at 09:37