0

I am trying to build a custom AlertDialogwith the default theme so that I can use the same AlertDialogat multiple places inside the code. I have created a class by extending AlertDialog and writing my custom business logic to decide the message in the dialog. The problem is that, I see a black overlay without the dialog been shown when I call AlertDialog.show(). Here is my code:

public class ClosestShrutiDialog extends AlertDialog {

    private String mLessonId;
    private Activity mActivity;
    private View mParentView;

    private OnClickedListener mOnClickListerner;

    public ClosestShrutiDialog(@NonNull Activity activity, final String lessonId, final View parentView) {
        super(activity);
        this.mLessonId = lessonId;
        this.mActivity = activity;
        this.mParentView = parentView;
    }

    public void setmOnClickListerner(OnClickedListener mOnClickListerner) {
        this.mOnClickListerner = mOnClickListerner;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        // My business login to decide the msg related params ....

        // showing message to user to select the alternate shruti ...
        final String msg = mActivity.getResources().getString(R.string.user_alternate_shruti_msg,
                userShrutiLabel,
                closestShruti.getLabel());

        this.setMessage(msg);
        this.setButton(android.support.v7.app.AlertDialog.BUTTON_POSITIVE,
                mActivity.getResources().getString(R.string.yes),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        if(mOnClickListerner != null) {
                            mOnClickListerner.proceedWithChange(closestShruti);
                        }
                    }
                });
        this.setButton(android.support.v7.app.AlertDialog.BUTTON_NEGATIVE,
                mActivity.getResources().getString(R.string.cancel),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
    }

    public interface OnClickedListener {
        void proceedWithChange(final Shruti closestShruti);
    }
}

I am not sure why is it unable to pickup the default template and show it. When I used AlerDialog.Builder(Context), without creating the custom class, I was able to see the dialog properly. Any suggestions?

Swapnil
  • 1,870
  • 2
  • 23
  • 48

3 Answers3

0

Pretty Very Old question :-)

Custom Alert Dialog - How to create a Custom Dialog box in android?

Custom Dialog theme - How to change theme for AlertDialog

Community
  • 1
  • 1
Keerthivasan
  • 1,651
  • 1
  • 21
  • 47
  • I have gone through all the links that you have posted earlier. The problem I am facing is that I want to show the default dialog popup for the AlertDialog, I don't want to customise it. – Swapnil May 12 '17 at 09:10
0
        **--java file code----**
    AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle);
    builder.setTitle("AppCompatDialog");
    builder.setMessage("Lorem ipsum dolor...");
    builder.setPositiveButton("OK", null);
    builder.setNegativeButton("Cancel", null);
    builder.show();


**----and you can put into xml file
styles.xml - Custom style----**

<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <!-- Used for the buttons -->
    <item name="colorAccent">#FFC107</item>
    <!-- Used for the title and text -->
    <item name="android:textColorPrimary">#FFFFFF</item>
    <!-- Used for the background -->
    <item name="android:background">#4CAF50</item>
</style>

**---and also you can add new style----**
<style name="MyTitleTextStyle">
    <item name="android:textColor">#FFEB3B</item>
    <item name="android:textAppearance">@style/TextAppearance.AppCompat.Title</item>
</style> 
**---and alert dialong style---**
<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
...
...
    <item name="android:windowTitleStyle">@style/MyTitleTextStyle</item>
</style>
Paras Nakum
  • 238
  • 2
  • 8
0

I found a solution for this. Basically, I should not override onCreate() method for AlertDialog. If I do that, then it is customary to provide a layout for rendering. Instead, Whatever I did inside in onCreate() method in the above code, I moved it inside a different function and then called it from the construction of the class and that worked for me. The code is something like:

public class ClosestShrutiDialog extends AlertDialog {

    private String mLessonId;
    private Activity mActivity;
    private View mParentView;

    private OnClickedListener mOnClickListerner;

    public ClosestShrutiDialog(@NonNull Activity activity, final String lessonId, final View parentView) {
        super(activity);
        this.mLessonId = lessonId;
        this.mActivity = activity;
        this.mParentView = parentView;

        setupView();
    }

    public void setmOnClickListerner(OnClickedListener mOnClickListerner) {
        this.mOnClickListerner = mOnClickListerner;
    }


    private void setupView() {

        // My business login to decide the msg related params ....

        // showing message to user to select the alternate shruti ...
        final String msg = mActivity.getResources().getString(R.string.user_alternate_shruti_msg,
                userShrutiLabel,
                closestShruti.getLabel());

        this.setMessage(msg);
        this.setButton(android.support.v7.app.AlertDialog.BUTTON_POSITIVE,
                mActivity.getResources().getString(R.string.yes),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        if(mOnClickListerner != null) {
                            mOnClickListerner.proceedWithChange(closestShruti);
                        }
                    }
                });
        this.setButton(android.support.v7.app.AlertDialog.BUTTON_NEGATIVE,
                mActivity.getResources().getString(R.string.cancel),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
    }

    public interface OnClickedListener {
        void proceedWithChange(final Shruti closestShruti);
    }
}
Swapnil
  • 1,870
  • 2
  • 23
  • 48