Material-dialogs: CustomView initial EditText

Created on 2 Feb 2015  路  4Comments  路  Source: afollestad/material-dialogs

I have following standard custom view. Can't initialize R.id.text value before dialog is shown.

boolean wrapInScrollView = true;
new MaterialDialog.Builder(this)
        .title(R.string.title)
        .customView(R.layout.custom_view_edittext, wrapInScrollView)
        .positiveText(R.string.positive)
        .build()
        .show();

custom_view_edittext.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:text=""/>
</LinearLayout>

Most helpful comment

Just updated your description to apply syntax highlighting and formatting to your code.

You can use something similar to this:

MaterialDialog dialog = new MaterialDialog.Builder(this)
        .title(R.string.title)
        .customView(R.layout.custom_view_edittext, true)
        .positiveText(R.string.positive)
        .build();

EditText text = (EditText) dialog.getCustomView();
text.setText("Hi!");

dialog.show();

All 4 comments

Just updated your description to apply syntax highlighting and formatting to your code.

You can use something similar to this:

MaterialDialog dialog = new MaterialDialog.Builder(this)
        .title(R.string.title)
        .customView(R.layout.custom_view_edittext, true)
        .positiveText(R.string.positive)
        .build();

EditText text = (EditText) dialog.getCustomView();
text.setText("Hi!");

dialog.show();

Also, if you don't need an instance of a MaterialDialog like you do here, you can skip using build() and just go right to show().

Great! big thanks!

why this force close?

                   MaterialDialog mDialog = new MaterialDialog.Builder(getActivity())
                            .title("Cancel order")
                            .customView(R.layout.dialog_cancel_order, true)
                            .positiveText("SUBMIT")
                            .negativeText("CANCEL")
                            .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which)
                            {

                            }
                            })
                            .onNegative(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                dialog.dismiss();
                            }
                            })
                            .build();

                           EditText etId = (EditText)mDialog.getCustomView();
                           etId.setText("How");

           mDialog.show();
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Supercaly picture Supercaly  路  3Comments

sarthak1996 picture sarthak1996  路  6Comments

imcloud picture imcloud  路  5Comments

RezaOruji picture RezaOruji  路  3Comments

pixelbendr picture pixelbendr  路  5Comments