CauCuKien
CauCuKien

Reputation: 1037

AlertDialog do not show positive and negative button

I used AlertDialog to alert user confirm delete. I check on my device (Android 5.1) and it show well

enter image description here

But on some another device (also run Android 5.1), the dialog missed positive and negative button.

enter image description here

I checked and found that devices happen this issue have a medium resolution (960x540, 854x480).

Is resolution relate with this issue ? If not, can you tell me the reason and how to fix this issue ?

My code for display dialog:

    public static final Dialog yesNoDialog(Context context,
                                               String message,
                                               DialogInterface.OnClickListener yesAction, DialogInterface.OnClickListener noAction) {


            AlertDialog.Builder  builder = new AlertDialog.Builder(context,R.style.todoDialogLight);

            builder.setTitle(context.getString(R.string.app_name))
                    .setMessage(message)
                    .setCancelable(false)
                    .setPositiveButton("YES", yesAction)
                    .setNegativeButton("NO", noAction);
            return builder.create();
 }

And styles.xml

  <style name="todoDialogLight" parent="Theme.AppCompat.Light.Dialog">

            <!-- Used for the buttons -->
            <item name="colorAccent">@color/colorPrimaryDark</item>
            <item name="android:textStyle">bold</item>
            <!-- Used for the title and text -->
            <item name="android:textColorPrimary">@color/colorText</item>
            <!-- Used for the background -->
            <!-- <item name="android:background">#4CAF50</item>-->
            <item name="android:fontFamily">sans-serif</item>
            <item      name="android:windowAnimationStyle">@style/RemindDialogAnimation</item>
            <item name="android:layout_width">@dimen/width_remind_dialog</item>
            <item name="android:layout_height">wrap_content</item>
 </style>

Upvotes: 34

Views: 28336

Answers (11)

Legend SP
Legend SP

Reputation: 11

use this

You can replace MainActivity.this by your activity, using getAplicationContext() will cause your app crashing because it asking for activity instead of context

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); View view = getLayoutInflater().inflate(R.layout.alert_profile_icon, null); builder.setView(view); ImageView imageView = view.findViewById(R.id.profile_pic); imageView.setImageURI(imagePath); builder.setMessage("Your Message"); builder.setCancelable(false); builder.setPositiveButton( "Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { //TODO } });

    builder.setNegativeButton(
            "No",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

    AlertDialog alert = builder.create();
    alert.setOnShowListener(arg0 -> {
        alert.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.grey));
        alert.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
    });
    alert.show();

Upvotes: 0

Shahriar enayaty
Shahriar enayaty

Reputation: 377

I know it's late but maybe someone else need it, change your style to this one MyAlertDialogStyle:

 <style name="MyAlertDialogStyle" parent="Theme.MaterialComponents.Light.Dialog.Alert">
        <!-- Used for the title and text -->
        <item name="android:textColorPrimary">#000000</item>
        <!-- Used for the background -->
        <item name="android:background">#FFFFFF</item>
        <item name="android:windowTitleStyle">@style/MyTitleTextStyle</item>
        <item name="android:buttonBarPositiveButtonStyle">@style/MyPositiveTextStyle</item>
        <item name="android:buttonBarNegativeButtonStyle">@style/MyNegativeTextStyle</item>

 </style>
 <style name="MyTitleTextStyle">
        <item name="android:textColor">#FFEB3B</item>
        <item name="android:textAppearance">@style/TextAppearance.AppCompat.Title</item>
 </style>
 <style name="MyPositiveTextStyle" parent="Widget.MaterialComponents.Button.TextButton">
        <item name="backgroundTint">@color/transparent</item>
        <item name="android:textColor">@color/green</item>
        <item name="android:textAppearance">@style/TextAppearance.AppCompat.Button</item>
 </style>
 <style name="MyNegativeTextStyle"  parent="Widget.MaterialComponents.Button.TextButton">
        <item name="android:textColor">@color/red</item>
 </style>

Upvotes: 2

@Ali answer is correct but doesn't work with the new Design Library

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(it, R.style.AlertDialogTheme)
                    .setMessage("The message")
                    .setPositiveButton("Yes") { _, _ -> /* Do something*/}
                    .setNegativeButton("No") { _, _ -> }
                    .show()

Upvotes: 15

Mohammed Fathi
Mohammed Fathi

Reputation: 1475

this problem can also happen if you call :

dialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        dialogBuilder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        });

after the call to :

dialogBuilder.create();

Upvotes: 3

Guru raj
Guru raj

Reputation: 826

