Android align center or align right title and message in MaterialDialog

1.5k Views Asked by At

i use MaterialDialog version 3.1.1 in this address.

i want align right or center title and message in dialog but can't find how to do this in doc.

i check this page https://github.com/afollestad/material-dialogs/issues/434 and somebody use MaterialDialog.Builder like this code:

new MaterialDialog.Builder(MainActivity.this)
                    .titleGravity(GravityEnum.END)
                    .contentGravity(GravityEnum.END)
                    .title("چقدر عجله داری بابا!")
                    .content("این ویژگی در نسخه‌ی بعدی فعال خواهد شد! برو بعدن بیا!")
                    .positiveText("باشه. :(")
                    .negativeText("چه بهد")
                    .typeface("iran_sans_bold","iran_sans")
                    .callback(new MaterialDialog.ButtonCallback() {
                        @Override
                        public void onPositive(MaterialDialog dialog) {
                            Log.wtf("+","shod");
                        }

                        @Override
                        public void onNegative(MaterialDialog dialog) {
                            Log.wtf("-","shod");
                        }
                    })
                    .show();

but it seams builder remove in this version. how can i do this?

Update 1: MaterialDialog support change layoutDirection base on android system language but i have different situation and i want change layoutDirection base on content

1

There are 1 best solutions below

4
On

You cannot do it with default Dialog. But obviously, you can start with your own layout on the dialog.

Custom AlertDialog

This full example includes passing data back to the Activity.

enter image description here

Create a custom layout

A layout with an EditText is used for this simple example, but you can replace it with anything you like.

custom_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:paddingLeft="20dp"
              android:paddingRight="20dp"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

Use the dialog in code

The key parts are

  • using setView to assign the custom layout to the AlertDialog.Builder
  • sending any data back to the activity when a dialog button is clicked.

This is the full code from the example project shown in the image above:

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void showAlertDialogButtonClicked(View view) {

        // create an alert builder
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Name");

        // set the custom layout
        final View customLayout = getLayoutInflater().inflate(R.layout.custom_layout, null);
        builder.setView(customLayout);

        // add a button
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // send data from the AlertDialog to the Activity
                EditText editText = customLayout.findViewById(R.id.editText);
                sendDialogDataToActivity(editText.getText().toString());
            }
        });

        // create and show the alert dialog
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    // do something with the data coming from the AlertDialog
    private void sendDialogDataToActivity(String data) {
        Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
    }
}

Notes

  • If you find yourself using this in multiple places, then consider making a DialogFragment subclass as is described in the documentation.

See also