Dialog do not show positive and negative button

狂风中的少年 提交于 2019-12-17 19:32:34

问题


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

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

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>

回答1:


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.




回答2:


Add this in style.xml:

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="android:textColor">@color/colorAccent</item>
</style>

and in activity use

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



回答3:


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();



回答4:


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>



回答5:


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.




回答6:


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();
}



回答7:


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

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

to this:

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



回答8:


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();
}


来源:https://stackoverflow.com/questions/39481735/dialog-do-not-show-positive-and-negative-button

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!