2

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

peyman
  • 143
  • 1
  • 9
  • Use the [MaterialAlertDialogBuilder](https://github.com/material-components/material-components-android/blob/master/docs/components/Dialog.md) in Material Components library – Gabriele Mariotti Oct 18 '19 at 19:35
  • i only need align text to center or in right side. because this library has very good feature, If it done, it'll be great – peyman Oct 18 '19 at 20:09

1 Answers1

1

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

GensaGames
  • 5,538
  • 4
  • 24
  • 53
  • I know I can do this on Android without any library, but I want to do it using the library I mentioned(for mentioned library features ). – peyman Oct 19 '19 at 09:03
  • @peyman It's the Dialog. What features should be here?) You can pass in same way custom layout to the Material Dialog. – GensaGames Oct 19 '19 at 17:46
  • see [this](https://github.com/afollestad/material-dialogs/blob/master/documentation/CORE.md). also i use `Files` and `Color` library in `MaterialDialog` in [@afollestad](https://github.com/afollestad/material-dialogs) github. @afollestad (the creator of MaterialDialog) done layout direction base on system language(i think this is very bad) but my app must show direction base on content(imagine a language tutorial app). i have no time for create my own design and temproraly done it with change manifest to `android:supportsRtl="false"` and `tools:replace="android:supportsRtl"`. – peyman Oct 20 '19 at 15:31
  • Continue previous comment: i think i do it in next version of my app :) – peyman Oct 20 '19 at 15:35