How to Change Textcolor in Alertdialog

Change text color of alert dialog

You have to provide a custom style id in the AlertDialog constructor:

AlertDialog.Builder(Context context, int themeResId)

and the style file should be something like:

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:colorAccent">#0000FF</item>
</style>

How to change textcolor in AlertDialog

For changing the font color only, try this:

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

result:

enter image description here


For changing the font color and button background color, try this:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(Html.fromHtml("<font color='#FF7F27'>This is a test</font>"));
builder.setCancelable(false);
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alert = builder.create();
alert.show();
Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
//Set negative button background
nbutton.setBackgroundColor(Color.MAGENTA);
//Set negative button text color
nbutton.setTextColor(Color.YELLOW);
Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
//Set positive button background
pbutton.setBackgroundColor(Color.YELLOW);
//Set positive button text color
pbutton.setTextColor(Color.MAGENTA);

Result:

Result:


If you want to change divider color, try this:

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Test Title");
builder.setMessage(Html.fromHtml("<font color='#FF7F27'>This is a test</font>"));
builder.setCancelable(false);
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
try {
Resources resources = dialog.getContext().getResources();
int alertTitleId = resources.getIdentifier("alertTitle", "id", "android");
TextView alertTitle = (TextView) dialog.getWindow().getDecorView().findViewById(alertTitleId);
alertTitle.setTextColor(Color.MAGENTA); // change title text color

int titleDividerId = resources.getIdentifier("titleDivider", "id", "android");
View titleDivider = dialog.getWindow().getDecorView().findViewById(titleDividerId);
titleDivider.setBackgroundColor(Color.YELLOW); // change divider color
} catch (Exception ex) {
ex.printStackTrace();
}
Button nbutton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
//Set negative button background
nbutton.setBackgroundColor(Color.MAGENTA);
//Set negative button text color
nbutton.setTextColor(Color.YELLOW);
Button pbutton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
//Set positive button background
pbutton.setBackgroundColor(Color.YELLOW);
//Set positive button text color
pbutton.setTextColor(Color.MAGENTA);

This is my sample code, but if you want to change the divider color consider the part of the code starts with "int titleDividerId".

Result:

This is the result of the code


If you want to customize the AlertDialog a lot. For example adding some checkboxes with custom background color, use this approach:

AlertDialog.Builder alert = new AlertDialog.Builder(this);
LinearLayout mainLayout = new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.VERTICAL);

LinearLayout layout1 = new LinearLayout(this);
layout1.setOrientation(LinearLayout.HORIZONTAL);
CheckBox cb1 = new CheckBox(getApplicationContext());
cb1.setText("Easy");
layout1.addView(cb1);
layout1.setBackgroundColor(Color.BLUE);
layout1.setMinimumHeight(50);

LinearLayout layout2 = new LinearLayout(this);
layout2.setOrientation(LinearLayout.HORIZONTAL);
layout2.addView(new TextView(this));
CheckBox cb2 = new CheckBox(getApplicationContext());
cb2.setText("Normal");
layout2.addView(cb2);
layout2.setBackgroundColor(Color.CYAN);
layout2.setMinimumHeight(50);

LinearLayout layout3 = new LinearLayout(this);
layout3.setOrientation(LinearLayout.HORIZONTAL);
CheckBox cb3 = new CheckBox(getApplicationContext());
cb3.setText("Hard");
layout3.addView(cb3);
layout3.setBackgroundColor(Color.GREEN);
layout3.setMinimumHeight(50);

mainLayout.addView(layout1);
mainLayout.addView(layout2);
mainLayout.addView(layout3);
alert.setTitle("Custom alert demo");
alert.setView(mainLayout);
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alert.setPositiveButton("Done", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(), "done", Toast.LENGTH_SHORT).show();
}
});

alert.show();

The result:

The result

Firstly, I created a main layout (vertical) as you see in the code. Then, for each one of the checkboxes I created a horizontal layout. In this case you can play with the colors and fonts of the elements (checkboxes, items, and etc.). I hope it helps.

Android: How can I change AlertDialog Title Text Color and Background Color without using custom layout?

You can use custom title to your alert dialog:

TextView textView = new TextView(context);
textView.setText("Select an option");
textView.setPadding(20, 30, 20, 30);
textView.setTextSize(20F);
textView.setBackgroundColor(Color.CYAN);
textView.setTextColor(Color.WHITE);

final CharSequence[] items = {"Visiting Card", "Prescription Letter"};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCustomTitle(textView);
builder.setItems(items, (dialog, item) -> {
}).show();

Custom Alert Dialog Header

How to change the color of an AlertDialog message?

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>

How to change button text color in AlertDialog

Use alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE) to have access to Alerts Buttons.

AlertDialog alertDialog;        
alertDialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("title") //AlertDialog title
.setMessage("msg") //AlertDialog description
.setCancelable(true)

//call button
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e("RuiterRoute", "Emergency Call triggered by user");
}
})

//cancel button
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e("RuiterRoute", "Emergency Call canceld by user");
}
}).show();
alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.RED);

How to change the text color of items in MaterialAlertDialog?

To change the color of the text in the items you can use something like:

<style name="AlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="android:checkedTextViewStyle" ns2:ignore="NewApi">@style/myCheckedTextView</item>
</style>

with:

  <style name="myCheckedTextView" parent="@style/Widget.MaterialComponents.CheckedTextView">
<item name="android:textColor">@color/.....</item>
</style>

Otherwise you can also use:

<style name="AlertDialog"  
parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="colorSecondary">@color/selected</item> <!-- selected -->
</style>

enter image description here

how to change the button text color on AlertDialog globally using style?

With the MaterialComponents theme and the MaterialAlertDialogBuilder you can define globally the style using the materialAlertDialogTheme attribute in your app theme.

Something like:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="materialAlertDialogTheme">@style/My_MaterialAlertDialog</item>
</style>

Then you can define a custom style:

  <style name="My_MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<!-- Style for positive button -->
<item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
<!-- Style for negative button -->
<item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
<!-- Style for neutral button -->
<item name="buttonBarNeutralButtonStyle">@style/NeutralButtonStyle</item>
</style>

with the button style defined by:

  <style name="PositiveButtonStyle" parent="@style/Widget.MaterialComponents.Button">
<item name="android:textColor">#FFFFFF</item>
<item name="backgroundTint">@color/primaryDarkColor</item>
</style>

<style name="NegativeButtonStyle" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">@color/primaryDarkColor</item>
</style>

<style name="NueutralButtonStyle" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
....
</style>

With the version 1.1.0 of the library you can also simply override the default color using the materialThemeOverlay in the custom style:

  <style name="My_MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="materialThemeOverlay">@style/DialogButtonOverlay</item>
</style>

<style name="DialogButtonOverlay">
<item name="colorPrimary">@color/...</item>
</style>


Related Topics



Leave a reply



Submit