Reputation: 13345
on android O when i do this :
<style name="MyTheme" parent="@android:style/Theme.Material.Light.NoActionBar">
<item name="android:textColor">#ff0000</item>
</style>
The text button color of my alertdialog change, but this not work under lollipop. Worse on lollipop it's change the color of the title of the alertdialog instead.
How from kitkat to android O I can globally change the font color of the button of all my alertdialog ?
Upvotes: 3
Views: 1381
Reputation: 363667
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>
Upvotes: 4
Reputation: 921
You need to write a theme for AlertDialog and set it to AppTheme. It will change you alet dialog theme globally.
<style name="AppAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
<item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
</style>
<style name="NegativeButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">@color/colorPrimaryText</item>
<item name="android:backgroundTint">@android:color/transparent</item>
</style>
<style name="PositiveButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">@color/colorPrimaryText</item>
<item name="android:backgroundTint">@android:color/transparent</item>
</style>
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
...
<item name="alertDialogTheme">@style/AppAlertDialog</item>
</style>
Upvotes: 1
Reputation: 336
Create a Java Class for your custom alert dialog
public class Dialog {
private static final int resId = R.layout.dialog_dialog;
private AlertDialog alertDialog;
/**
* Custom Dialog
*
* @param context
* @param titleText
* @param message
* @param positiveText
* @param negativeText
* @param type
* @param dialogListener
*/
public Dialog(final Context context,
String titleText,
String message,
String positiveText,
String negativeText,
Type type,
final DialogListener dialogListener) {
TextView labelTitle, labelMessage, buttonPositive, buttonNegative;
View view = LayoutInflater.from(context).inflate(resId, null, false);
labelTitle = view.findViewById(R.id.labelTitle);
labelMessage = view.findViewById(R.id.labelMessage);
buttonPositive = view.findViewById(R.id.buttonPositive);
buttonNegative = view.findViewById(R.id.buttonNegative);
if (Utils.isNotEmpty(titleText)) labelTitle.setText(titleText); //(HMI2Utils.isNotEmpty is a null check
if (Utils.isNotEmpty(message)) labelMessage.setText(message);
if (Utils.isNotEmpty(positiveText)) buttonPositive.setText(positiveText);
if (Utils.isNotEmpty(negativeText)) buttonNegative.setText(negativeText);
switch (type) {
case DANGEROUS:
labelTitle.setTextColor(ContextCompat.getColor(context, R.color.white));
}
buttonNegative.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
dialogListener.onNegativeButtonClick();
}
});
buttonPositive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
dialogListener.onPositiveButtonClick();
}
});
Utils.hideSoftKeyboard((Activity) context);
ContextThemeWrapper ctw = new ContextThemeWrapper(context, R.style.MyDialogTheme);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctw);
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setView(view);
alertDialog = alertDialogBuilder.create();
Window window = alertDialog.getWindow();
if (window != null) {
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(ContextCompat.getColor(context, R.color.app_theme_color));
}
alertDialog.show();
}
and for xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rlRoot"
android:layout_width="736.15px"
android:layout_height="532.66px"
tools:ignore="Overdraw">
<View
android:id="@+id/focus_eater_dummy"
android:layout_width="1px"
android:layout_height="1px"
android:focusable="true" />
<ImageView
android:layout_width="736.15px"
android:layout_height="532.66px"
android:scaleType="fitXY"
android:src="your drawable" />
<RelativeLayout
android:id="@+id/container"
android:layout_width="736.15px"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="67.83px"
android:layout_marginRight="67.83px"
android:layout_marginTop="57.855px"
android:orientation="vertical">
<TextView
android:id="@+id/labelTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center_horizontal"
android:maxLines="1"
android:text="@string/delete_message_string"
android:textColor="@color/white"
android:textSize="24sp" />
<TextView
android:id="@+id/labelMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18px"
android:lineSpacingExtra="18px"
android:gravity="center_horizontal"
android:lineSpacingMultiplier="1"
android:text="@string/delete_message_string"
android:textColor="@color/white"
android:textSize="24sp" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="@+id/containerButtons"
android:layout_width="736.15px"
android:layout_height="532.66px"
android:weightSum="2"
android:gravity="bottom"
android:layout_gravity="bottom"
android:orientation="horizontal">
<TextView
android:id="@+id/buttonPositive"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="@dimen/margin_button_vertical_dai"
android:background="@drawable/popup_button_selector"
android:gravity="center"
android:textColor="@color/white"
android:textSize="32sp"
tools:text="OK" />
<TextView
android:id="@+id/buttonNegative"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="@drawable/popup_button_selector"
android:gravity="center"
android:text="Cancel"
android:textColor="@color/white"
android:textSize="32sp" />
</LinearLayout>
</FrameLayout>
make your custom changes and call the dialog like
new Dialog(context,
"Title 1",
"Message 2",
"OK",
"Cancel",
Dialog.Type.DANGEROUS,
new Dialog.DialogListener() {
@Override
public void onPositiveButtonClick() {
//implement Click here
}
@Override
public void onNegativeButtonClick() {
//implement Click here
}
}
);
dialog.dismiss();
Upvotes: 0