4

Hay,

Is there a simple way to change the title and the text colour without changing the primary colour of the whole app?

Here is what I got now:

enter image description here

I dont want to change the textColorPrimary

Raheel Hasan
  • 5,753
  • 4
  • 39
  • 70

4 Answers4

7

First, create a style definition like this:

<style name="DialogTheme" parent="ThemeOverlay.AppCompat.Dialog">
    <item name="android:textColorPrimary">your color here</item>
</style>

Then, when you create the dialog, instead of using the AlertDialog.Builder(Context) constructor, use the AlertDialog.Builder(Context, int) method and pass a reference to your style:

new AlertDialog.Builder(this, R.style.DialogTheme)
        .setTitle("Hello world")
        .setMessage("some longer text for the body of the dialog")
        .show();

enter image description here

Even though this depends on changing textColorPrimary, it doesn't do so in a way that affects anything else in your app.

Ben P.
  • 52,661
  • 6
  • 95
  • 123
1

Simple and easy to implement:

SpannableString title = new SpannableString("Your title");
                title.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.your_color)), 0, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

then just pass as title in

new AlertDialogBuilder(context).setTitle(title).show();

Do the same with the message.

alx
  • 352
  • 1
  • 8
1

Simply, you just change your "TITLE" text with this code:

Html.fromHtml("<font color='#FFFFFF'>TITLE</font>")

This code will totally change your text color depend on your added color value. You can also use it for button's text as well.

Sarith Nob
  • 337
  • 2
  • 13
0

Try this,

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(Html.fromHtml("<font color='#0288D1'>This is a test</font>"));
builder.setPositiveButton(Html.fromHtml("<font color='#0288D1'>Yes</font>"), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int arg1) {
        Log.e(LOG_TAG, "Yes");
    }
});
builder.setNegativeButton(Html.fromHtml("<font color='#0288D1'>No</font>"), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int arg1) {
        Log.e(LOG_TAG, "No");
    }
});
builder.create();
builder.show();
Komal12
  • 3,340
  • 4
  • 16
  • 25