简体   繁体   中英

change title color of alertDialog on below android 5.0

I had created an AlertDialog and wanted to change the title color of AlertDialog but getting failed in every attempt. It works very fine on Android 5.0 and above with title color as black but when it runs below Android 5.0, it's title color get's changed to white, i had used style and many other sources from internet but failed, my code is as follows,

AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setMessage(msg)
            .setCancelable(true)
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                }
            });

    final AppCompatDialog dialog = builder.create();
    dialog.setTitle("VALIDATION_TITLE error");
    dialog.setCancelable(false);
    dialog.show();

My issue get's solved by following line,

dialog.setTitle( Html.fromHtml("<font color='#FF7F27'>Set IP Address</font>"))

but i don't want to use this, anyhelp would be greatly appreciated. You can see the screenshot of my dialog from this chat link

Use custom theme to customize alert dialog

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle);
builder.setTitle("My Dialog");
builder.setMessage(msg)
                .setCancelable(true)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                });
       final AppCompatDialog dialog = builder.create();
       dialog.show();

styles.xml - Custom style

<style name="MyAlertDialogMaterialStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <!-- Used for the buttons -->
        <item name="colorAccent">@color/md_teal_900</item>
        <!-- Used for the title and text -->
        <item name="android:textColorPrimary">@color/white</item>
        <!-- Used for the background -->
        <item name="android:background">@color/color_primary_dark</item>
    </style>

First type

AlertDialog.Builder builder = new AlertDialog.Builder(this,

then type

android.R.style.Theme_DeviceDefault_Dialo

then Crtl + Space then you will get some suggestion. You can select like this

AlertDialog.Builder builder = new AlertDialog.Builder(this, 
  android.R.style.Theme_DeviceDefault_Dialog);

or

AlertDialog.Builder builder = new AlertDialog.Builder(this, 
  android.R.style.Theme_DeviceDefault_Dialog_MinWidth);

or

AlertDialog.Builder builder = new AlertDialog.Builder(this, 
  android.R.style.Theme_DeviceDefault_Dialog_NoActionBar);

or

AlertDialog.Builder builder = new AlertDialog.Builder(this, 
  android.R.style.Theme_DeviceDefault_Dialog_NoActionBar_MinWidth);

or

AlertDialog.Builder builder = new AlertDialog.Builder(this, 
  android.R.style.Theme_DeviceDefault_DialogWhenLarge);

etc...

Create custom view and set in dialoge using

dialog.setContentView(YOUR CUSTOM VIEW HERE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM