Center message in android dialog box

前端 未结 8 607
野趣味
野趣味 2020-11-29 01:53

I want the message text within my dialog box to be center aligned.

相关标签:
8条回答
  • 2020-11-29 02:07

    Create your own TextView object and then supply it to popup builder as View:

    AlertDialog.Builder popupBuilder = new AlertDialog.Builder(this);
    TextView myMsg = new TextView(this);
    myMsg.setText("Central");
    myMsg.setGravity(Gravity.CENTER_HORIZONTAL);
    popupBuilder.setView(myMsg);
    

    You can control all other text parameters (style, color, size ...). To control margins you may programatically create LinearLayout, set LayoutParams, and then put TextView into it.

    0 讨论(0)
  • 2020-11-29 02:07

    Just use that method and your dialog title and message will appear at center:

    public static void openDialog(Context context, String message) {

        TextView title = new TextView(context);
        // You Can Customise your Title here
        title.setText("Information Message");
        title.setBackgroundColor(Color.BLACK);
        title.setPadding(10, 15, 15, 10);
        title.setGravity(Gravity.CENTER);
        title.setTextColor(Color.WHITE);
        title.setTextSize(22);
    
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setCustomTitle(title);
        alertDialog.setMessage(message);
    
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
    
            }
        });
        alertDialog.show();
    
        // You Can Customise your Message here
        TextView messageView = (TextView) alertDialog
                .findViewById(android.R.id.message);
        messageView.setGravity(Gravity.CENTER);
    
    }
    
    0 讨论(0)
  • 2020-11-29 02:08

    you can try this code

    public void Info(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Info Aplication");
        builder.setIcon(R.drawable.info_adp);
        builder.setMessage("Jakarta Hospital");
        builder.setCancelable(false);
        builder.setPositiveButton("Exit", null);
        AlertDialog dialog = builder.show();
        TextView messageView = (TextView)dialog.findViewByI(android.R.id.message);
        messageView.setGravity(Gravity.CENTER);
    }
    
    0 讨论(0)
  • 2020-11-29 02:12

    try this in your TextView:

    android:gravity = "center_vertical|center"
    
    0 讨论(0)
  • 2020-11-29 02:14

    without using textview. worked for me.

    styles.xml

    <style name="CustomAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
            <item name="android:gravity">center</item>
        </style>
    

    Alertdialog

    final AlertDialog builder = new AlertDialog.Builder(activity, 
    R.style.CustomAlertDialog)
    
    0 讨论(0)
  • 2020-11-29 02:16

    we can use as like.

    public static void showAlert(Activity activity, String message) {
      TextView title = new TextView(activity);
    
      title.setText("Your Title Here");
      title.setPadding(10, 10, 10, 10);
      title.setGravity(Gravity.CENTER);
      title.setTextColor(Color.WHITE);
      title.setTextSize(20);
    
      AlertDialog.Builder builder = new AlertDialog.Builder(activity);
      builder.setCustomTitle(title);
      builder.setMessage(message);
      builder.setCancelable(false);
      builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
          dialog.cancel();
        }
      });
    
      AlertDialog alert = builder.show();
      TextView messageText = (TextView)alert.findViewById(android.R.id.message);
      messageText.setGravity(Gravity.CENTER);
      messageText.setTextColor(Color.RED);
    }
    
    0 讨论(0)
提交回复
热议问题