How to get value of material dialog box input field in Android?

I have a material alert dialog box like this:

enter image description here

Here is the java code for the dialog box:

public void openAddNoteDialog() {

    MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(MainActivity.this, R.style.dialogBoxTheme);
    builder.setTitle("Add note");
    builder.setBackground(getResources().getDrawable(R.drawable.dialog_box, null));
    builder.setIcon(R.drawable.ic_add_note);
    builder.setView(R.layout.addnote_dialog);
    builder.setPositiveButton("Add", ((dialogInterface, i) -> handleAddNote()));
    builder.setNegativeButton("Cancel", (dialogInterface, i) -> dialogInterface.dismiss());
    builder.show();

}

And here is the layout that is in the dialog box:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/notenameFLD"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="260dp"
    android:layout_height="90dp"
    android:layout_gravity="center"
    android:layout_marginTop="25dp"
    android:hint="@string/notename"
    android:textColorHint="@color/white"
    app:boxStrokeColor="@color/white"
    app:counterEnabled="true"
    app:counterMaxLength="20"
    app:counterTextColor="@color/white"
    app:helperTextTextColor="@color/white"
    app:hintTextColor="@color/white">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/notenameEDITFLD"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="@font/opensans_regular"
        android:maxLength="20"
        android:textColor="@color/white"
        android:textSize="20sp" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/notecontentFLD"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="260dp"
    android:layout_height="90dp"
    android:layout_gravity="center"
    android:layout_marginBottom="25dp"
    android:hint="@string/notecontent"
    android:textColorHint="@color/white"
    app:boxStrokeColor="@color/white"
    app:counterEnabled="true"
    app:counterMaxLength="250"
    app:counterTextColor="@color/white"
    app:helperTextTextColor="@color/white"
    app:hintTextColor="@color/white">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/notecontentEDITFLD"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="@font/opensans_regular"
        android:maxLength="250"
        android:textColor="@color/white"
        android:textSize="20sp" />
</com.google.android.material.textfield.TextInputLayout>

Now, I’m trying to get the value of these input fields in other function called handleAddNote().

final TextInputEditText noteNameFLD = findViewById(R.id.notenameEDITFLD);
final TextInputEditText noteContentFLD = findViewById(R.id.notecontentEDITFLD);

The problem is, when I’m trying to get the values of these input fields as strings, they return nothing. What is the problem here? Is this because the findViewById() can’t find the fields as they are inside of the alert dialog box and not in the actual activity?

Ps. sorry if this is confusing, I can add more info if needed.

Answer

Is this because the findViewById() can’t find the fields as they are inside of the alert dialog box and not in the actual activity?

Yes, you are correct 🙂 you need to get them on the view you set to your dialogBuilder. Change the way you set your dialog view to:

final View dialogView = LayoutInflater.from(context).inflate(R.layout. addnote_dialog, null);
alertDialogBuilder.setView(dialogView);

Then, you can find the views using dialogView.findViewById().