Reputation: 93
I have been using a library called material-dialogs by afollestad. I need to change the text color of the positive and negative button action.
MaterialDialog(this).show {
positiveButton("yes") {
clearData()
goToLoginPage()
}
negativeButton("cancel") { dismiss() }
message("Some message")
}
The above is the code for showing the material dialog. For the button arguments only the title of the button could be given. My requirement is to change 'yes' to green text color and 'cancel' to red text color. Is it possible to achieve using this library?
Upvotes: 5
Views: 4325
Reputation: 1558
This is a simple solution in two steps and it works for me
Step 1) In style
<style name="AppThemeMD" parent="Theme.AppCompat.Light.NoActionBar">
<item name="md_color_button_text">@color/YOUR_COLOR_HERE</item>
</style>
Step 2) In you activity or fragment
MaterialDialog(this@SplashActivity).show {
setTheme(R.style.AppThemeMD)
icon(R.mipmap.ic_launcher)
title(null, AppConstants.APP_NAME)
positiveButton(R.string.YOUR_STRING_HERE) {
}
}
Upvotes: 0
Reputation: 2609
By default, android:textColorPrimary and android:textColorSecondary attributes from your Activity theme are used for the title and content colors of Material Dialog. colorPrimary is used for the default text color of action buttons. If you wish to override these, there are attributes provided. Use this Custom Style to your Dialog.
<style name="AppTheme.Custom" parent="Theme.AppCompat">
<item name="md_color_title">@color/your_color</item>
<item name="md_color_content">@color/your_color</item>
<item name="md_color_button_text">@color/your/color</item>
</style>
or Use bellow code to Your AppTheme.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
<item name="android:textViewStyle">@style/InterTextViewStyle</item>
<item name="android:buttonStyle">@style/InterButtonStyle</item>
<item name="md_color_button_text">@color/colorAccent</item>
</style>
Hopefully you got the answer. Happy Coding
Upvotes: 0
Reputation: 3796
try this code, .positiveColor() and .negativeColor() and change to gradle version on your project
// Material Dialogs
implementation 'com.afollestad.material-dialogs:core:0.9.6.0'
See full example and download source code, when u takes.
https://github.com/afollestad/material-dialogs/blob/master/documentation/CORE.md#text-color
Upvotes: 0
Reputation: 2471
Ok here are the three lines you can override in your AppTheme inside styles.xml to give it your own colors.
To Change Dialog Title Color
<item name="md_color_title">@color/yourTitleColor</item>
To Change Dialog Content Color
<item name="md_color_content">@color/yourContentColor</item>
To Change Dialog Positive and Negative Button Colors
<item name="md_color_button_text">@color/yourPositiveNegativeColor</item>
To Change Dialog Background Color
<item name="md_background_color">@color/yourDialogBgColor</item>
To Change Dialog Divider Color
<item name="md_divider_color">@color/yourDialogDividerColor</item>
EDIT:
As OP told that he wants to give different colors for Positive and Negative buttons there seems no solution in the library but we can use a work around given below. By using java method Html.from where we can set our own color for each button text.
val yesText = "<font color='#1B1ED8'>Yes</font>"
val cancelText = "<font color='#44D81B'>Cancel</font>"
MaterialDialog(this).show {
positiveButton(text = Html.fromHtml(yesText))
negativeButton(text = Html.fromHtml(cancelText))
message(R.string.Some_message)
}
Upvotes: 12
Reputation: 2040
check this link for theming and dialog text colors : material-dialog > docs > Text Color
Upvotes: 1