26

I have an AlertDialog and it's message is displayed, but the color of the text is white. It blends in with the background. I've tried changing the theme but it doesn't work. How do I change the color of the message?

The relevant code:

AlertDialog.Builder builder;
builder = new AlertDialog.Builder(MainActivityGame.this);

builder.setTitle("Name");
builder.setMessage("Are you ");
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        //Submit default name, go home
        boolean isInserted = myDb.insertData(defaultName, triesTaken, difficultyText);
        if (isInserted) {
            Toast.makeText(MainActivityGame.this, "Your name was submitted", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivityGame.this, "Error, your name wasn't submitted\n Have you entered a default name?\n Go to Settings/Default Name to set it up", Toast.LENGTH_SHORT).show();
        }
        Intent intent = new Intent(MainActivityGame.this, MainActivity.class);
        startActivity(intent);
    }
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        userName.setVisibility(View.VISIBLE);
        submitName.setVisibility(View.VISIBLE);
        submitName.setEnabled(true);
        dialogInterface.dismiss();
    }
});
builder.create();
builder.show();
aydinugur
  • 1,208
  • 2
  • 14
  • 21
an3
  • 568
  • 1
  • 6
  • 14
  • 1
    Did you try to research for an answer? https://stackoverflow.com/questions/16200914/alertdialog-styling-how-to-change-style-color-of-title-message-etc https://stackoverflow.com/questions/38609262/change-text-color-of-alert-dialog – Karim ElGhandour Jun 18 '17 at 19:13

4 Answers4

63

you can give style to your alert dialog like this:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialogStyle);

and the style is like always:

<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:colorAccent">#f3f3f3</item>
    <item name="android:textColor">#f3f3f3</item>
    <item name="android:textColorPrimary">#f3f3f3</item>
</style>
Meikiem
  • 1,876
  • 2
  • 11
  • 19
  • You can apply the above style to all alert dialogs in themes.xml as follows @style/AlertDialogStyle – fbiego Mar 11 '23 at 13:14
1

You can either use this method, like @KarimElGhandour mentioned or just create your custom Layout in the res\layout folder and then apply it with alertDialog.setView(LayoutInflater.inflate(R.layout.yourlayout), yourRootView.

creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
  • Thank you, I created my own theme and added it to the initialization of the AlertDialog. – an3 Jun 18 '17 at 19:39
0

In the Style even you can change gravity and Mode:

<style name="WelcomeStyle" parent="Theme.AppCompat.Light.Dialog">
        <item name="android:background">#FFD600</item>
        <item name="android:textColor">#C51162</item>
        <item name="android:textColorPrimary">#00B8D4</item>
        <item name="android:gravity">center</item>
        <item name="android:justificationMode">inter_word</item>
    </style>
Mori
  • 2,653
  • 18
  • 24
0
  AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.DialogStyle);

styles.xml

  <style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:colorAccent">#303030</item>
    <item name="android:textColor">#FFFFFF</item>
    <item name="android:textColorPrimary">#494949</item>
    <item name="android:windowBackground">#201D1D</item>
</style>
younes
  • 742
  • 7
  • 8