Sérgio Carneiro
Sérgio Carneiro

Reputation: 3966

Change text color of Dialog's title

This seems to be a very simple problem but I have searched and I can't find any solution. I have a Dialog with content, like this one:

enter image description here

And I want to change the color of "Enter Password", the Dialog's title. How can I do it?

Upvotes: 0

Views: 6205

Answers (2)

ekosman
ekosman

Reputation: 338

There seems to be a very simple answer:

String chars = getResources().getString(R.string.playerDialogTitle);
    SpannableString str = new SpannableString(chars);
    str.setSpan(new ForegroundColorSpan(Color.WHITE), 0, chars.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

    AlertDialog.Builder builder = new AlertDialog.Builder(PlayerActivity.this);
    builder.setTitle(str)

Upvotes: 6

Sérgio Carneiro
Sérgio Carneiro

Reputation: 3966

The method setCustomTitle(View customTitleView) allows you to do that. You need to create a layout to set in this, and in that layout simply set to the TextView the style you want.

This method belongs to AlertDialog.Builder:

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customTitleView = inflater.inflate(R.layout.custom_title_view, null);

AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setCustomTitle(customTitleView);

Upvotes: 2

Related Questions