1

Как можно поменять цвет заголовка в alertdialog программно?

1
  • Если вам дан исчерпывающий ответ, отметьте его как принятый нажав на галочку слева от ответа.
    – Andrew
    24 дек 2019 в 6:14

2 ответа 2

3

Есть два варианта, использовать свой textView:

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

либо сделать стиль:

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

и:

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

либо через тег setTitleColor() и дальше уже при нажатии на кнопку или как вы там его менять планируете менять, изменять его.

1
Вы можете придать стиль своему Alertdialog следующим образом:

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

И стиль выглядит вот так:

<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>

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.