We can set the color to the buttons.

public  void showImageDownloadDailog(Activity activity, String message)
{
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setMessage(message);
    builder.setCancelable(false);
    builder.setPositiveButton(
            "Yes",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                   //TODO
                }
            });

    builder.setNegativeButton(
            "No",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                    //TODO
              }
            });

    AlertDialog alert = builder.create();
    alert.setOnShowListener(arg0 -> {
        alert.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.button_color));
        alert.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.button_color));
    });
    alert.show();
}

Upvotes: 4

Derry
Derry

Reputation: 11

Go to colors.xml and change your colorAccent from this:

<color name="colorAccent">#FFFFFF</color>

to this:

<color name="colorAccent">#FF4081</color>

Upvotes: 1

TharakaNirmana
TharakaNirmana

Reputation: 10353

If you are using a custom theme in your styles.xml set the color of colorAccent to a darker color.

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimary</item>
        <item name="colorAccent">@color/colorPrimary</item>
    </style>

Upvotes: 1

kiran nanda
kiran nanda

Reputation: 31

Dialog dialog;

public void startAlertDialog(String message)
{
    AlertDialog.Builder alertDialog=new AlertDialog.Builder(this);


    alertDialog.setCancelable(false);

    LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
    View view=inflater.inflate(R.layout.alertdialoglayout,null);
    TextViewRagular textViewRagular=(TextViewRagular)view.findViewById(R.id.textviewMessage);
    textViewRagular.setText(message);
    alertDialog.setView(view);
    dialog=alertDialog.create();
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialog.show();
}

Upvotes: 0

T_Dun
T_Dun

Reputation: 101

Ali's solution worked for me. My original code worked on previous android versions <7. But testing on my Pixel gave invisible buttons. I added the style concept detailed by Ali as shown below and all is well:

   return new AlertDialog.Builder(getActivity(),R.style.MyDialogTheme)
            .setView(v)
            .setTitle(R.string.filter_picker_title)
            .setPositiveButton(android.R.string.ok,
                    // when the user presses the button to select a new number
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            Integer markerIndex = mNumberPicker.getValue();
                            stringFilter=uniqueValues[markerIndex];
                            sendResult(Activity.RESULT_OK,  stringFilter);
                        }
                    })
            .create();

Upvotes: 3

Ali
Ali

Reputation: 591

So the buttons are there for me. Unfortunately, they were white text on white background. It has nothing to do with the resolution but more to do with the theme you are choosing. To solve this you need to set the right text color in your dialog theme.

For example, in styles.xml add

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorPrimary">@color/colorPrimaryDarkBlue</item>
</style>

and in your activity add

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MyActivity.this, R.style.MyDialogTheme);

Hope this helps.

Upvotes: 59

CauCuKien
CauCuKien

Reputation: 1037

It is really relate to resolution, I do not know exact the reason and just make a if else condition to fix this issue.

public static String getDensity(Context context) {
        float density = context.getResources().getDisplayMetrics().density;
        if (density >= 4.0) {
            return "xxxhdpi";
        }
        if (density >= 3.0) {
            return "xxhdpi";
        }
        if (density >= 2.0) {
            return "xhdpi";
        }
        if (density >= 1.5) {
            return "hdpi";
        }
        if (density >= 1.0) {
            return "mdpi";
        }
        return "ldpi";
}

AlertDialog

    public static Dialog yesNoDialog(final Context context,
                                               final String message,
                                               final DialogInterface.OnClickListener yesAction,
                                               final DialogInterface.OnClickListener noAction) {
            int theme = PreferenceUtil.getThemeSetting(context, PreferenceUtil.PREF_THEME);
            AlertDialog.Builder builder = null;
            String density = AppUtil.getDensity(context);
            if (theme == ThemeUtil.THEME_LIGHT) {
                if(density.equals("hdpi")){
                    builder = new AlertDialog.Builder(context);
                }else{
                    builder = new AlertDialog.Builder(context, R.style.todoDialogLight);
                }
            } else {
                if(density.equals("hdpi")){
                    builder = new AlertDialog.Builder(context);
                }else{
                    builder = new AlertDialog.Builder(context, R.style.todoDialogDark);
                }
            }
            builder.setTitle(context.getString(R.string.app_name))
                    .setMessage(message)
                    .setCancelable(false)
                    .setPositiveButton("YES", yesAction)
                    .setNegativeButton("NO", noAction);
            return builder.create();
   }

Hope it help for others developer who have the same problem.

Upvotes: 1

Related Questions