Как можно поменять цвет заголовка в alertdialog программно?
2 ответа
Есть два варианта, использовать свой 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()
и дальше уже при нажатии на кнопку или как вы там его менять планируете менять, изменять его.
Вы можете придать стиль своему 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>