user4489210
user4489210

Reputation:

How to customize android AlertDialog text properties

I have an alert dialog like this enter image description here

I am using default alert dialog. I need to set size and color of Title , Content and Buttons. Right now I am using following code

AlertDialog.Builder alertDialogBuilder =new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.AlertDialogCustom));

And I've created one style resource for this also

    <style name="AlertDialogCustom" parent="@android:style/Theme.Dialog">
    <item name="android:textColor">@color/medium_black</item>
    <item name="android:textSize">20sp</item>
    </style>

I could change the color and size of Title with

<item name="android:textColor">@color/medium_black</item>
<item name="android:textSize">20sp</item>

But it is applying for Yes/No buttons also. I need separate font color and size for all these 3. Is it possible?

Upvotes: 2

Views: 2253

Answers (5)

Aditya Naique
Aditya Naique

Reputation: 1158

Just add the colorAccent property. It gets applied to buttons.

S'more properties for future explorers:
You can make a default style like this...

<style name="CustomDialogAlert" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">@color/accent_pink_a200</item> <!-- gets applied to positive/neutral/negative buttons -->
    <item name="android:textColor">@color/white</item>
    <item name="android:textColorPrimary">@color/white</item>
    <item name="android:textColorSecondary">@color/white</item> <!-- gets applied to the message text-->
    <item name="android:background">@color/primary_deepPurple_500</item>
    <item name="textColorAlertDialogListItem">@color/white</item> <!-- if the alert's got a list instead of a message -->
</style>

And include in your base app theme like this..

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    ...
    <item name="alertDialogTheme">@style/CustomDialogAlert</item>
</style>

Upvotes: 1

Lakshan
Lakshan

Reputation: 208

Use html tags for style your texts.

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

            // set title
            alertDialogBuilder.setTitle( Html.fromHtml("<font size='3' color='#FF7F27'>Set IP Address</font>"));

            // set dialog message
            alertDialogBuilder
                    .setMessage(Html.fromHtml("<font size='3' color='red'>Click yes to exit!</font>"))
                    .setCancelable(false)
                    .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                            // if this button is clicked, close
                            // current activity

                        }
                    })
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // if this button is clicked, just close
                            // the dialog box and do nothing
                            dialog.cancel();
                        }
                    });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();

Upvotes: 1

subhash
subhash

Reputation: 2707

If you don't want to create custom dialog:

After creating alert dialog and just before showing it, you can do something like:

  final AlertDialog alertDialog = builder.create();
    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            Button yesButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
            Button noButton = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);

            yesButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
            yesButton.setTypeface(/* "your type face" */);

        }
    });
    alertDialog.show();

Upvotes: 1

Akash Singh
Akash Singh

Reputation: 751

You can use this library for customising Dialogs.

Material Dialogs

Upvotes: 0

Jarvis
Jarvis

Reputation: 503

You can define your own custom dialog box and design accordingly. Please refer to this http://www.mkyong.com/android/android-custom-dialog-example/

Upvotes: 0

Related Questions