I'm trying to change the color of AlertDialog with MultiChoiceItems
Java :
private void displayMultiSelectDialog() {
emoji = getResources().getStringArray(R.array.photo_editor_emoji);
boolean[] checkedItems = new boolean[emoji.length];
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this, R.style.DialogTheme);
dialogBuilder.setTitle("Select Emoji");
dialogBuilder.setMultiChoiceItems(convertListEmoji(emoji), checkedItems,
(dialogInterface, which, isSelected) -> {
if (isSelected) {
selectedEmoji.add(emoji[which]);
} else {
selectedEmoji.remove(emoji[which]);
}
}
);
dialogBuilder.setPositiveButton("Done", (dialog, which) -> showSelectedColors());
dialogBuilder.create().show();
}
XML :
<style name="DialogTheme">
<item name="android:background">#000</item>
<item name="android:textColor">#586eea</item>
<item name="android:textSize">18sp</item>
<item name="android:textColorPrimary">#586eea</item>
<item name="android:colorAccent" tools:targetApi="lollipop">#586eea</item>
</style>
but I have a problem my AlertDialog background, it's BLACK so the checkbox items look invisible
how to make the checkbox looks like this :
Massive thanks in advance
Try setting a custom theme to your checkbox.
Add style in your styles.xml
<style name="MyCheckBox" parent="Theme.AppCompat.NoActionBar">
<item name="colorControlNormal">#000</item> <!-- normal border color -->
<item name="colorControlActivated">#000</item> <!-- activated color -->
<item name="android:textColor">#FFFF3F3C</item> <!-- text color -->
</style>
and set this style as your checkbox's theme as below:
<CheckBox android:id="#+id/check_agree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="I agree"
android:theme="#style/MyCheckBox"/> <!-- here apply your checkbox style -->
<CheckBox
...
android:buttonTint="#color/tint_color" />
minSdkVersion is 21+ use android:buttonTint attribute to update the color of a checkbox:
Related
I'm trying to change the color of the AlertDialog with likely all solutions defined for this issue in stackoverflow and it still doesn't work.
This is the code I'm using to try to achieve that:
val vacationDialog = AlertDialog.Builder(fragment.context,R.style.DialogTheme)
val factory = LayoutInflater.from(OtrosDetailFragment.fragment.context);
var view = factory.inflate(R.layout.sample, null)
[...]
vacationDialog.setView(view)
val window = vacationDialog.create()
vacationDialog.setPositiveButton("Cerrar"){dialog, which ->
dialog.dismiss()
}
/**Listener called when the AlertDialog is shown**/
vacationDialog.show()
window.setOnShowListener {
/**Get the positive button from the AlertDialog**/
val positiveButton = window.getButton(DialogInterface.BUTTON_POSITIVE)
/**Set your color to the positive button**/
positiveButton.setBackgroundColor(ContextCompat.getColor(fragment.context!!, R.color.colorPrimary))
positiveButton.setTextColor(ContextCompat.getColor(fragment.context!!, R.color.colorPrimary))
positiveButton.setHintTextColor(ContextCompat.getColor(fragment.context!!, R.color.colorPrimary))
positiveButton.setLinkTextColor(ContextCompat.getColor(fragment.context!!, R.color.colorPrimary))
}
This is the style DialogTheme defined in styles.xml, in which, by the way, I cannot find a way to change the default button color (black), and seems to override any change tried from code.
<style name="DialogTheme" parent="android:Theme.Holo">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<!-- No backgrounds, titles or window float -->
<item name="android:windowBackground">#color/white</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:gravity">center</item>
</style>
Any idea on how to approach this issue?
If you are using an AppCompat theme you can use something like:
<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<!-- Used for the buttons -->
<item name="colorAccent">#FFCC00</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">#FFFFFF</item>
<!-- Used for the background -->
<item name="android:background">#5fa3d0</item>
</style>
With the new Material components for android library you can use the new androidx.appcompat.app.AlertDialog.
Just use something like:
new MaterialAlertDialogBuilder(context)
.setTitle("Dialog")
.setMessage("Lorem ipsum dolor ....")
.setPositiveButton("Ok", /* listener = */ null)
.setNegativeButton("Cancel", /* listener = */ null)
.show();
You can customize your theme with something like:
<!-- Alert Dialog -->
<style name="MyThemeOverlay.MaterialComponents.MaterialAlertDialog" parent="#style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="buttonBarPositiveButtonStyle">#style/PositiveButtonStyle</item>
<item name="buttonBarNegativeButtonStyle">#style/Widget.MaterialComponents.Button.TextButton.Dialog</item>
</style>
and:
<style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">#FFFFFF</item>
<item name="backgroundTint">#color/selector_bt</item>
</style>
If you use custom layout then you can change anything easily. Follow the code below:
private fun showAlertDialog(activity: Activity){
val dialog = Dialog(activity)
dialog.setContentView(R.layout.dialog_layout)
dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
dialog.show()
}
You can create your custom layout for AlertDialog and inflate it in your custom class extending AlertDialog.
Use androidx.appcompat.app.AlertDialog rather than normal android.app.AlertDialog
I'm trying to change the background color of the "header" (top section) of an AlertDialog. I managed to change the color of the title but I can't find how you change the background color of its container. Is it possible? Any suggestions?
This is what I have so far.
AndroidManifest.xml
<application
...
android:theme="#style/AppTheme">
styles.xml
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionBarStyle">#style/ActionBarStyle</item>
<item name="android:alertDialogTheme">#style/AlertDialogTheme</item>
</style>
another_file_with_styles.xml
<style name="AlertDialogTheme" parent="#android:style/Theme.Holo.Light.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:textColor">#color/success_color</item>
</style>
a method in a class does this
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(searchCriteria.getName());
builder.setItems(items, clickListener);
AlertDialog alert = builder.create();
alert.show();
// Eventually I'll do this to change the color of the divider
// int titleDividerId = context.getResources().getIdentifier("titleDivider", "id", "android");
// View titleDivider = alert.findViewById(titleDividerId);
//
// if (titleDivider != null) {
// titleDivider.setBackgroundColor(context.getResources().getColor(R.color.accent_color));
//}
I was trying to follow this tutorial, but it doesn't explain how to change the background color of the window.
EDIT: just to be clear, the arrow is pointing to the gray/white background color (not to the title [Make and Model])
You can set the background color of the title using a custom title view.
Creat the custom title view and define the background color:
res / layout / custom_title.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/backgroundColor"
android:padding="16dp">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/textColor" />
</RelativeLayout>
Set the custom title view:
View customTitleView = getLayoutInflater().inflate(R.layout.custom_title, null);
TextView title = (TextView) customTitleView.findViewById(R.id.title);
title.setText("TITLE");
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setItems(items, clickListener);
builder.setCustomTitle(customTitleView);
AlertDialog alert = builder.create();
alert.show();
I am trying new AlertDialog from appcompat v7 22.1.1.
It works pretty well (In all android versions) as in image.
Style for AlertDialog is this. (For now I am using hardcoded color values instead of color resources)
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimaryDark">#111111</item>
<item name="colorPrimary">#00ddff</item>
<item name="colorAccent">#0044aa</item>
<item name="colorButtonNormal">#00aaaa</item>
<item name="colorControlHighlight">#00ddff</item>
<item name="alertDialogTheme">#style/AlertDialogTheme</item>
</style>
<style name="AlertDialogTheme" parent="Theme.AppCompat.Dialog.Alert">
<item name="colorAccent">#0044aa</item>
<item name="android:background">#ffffff</item>
<item name="android:textColorPrimary">#000000</item>
<item name="android:windowTitleStyle">#style/MyTitleTextStyle</item>
</style>
<style name="MyTitleTextStyle">
<item name="android:textColor">#0044aa</item>
<item name="android:textAppearance">#style/TextAppearance.AppCompat.Title</item>
</style>
My question is ,
1) how to change statePressed color which is rounded (Gray) in the image ?
2) No pressed color is there in android >= 21 , what is hack for this ?
3) How can I have different colors of action buttons (Is it possible)?
Any help would be great.
You can use style attributes like
buttonBarButtonStyle
buttonBarNegativeButtonStyle
buttonBarNeutralButtonStyle
buttonBarPositiveButtonStyle
Example:
<style name="dialog_theme" parent="Theme.AppCompat.Dialog.Alert">
<item name="buttonBarNegativeButtonStyle">#style/dialog_button.negative</item>
<item name="buttonBarPositiveButtonStyle">#style/dialog_button.positive</item>
</style>
<style name="dialog_button">
<item name="android:textStyle">bold</item>
<item name="android:minWidth">64dp</item>
<item name="android:paddingLeft">8dp</item>
<item name="android:paddingRight">8dp</item>
<item name="android:background">#drawable/dialogButtonSelector</item>
</style>
<style name="dialog_button.negative">
<item name="android:textColor">#f00</item>
</style>
<style name="dialog_button.positive">
<item name="android:layout_marginLeft">8dp</item>
<item name="android:textColor">#00f</item>
</style>
Where dialogButtonSelector is our custom drawable selector.
Unfortunatelly setting background on dialog_button destroy our paddings and margins so I need to set it again.
dialog_button style can inherit through Widget.AppCompat.Button.ButtonBar.AlertDialog but I found that it has missing styles like textStyle bold.
I have the Answer for the 3rd Questions
( How can I have different colors of action buttons (Is it possible)? )
Code:
// Initialize AlertDialog & AlertDialog Builder
AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this);
builder.setTitle("AlertDialog Title");
...........
.........
//Build your AlertDialog
AlertDialog Demo_alertDialog= builder.create();
Demo_alertDialog.show();
//For Positive Button:
Button b_pos;
b_pos=Demo_alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
if(b_pos!=null){
b_pos.setTextColor(getResources().getColor(R.color.YourColor));
}
//For Neutral Button:
Button b_neu;
b_neu=Demo_alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL);
if(b_neu!=null){
b_neu.setTextColor(getResources().getColor(R.color.YourColor));
}
//For Negative Button:
Button b_neg;
b_neg=Demo_alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
if(b_neg!=null){
b_neg.setTextColor(getResources().getColor(R.color.YourColor));
}
Happy Coding :)
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setMessage("Title"); builder.setCancelable(true);
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertdialog = builder.create();
alertdialog.show();
Button nbutton = alertdialog.getButton(DialogInterface.BUTTON_NEGATIVE);
nbutton.setBackground(getResources().getDrawable(R.drawable.btn_press_white_rect));
Button pbutton = alertdialog.getButton(DialogInterface.BUTTON_POSITIVE);
pbutton.setBackground(getResources().getDrawable(R.drawable.btn_press_white_rect));
**btn_press_white_rect.xml**
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/rounded_rect_yellow" ></item>
<item android:state_pressed="false" android:drawable="#drawable/rounded_rect_white" ></item>
</selector>
In an app I am developing, all I want to do is change the highlight color of an AlertDialog button. My main source of intelligence is this discussion, but I read pretty much every article about this on StackOverflow as well. The code below runs without crashing, but the highlight color of the button is still the default orange-yellow. Does anybody have an idea what is wrong?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(message).setCancelable(true)
.setPositiveButton(getResources().getString(R.string.positive_button_title), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int whichButton) {
// Do stuff
}
});
// Incredibly bulky way of simply changing the button highlight color,
// but it appears to be the only way we can do it without getting a NullPointerException
AlertDialog dialog = builder.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialogInterface) {
if (Build.VERSION.SDK_INT < 16) {
((AlertDialog) dialogInterface).getButton(AlertDialog.BUTTON_POSITIVE).setBackgroundDrawable(getResources().getDrawable(R.drawable.dialog_button_drawable));
} else {
((AlertDialog) dialogInterface).getButton(AlertDialog.BUTTON_POSITIVE).setBackground(getResources().getDrawable(R.drawable.dialog_button_drawable));
}
}
});
dialog.show();
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/custom_button_background" android:state_focused="true" android:state_pressed="false" />
<item android:drawable="#android:drawable/btn_default" android:state_focused="true" android:state_pressed="true" />
<item android:drawable="#android:drawable/btn_default" android:state_focused="false" android:state_pressed="true" />
<item android:drawable="#android:drawable/btn_default" />
</selector>
Sorry if I don't get your question correctly, but since you are loading the drawable anyway, shouldn't this work for you as well?
How to change colors of a Drawable in Android?
When creating the AlertDialog you can set a theme to use.
Example - Creating the Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle);
builder.setTitle("AppCompatDialog");
builder.setMessage("");
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
builder.show();
styles.xml - Custom style
<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<!-- Used for the buttons -->
<item name="colorAccent">#FFC107</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">#FFFFFF</item>
<!-- Used for the background -->
<item name="android:background">#4CAF50</item>
</style>
In order to change the Appearance of the Title, you can do the following. First add a new style:
<style name="MyTitleTextStyle">
<item name="android:textColor">#FFEB3B</item>
<item name="android:textAppearance">#style/TextAppearance.AppCompat.Title</item>
</style>
afterwards simply reference this style in your MyAlertDialogStyle:
<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
...
<item name="android:windowTitleStyle">#style/MyTitleTextStyle</item>
</style>
This way you can define a different textColor for the message via android:textColorPrimary and a different for the title via the style.
I am trying to style my AlertDialog and I have been able to change most of it through styles and xml declarations... but there are still a few issues:
How do I change the area around the title bar from black to my custom color?
How do I change the outer background to transparent (the outside part that is blue the the shadow drops upon)
How do I change the buttons so they do not overlap the black border around the alert message?
here is the function I have in my RootActivity (my activities extend this one)
public static void showNoConnectionDialog(Context ctx1) {
final Context ctx = ctx1;
LayoutInflater factory = LayoutInflater.from(ctx);
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(ctx, R.style.SetdartDialog));
builder.setView(factory.inflate(R.layout.alert_dialog, null))
.setIcon(R.drawable.icon)
.setCancelable(true)
.setMessage(R.string.check_wireless_settings)
.setTitle(R.string.no_connection)
.setPositiveButton(R.string.myes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ctx.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
}
})
.setNegativeButton(R.string.mno, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
})
.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
return;
}
})
.show();
}
here a snippet from styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.WhiteBackground" parent="android:Theme">
<item name="android:windowBackground">#null</item>
<item name="android:background">#android:color/white</item>
<!-- Dialog attributes
<item name="alertDialogStyle">#style/AlertDialog</item> -->
</style>
<style name="SetdartDialog">
<item name="android:background">#color/sd_blue</item> <!-- MUST HAVE with white bg-->
<!--<item name="android:windowBackground">#color/sd_blue</item> -->
<!--<item name="android:windowBackground">#color/transparent</item> needed with white bg ? -->
<item name="android:windowFrame">#color/transparent</item><!-- not sure what this changes-->
<item name="android:textColor">#android:color/black</item>
<item name="android:windowNoTitle">true</item>
<item name="android:textSize">10sp</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#color/transparent</item>
<item name="android:windowTitleStyle">#style/setwindowTitleStyle</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:gravity">center_vertical|center_horizontal</item>
<!--<item name="android:colorBackgroundCacheHint">#android:color/white</item>-->
</style>
<style name="setwindowTitleStyle">
<item name="android:textColor">#android:color/white</item>
<item name="android:background">#color/sd_blue</item>
</style>
</resources>
Also R.layout.alert_dialog
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/screen"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
Create your custom layout with all these attributes you've mentioned. Use Dialog instead of AlertDialog, inflate the layout you have created, use the Dialog object to set the inflated layout. If you haven't been introduced to inflating service, do some research. After you are clear with inflating, remember that all the components of the dialog you access with the View object, that you have created with the inflating. The rest (like click listeners) remains to be done on usual way. Cheers. I hope it helps.
To make custom AlertDialog you should extend DialogFragment