Thursday, November 8, 2018

Android : How to show HTML in AlertDialog?

Below code will help for it.

public static void showAlertMessageWithHTML(Context context, String title, String message, String positiveAction, String negativeAction, String neutralAction, boolean cancellable, final AlertActionListener actionListener)
    {
        TextView msg = new TextView(context);
        msg.setText(Html.fromHtml(message));
        msg.setPadding(30,20,30,10);
       

        final SpannableString s = new SpannableString(message);
        Linkify.addLinks(s, Linkify.ALL);
        AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
        builder1.setTitle(title);

        builder1.setView(msg);
        builder1.setCancelable(cancellable);
        if(positiveAction != null)
        {
            builder1.setPositiveButton(positiveAction, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if(actionListener != null)
                    {
                        actionListener.onPositiveAction();
                    }
                }
            });
        }
        if(negativeAction != null)
        {
            builder1.setNegativeButton(negativeAction, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if(actionListener != null) {
                        actionListener.onNegativeAction();
                    }
                }
            });
        }
        builder1.setNeutralButton(neutralAction, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if(actionListener != null)
                {
                    actionListener.onNeutralAction();
                }
            }
        });

        AlertDialog alert11 = builder1.create();
        alert11.show();
    }

No comments:

Post a Comment