Unable to show button in material dialog - android

I am using material design theme in app. I am trying to show Material Design dialog in app, but in dialog positive and negative buttons are not showing. Below is my code:
new MaterialAlertDialogBuilder(getActivity())
.setMessage("Do you want to close this application ?")
.setNegativeButton("NO", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext,"Hello",Toast.LENGTH_SHORT).show();
}
}).show();
themes.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Caketale" parent="Theme.MaterialComponents.Light">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryVariant">#color/primaryDark</item>
<item name="colorOnPrimary">#color/white</item>
<item name="colorSecondary">#color/accent</item>
<item name="colorSecondaryVariant">#color/accent</item>
<!-- Secondary brand color. -->
<!-- <item name="colorSecondary">#color/teal_200</item> -->
<!-- <item name="colorSecondaryVariant">#color/teal_700</item> -->
<item name="colorOnSecondary">#color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
How can I resolve this issue?

To use it with the design library, use this style.
<style name="AlertDialogTheme" parent="Theme.MaterialComponents.Light.Dialog.Alert">
<item name="colorPrimary">#color/colorAccent</item>
</style>
Then you use it like this.
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AlertDialogTheme);
builder.setTitle("AppCompatDialog");
builder.setMessage("Lorem ipsum dolor...");
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
builder.show();

Related

Alert dialog theme does not inherit `windowBackground` from activity theme

In my app I have this theme in the styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#000000</item>
<item name="colorAccent">#color/neon_green</item>
<item name="android:windowBackground">#color/black_17</item>
</style>
but the attribute windowBackground does not cause the dialog background to change. The dialog background color is still white. The neon_green highlight color does appear for the dialog text though.
Only the background color that does not change.
AlertDialog.Builder builder =
new AlertDialog.Builder(new ContextThemeWrapper(act, R.style.AppTheme));
builder.setMessage("text")
.setPositiveButton("pos ", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton("neg", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
builder.create().show();
I just added this to my styles
<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textColor">#color/white</item>
<item name="android:textColorPrimary">#color/white</item>
<item name="android:background">#color/black_17</item>
</style>
then I passed the styles to the builder
AlertDialog.Builder builder =
new AlertDialog.Builder(act,
R.style.AppCompatAlertDialogStyle);

Alert Dialogue Missing Button Text

I have one application in which I have alert dialogue used for page navigation. It's working fine all other API...in Android 7.1.1 it's working but not showing button text. I have tried to use style also as located answer here
Missing buttons on AlertDialog | Android 7.0 (Nexus 5x)
but I am not success to solve the puzzle. My dialogue java code is like below
private void showGotoPageDialog(){
if(mTotalPages>0){
AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity(),R.style.AlertDialogTheme);
builder.setTitle("Quick Navigation To:");
builder.setSingleChoiceItems(mPageOptions,mPageIndx-1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int item) {
mIsDialogDismissed=true;
mOptionDiag.dismiss();
SlideInDownAnimator animator = new SlideInDownAnimator();
animator.setInterpolator(new OvershootInterpolator());
animator.setAddDuration(500);
animator.setRemoveDuration(500);
mQuoteList.setItemAnimator(animator);
mPageIndx=item+1;
updateQuotesList();
updatePageInfo();
}
});
builder.setNegativeButton("Dismiss",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// action on dialog close
}
});
mOptionDiag= builder.create();
mOptionDiag.show();
}
}
and my style is like below
<style name="AppBaseTheme" parent="android:Theme.Light">
<item name="alertDialogTheme">#style/AlertDialogTheme</item>
</style>
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowActionBar">true</item>
<item name="alertDialogTheme">#style/AlertDialogTheme</item>
</style>
<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="buttonBarButtonStyle">#style/DialogButtonStyle</item>
</style>
<style name="DialogButtonStyle" parent="#style/Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">#color/colorPrimary</item>
</style>
Please check and let me know if someone have similar issue and solved it.
Thanks

Why is My Android Alert dialog BLACK?

Can you help my figure out why is my alert dialog BLACK?!
Recently i changed my app theme to support material design, but my alert dialog got black!
Here is my create dialog Code:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(TestActivity.this);
alertDialog.setCancelable(true);
alertDialog.setTitle("sample");
alertDialog.setItems(new String[] { "a", "b" }, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alertDialog.show();
and its my main style and dialog style
<style name="MaterialTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:alertDialogStyle">#style/AppDialog</item>
<item name="android:alertDialogTheme">#style/AppDialog</item>
<item name="android:textColor">#color/black</item>
<item name="android:textColorPrimary">#color/black</item>
<item name="android:dialogTheme">#style/AppDialog</item>
<item name="colorPrimaryDark">#color/myPrimaryDarkColor</item>
<item name="android:textColorHint">#color/gray_1</item>
<item name="colorAccent">#color/myAccentColor</item>
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<item name="android:windowBackground">#color/black</item>
</style>
<style name="AppDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#FFC107</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:background">#4CAF50</item>
</style>
You don't set theme for AlertDialog, for using theme, change code to:
ContextThemeWrapper ctw = new ContextThemeWrapper(TestActivity.this, R.style.AppDialog);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(ctw);
you must use a light theme with your alertdialog builder such as the THEME_HOLO_LIGHT.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this,AlertDialog.THEME_HOLO_LIGHT);

Appcompat Alert Dialog Action button background On pressed state

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>

Changing AlertDialog button background

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.

Categories

Resources