Alert dialog buttons problems in Android L

2019-04-26 11:25发布

问题:

I have created a AlertDialog in my app. Before Android L AlertDialog buttons fit in dialog box, but in Android L buttons label automatically converts in title case and buttons not fit in dialog box. Please see screenshots: Android L:

Android Kitkat:

Is anybody see this issue. Can any help me to solve this problem, although this is latest android version.

Code: (I have not used xml code to create dialog, here is java code:)

AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle(R.string.feedback_label);
        alert.setMessage(msgStrId);
        alert.setNegativeButton(R.string.close_label, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
            }
        });
        alert.setPositiveButton(R.string.rate_app, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
            }
        });
        alert.setNeutralButton(R.string.feedback_label,new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {
                // TODO Auto-generated method stub
            }
        });
        alert.setOnCancelListener(new DialogInterface.OnCancelListener() 
        {
            @Override
            public void onCancel(DialogInterface dialog) 
            {
                // TODO Auto-generated method stub
            }
        });
        AlertDialog alertDialog = alert.create();
        alertDialog.show();

回答1:

I know, I am too late. But I share my suggestion here. Maybe it will be helpful.

  AlertDialog alertDialog  = alert.create();
    alertDialog .show();
    alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setAllCaps(false);
    alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setAllCaps(false);
    alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL).setAllCaps(false);
 alertDialog.show();


回答2:

I think it will works For You. https://github.com/drakeet/MaterialDialog



回答3:

A slight modification to @Kush's answer. You don't have to call dialog.show() multiple times.

AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
        builder.setTitle("Title");
        builder.setMessage("message");
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //Do Something
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
       dialog.getButton(AlertDialog.BUTTON_POSITIVE).setAllCaps(false);


回答4:

If you will try to find solution for this then you might get but also for small screens you will stuck again and you need to find something for it also.

So better is divide your string so it will resolve your issue for any screen.

For that follow below code.

Go to strings.xml file. and change your title as below.

 <string name="rate_app">Rate This \n App</string>
 <string name="feedback_label">Report \n Issues</string>

If "\n" not work then user
tag. Hope this will help you.



回答5:

Use <item name="android:textAllCaps">false</item> in the definition of your application theme. Then all dialogs in the application are no more uppercase.