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();
Related
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:
How to make an Alert Dialog in full screen in Android?
Try below code
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
you must create a custom style for your dialog
in your style.xml
<style name="DialogTheme" parent="android:Theme.Dialog">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<!-- No backgrounds, titles or window float -->
<item name="android:windowBackground">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
</style>
while inflating your dialog set this theme
dialog = new Dialog(this, R.style.DialogTheme);
【Method 1 | Use Custom Style】
In styles file:
<style name="myFullscreenAlertDialogStyle" parent="Theme.AppCompat.Light.NoActionBar"> //no actionbar, but status bar exists
<item name="android:windowFullscreen">true</item> //remove status bar
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item> //smooth animation
<item name="colorAccent">#color/colorAccent</item> //change button text color
</style>
In java file:
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.myFullscreenAlertDialogStyle); //second argument
【Method 2 | Use Built-In Style】
In java file:
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), android.R.style.Theme_Material_Light_NoActionBar_Fullscreen);
This method will make the button text become green color, you can use dialog.getButton().setTextColor() to change it.
if you're using DialogFragment to build AlertDialog you may set MATCH_PARENT on LayoutParams in onResume():
#Override
public void onResume() {
super.onResume();
ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.MATCH_PARENT;
getDialog().getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
}
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.
In my application i am using custom dialog .my dialog have rounded rectangle border and i can not remove that.please help me to set style for that to remove border. Thanks
public class myDialog extends Dialog
{
private String mMessage = "";
public myDialog(Context context, String message)
{
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
View v = getLayoutInflater().inflate(R.layout.dialog, null);
mMessage = message;
setCancelable(false);
setCanceledOnTouchOutside(false);
setContentView(v);
getWindow().setLayout(300,150);
getWindow().setBackgroundDrawableResource(R.);
}
}
My dialog cutom Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/dialog"
>
</LinearLayout>
This has been a long time frustrating issue with dialogs in Android. The only way I've been able to solve it, is by extending DialogFragment instead of Dialog and adding the following:
YourDialog.java
Dialog dialog = new Dialog(getActivity(), R.style.ActivityDialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
styles.xml
<style name="ActivityDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#null</item>
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowTitleStyle">#style/ActivityDialog</item>
</style>
android:border = "invisible"
or transparent. You can find the info online
All my theme is working beside my dialogfragment background.
I try to make my dialogFragment background black but i'm doing something wrong.
Here what i got so far in the theme xml (Note that i trunked the top and the bottom of it):
<style name="easydealtheme" parent="#style/_easydealtheme"/>
<style name="_easydealtheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:editTextBackground">#drawable/easydealtheme_edit_text_holo_light</item>
<item name="android:dropDownListViewStyle">#style/Listviewdropdowneasydealtheme</item>
<item name="android:textViewStyle">#style/Textvieweasydealtheme</item>
<item name="android:editTextStyle">#style/Edittexteasydealtheme</item>
<item name="android:alertDialogTheme">#style/Dialogeasydealtheme</item>
...
Here is what i got in the Styles xml for my dialog :
<style name="Dialogeasydealtheme" parent="android:Theme.Holo.Dialog">
<item name="android:textColor">#FFFFFF</item>
<item name="android:windowBackground">#color/test</item>
</style>
Here is the code that show the dialogFragment :
public static void ShowPhotoDialog(String title,File photoFile, FragmentManager fragM)
{
FragmentPhotoDialog photoD = new FragmentPhotoDialog();
FragmentTransaction fTX = fragM.BeginTransaction();
photoD.SetStyle(DialogFragmentStyle.NoFrame, EasyDeal_Android.Resource.Style.easydealtheme);
photoD.SetTile(title);
photoD.SetPhotoFile(photoFile);
photoD.Show(fTX, "Dialog");
}
Here the result which is not working (The white should be black ) :
I went crazy finding a solution for this problem until I came across a solution.
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Setup the layout
LayoutInflater inflater = getActivity().getLayoutInflater();
final View root = inflater.inflate(*"YOUR LAYOUT"*, null);
root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//Customizing the dialog features
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(root);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(getResources().getColor(*"YOUR SELECTED COLOR"*)));
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
return dialog;
}
Try changing the alert dialog background as
<style name="Dialogeasydealtheme" parent="android:Theme.Holo.Dialog">
<item name="android:textColor">#FFFFFF</item>
<item name="android:background">#color/black</item>
</style>
Let me know if see any issue.
I find a solution not my favorite though but it will do.
I added a background to my layout. Here is the code on my main linearLayout:
<LinearLayout xmlns:p1="http://schemas.android.com/apk/res/android"
p1:orientation="vertical"
p1:minWidth="25px"
p1:minHeight="25px"
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:id="#+id/liDialogPhoto"
p1:background="#drawable/easydealtheme_tilesbg">
I can now continue using no frame having a nice colored background !
Thanks for the help !