Raheel Hasan
Raheel Hasan

Reputation: 6013

Android Alert Dialog - How to change the title color and the text color without changing the primary color

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

Upvotes: 4

Views: 6276

Answers (4)

Sarith Nob
Sarith Nob

Reputation: 347

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.

Upvotes: 1

alx
alx

Reputation: 352

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.

Upvotes: 1

Komal12
Komal12

Reputation: 3348

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();

Upvotes: 0

Ben P.
Ben P.

Reputation: 54194

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.

Upvotes: 8

Related Questions