Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Alert Dialog - How to change the title color and the text color without changing the primary color

Hay,

Is there a simple way to change the title and the text colour without changing the primary colour of the whole app?

Here is what I got now:

enter image description here

I dont want to change the textColorPrimary

like image 827
Raheel Hasan Avatar asked Mar 07 '23 19:03

Raheel Hasan


1 Answers

First, create a style definition like this:

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

Then, when you create the dialog, instead of using the AlertDialog.Builder(Context) constructor, use the AlertDialog.Builder(Context, int) method and pass a reference to your style:

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

enter image description here

Even though this depends on changing textColorPrimary, it doesn't do so in a way that affects anything else in your app.

like image 96
Ben P. Avatar answered Mar 10 '23 12:03

Ben P.