Dialog do not show positive and negative button

2019-01-25 04:01发布

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>

6条回答
倾城 Initia
2楼-- · 2019-01-25 04:29

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();
查看更多
三岁会撩人
3楼-- · 2019-01-25 04:31

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.

查看更多
Summer. ? 凉城
4楼-- · 2019-01-25 04:33

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.

查看更多
在下西门庆
5楼-- · 2019-01-25 04:33

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();
}
查看更多
别忘想泡老子
6楼-- · 2019-01-25 04:43

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);
查看更多
ゆ 、 Hurt°
7楼-- · 2019-01-25 04:52

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>
查看更多
登录 后发表回答