EditText style
<style name="MyEditTextStyle" parent="Widget.AppCompat.EditText">
<item name="android:textCursorDrawable">#null</item>
<item name="android:textColor">#color/color_states_blackish</item>
<item name="android:fontFamily">#font/sfd_medium</item>
<item name="android:background">#drawable/edit_white_gray_x_normal_rounded</item>
<item name="android:height">#dimen/general_view_height</item>
</style>
Dialog style
<style name="MyAlertDialogTheme" 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/text_color_blackish</item>
<item name="android:fontFamily">#font/sfd_regular</item>
</style>
<style name="PositiveButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">#color/text_color_blackish</item>
<item name="android:fontFamily">#font/sfd_bold</item>
</style>
Dialog code
final EditText editText = new EditText(new android.view.ContextThemeWrapper(getContext(),R.style.MyEditTextStyle));
final FrameLayout layout = new FrameLayout(getContext());
int padding = getContext().getResources().getDimensionPixelSize(R.dimen.standard_wall_space);
int paddingView = getContext().getResources().getDimensionPixelSize(R.dimen.standard_view_space);
layout.setPadding(padding,paddingView,padding,paddingView);
layout.addView(editText);
editText.setHint(R.string.type_here);
new AlertDialog.Builder(getContext(),R.style.MyAlertDialogTheme)
.setTitle("Enter Your Unit")
.setView(layout)
.setPositiveButton("Add", (dialog, whichButton) -> {
String value = editText.getText().toString();
MyApp.showToast("Unit-"+value);
})
.setNegativeButton("Cancel", null).show();
What I got following kind of pointers with background same as EditText, I remove them ContextThemeWrapper then It shows correctly but I need change the background using style?, Why is this happening? This is dialog inside fullscreen dialog? Any solution? Thanks
The problem is that the android:background parameter in your theme is applied to everything inside the EditText. For me the solution was in removing that parameter from the style.
Looks like bugg, selector popup window get wrong background style, Don't use ContextThemeWrapper, style then it shows normally, set background programmatically.
final EditText editText = new EditText(getContext());
editText.setBackgroundResource(R.drawable.edit_white_gray_x_normal_rounded);
editText.setTextColor(ContextCompat.getColor(getContext(),R.color.color_states_blackish));
Additional to other answers here, it also occurs when we set background in the app theme in style.xml. Another solution I found was to replace android:background with android:backgroundTint of the background color and set android:backgroundTintMode to multiple.
Put this in styles:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="android:backgroundTint">#FFFFFF</item>
<item name="android:backgroundTintMode">multiply</item>
</style>
or in the view tag whose background color you want to change:
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#FFFFFF"
android:backgroundTintMode="multiply" />
Related
hello, I have an alert dialog. the buttons are purple. yes I can change the color of the text inside using:
alertDialog.getButton(Dialog.BUTTON_NEGATIVE).
setTextColor(Color.parseColor("#000000"));
however when they are touched, there is a purple hue that forms.
I want the hue to be blue like the main accent of my app and the text to be black.
this is my alert style:
<style name="MyCustomAlert2" parent="Theme.MaterialComponents.Dialog.Alert">
<item name="colorAccent">#color/materialBlue</item>
<item name="android:textColorPrimary">#android:color/black</item>
<item name="android:windowBackground">#drawable/custom_alert2</item>
<item name="colorControlNormal">#android:color/black</item>
<item name="colorControlActivated">#color/materialBlue</item>
<item name="textColorAlertDialogListItem">#android:color/black</item>
</style>
note: none of the colors linked above are purple.
thanks for the help
Use new theandroidx.appcompat.app.AlertDialog and the Material components for android library.
Just use something like:
new MaterialAlertDialogBuilder(context)
.setTitle("...")
.setMessage("....")
.setPositiveButton("Ok", /* listener = */ null)
.setNegativeButton("Cancel", /* listener = */ null)
.show();
Then you can customize the style 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 here you can change each button:
<style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">#000000</item>
<item name="backgroundTint">#color/selector_bt</item>
</style>
You can set the style globally in your app theme using:
<item name="materialAlertDialogTheme">#style/MyThemeOverlay.MaterialComponents.MaterialAlertDialog</item>
or you can use the constructor:
new MaterialAlertDialogBuilder(context,
R.style.MyThemeOverlay_MaterialComponents_MaterialAlertDialog)
I am having problems with the AlertDialog. The buttons doesn't use the accentColor to set the button text color any more.
I am using the newest support library, v24.2.1. I am styling my dialogs in my styles.xml the following way:
<style name="Base.Theme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:alertDialogTheme">#style/Widget.DialogStyle</item>
<item name="alertDialogTheme">#style/Widget.DialogStyle</item>
</style>
and the Widget.DialogStyle looks like this:
<style name="Widget.DialogStyle" parent="#style/Theme.AppCompat.Light.Dialog.Alert">
<item name="android:colorAccent" tools:targetApi="lollipop">#color/primaryColor</item>
<item name="colorAccent">#color/primaryColor</item>
<item name="android:textColorPrimary">#color/primaryText</item>
<item name="android:textColor">#color/primaryText</item>
<item name="android:background">#color/backgroundColor</item>
<item name="android:textAppearanceLarge">#color/primaryText</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
</style>
On app API's lower than 24 the dialog buttons are colored with the colorAccent but on API 24 this is no more the behaviour (the text is black, should be orange). See the following screenshot.
Is there anybody that knows how to get the accentColor back on the buttons?
Thank you.
For some AlertDialog implementations, the buttons are contained in a ButtonBar and take their style from buttonBarButtonStyle. So you have to override the settings you inherit from the parent theme (Theme.AppCompat.Light.Dialog.Alert).
Add the following item to Widget.DialogStyle:
<item name="buttonBarButtonStyle">#style/MyButtonStyle</item>
and add another style named MyButtonStyle like this:
<style name="MyButtonStyle" parent="Widget.AppCompat.Button.Borderless">
<!-- Set background drawable and text size of the buttons here
<item name="android:background">#color/my_dialog_dark</item>-->
<item name="android:textSize">18sp</item>
<!-- this is the button text color! -->
<item name="android:textColor">#color/primaryColor</item>
</style>
EDIT
Thanks to kirtan403 for pointing this out: you can also use another parent style for the buttons if Widget.AppCompat.Button.Borderless does not meet your requirements.
An example by nicola.v...#icapps.com using Widget.AppCompat.Button.ButtonBar.AlertDialog as parent style for the buttons can be found under the AOSP Issue 220699: colorAccent not applied to AlertDialog buttons on Android N.
Be sure you are importing the correct AlertDialog:
import android.support.v7.app.AlertDialog
Also try inflating the dialog with another AlertDialog.Builder constructor:
android.support.v7.app.AlertDialog.Builder#Builder(android.content.Context, int)
which means, the second parameter is the style of the dialog:
mDialog = new AlertDialog.Builder(context, R.style.Widget.DialogStyle).create();
EDIT:
Sharing the code, that I use to show alert Dialog:
public AlertDialog showSimpleDialog(Context context, String title, String message, String btnOk, DialogInterface.OnClickListener handler) {
if (mDialog != null && mDialog.isShowing()) {
mDialog.dismiss();
mDialog = null;
}
mDialog = new AlertDialog.Builder(context, R.style.AppTheme_Dialog).create();
mDialog.setTitle(title);
mDialog.setMessage(message);
mDialog.setButton(DialogInterface.BUTTON_POSITIVE, btnOk, handler);
mDialog.setCanceledOnTouchOutside(false);
mDialog.show();
return mDialog;
}
and the style:
<style name="AppTheme.Dialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#color/colorPrimary</item> //blue
<item name="android:textColorPrimary">#color/primary_text_material_light</item> //black
<item name="android:windowMinWidthMajor">97%</item>
<item name="android:windowMinWidthMinor">97%</item>
</style>
and the buttons are blue. Tested on emulator API 24.
How to change color of Items in AlertDialog.Builder AppCompat ?
I want to set textColor of items(photo,gallary) to any other.
here is my code:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AppCompatAlertDialogStyle);
builder.setTitle(getResources().getString(R.string.choose_image_source));
builder.setItems(new CharSequence[]{getResources()
.getString(R.string.photo), getResources()
.getString(R.string.camera)}, new DialogInterface.OnClickListener() {
...
<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#000000</item><!-- buttons OK cancel-->
<item name="android:textColorPrimary">#696969</item><!--text in message-->
<item name="android:background">#f0a400</item> <!-- all bg-->
</style>
<style name="MyTitleTextStyle">
<item name="android:textColor">#000000</item><!-- title color-->
<item name="android:textAppearance">#style/TextAppearance.AppCompat.Title</item>
</style>
Try this:
<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#000000</item><!-- buttons OK cancel-->
<item name="android:textColorPrimary">#696969</item><!--text in message-->
<item name="android:background">#f0a400</item> <!-- all bg-->
<!-- items color -->
<item name="textColorAlertDialogListItem">#33b5e5</item>
</style>
<style name="MyTitleTextStyle">
<item name="android:textColor">#000000</item><!-- title color-->
<item name="android:textAppearance">#style/TextAppearance.AppCompat.Title</item>
</style>
I kinda faced the same problem. And the only way to solve it was extending my own version of layout. I see that in your case it is an AlertDialog. What I recommend you to do, is to create a unique class, that is your customized AlertDialog and create a layout for this, and then you inflate this.
Here is a post that helped me a lot. http://blog.androgames.net/10/custom-android-dialog/
I followed this post and solved my problem with customizing dialogs.
Please, if you have more doubts, let me know.
Thanks.
I created a custom alertdialog. I used a half-transparent shape for background.
And its button is fully transparent in unfocused state.
The button is also customized by using a solid colored shape.
My problem is, there are white lines in two sides of the button where the default button originally located.
I tried to manipulate dividers by changing its color, width etc.
I tried:
android:showDividers="none"
But none of my operations worked.
So I am thinking these two lines may not be dividers.
But I can not find what are these and how to hide them.
Here is my java code:
AlertDialog.Builder alert = new AlertDialog.Builder(this, R.style.Theme_Organic_Dialog_Alert);
alert.setMessage(R.string.channel_warning_message);
alert.setNeutralButton(R.string.okay, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//sthLikeDoNothing();
}
});
alert.show();
My theme:
<style name="Theme_Organic_Dialog_Alert">
<item name="android:windowBackground">#drawable/alert_dialog_bg</item>
<item name="android:background">#android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:buttonBarButtonStyle">#style/Organic_Button_AlertDialog</item>
<item name="android:buttonBarStyle">#style/Organic_ButtonBar_AlertDialog</item>
<item name="android:alertDialogStyle">#style/AlertDialog_Organic</item>
<item name="android:textAppearanceMedium">#style/TextAppearance_Organic</item>
</style>
And my button styles:
<style name="Organic_ButtonBar_AlertDialog" parent="#android:style/Holo.ButtonBar.AlertDialog">
<item name="android:background">#android:color/transparent</item> <!-- ButtonBar background transparency -->
</style>
<style name="Organic_Button_AlertDialog" parent="#android:style/Holo.ButtonBar.AlertDialog"> <!-- Custom drawable button -->
<item name="android:background">#drawable/alert_dialog_button_selector</item>
<item name="android:textColor">#drawable/alert_dialog_button_txt_color_selector</item>
<item name="android:fontFamily">roboto-regular</item>
<item name="android:textSize">20sp</item>
</style>
These are the lines I want to hide:
I found what causes those divider lines. It was because I was setting Holo as the parent of my button styles.
Here,
<style name="Organic_ButtonBar_AlertDialog" parent="#android:style/Holo.ButtonBar.AlertDialog">
and here,
<style name="Organic_Button_AlertDialog" parent="#android:style/Holo.ButtonBar.AlertDialog">
Now I removed parents and the lines are gone.
How do I change title's background in this dialog ["Lesson Editor"]?
As you can see,the theme of my application is dark and in the title's background in the main activities are dark as i want, but somehow the dialog isn't.
Help me please!!
Here is the screenshot of my dialog:
Recently I used it for my implementation, so here is my code.
You can do that by creating a new style in the styles.xml file:
<style name="question_dialog" parent="#android:style/Theme.Holo.Dialog">
<item name="android:windowTitleStyle">#style/question_dialog_title</item>
</style>
<style name="question_dialog_title" parent="android:Widget.TextView">
<item name="android:background">#5cc5cc</item>
<item name="android:textSize">21sp</item>
<item name="android:textColor">#ffffff</item>
</style>
and then, when you create your Dialog object, you wanna use this constructor:
answeringDialog = new Dialog(this, R.style.question_dialog);
the output is: