Search code examples
javaandroidandroid-dialogfragment

EditTexts are always empty when clicking OK


When I click OK the Strings returned are always empty strings "". They aren't, could the problem be that the dialog is created twice so they're not referring to the same EditTexts?

package com.example.gaetano.notebook;


public class AddNoteFragment extends DialogFragment`enter code here`{

public EditText title;
public EditText note;
public OnNoteAdded noteAdded;

public interface OnNoteAdded {
    void onNoteAdded(Note note);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    noteAdded = (OnNoteAdded) activity;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();
    View rootView = inflater.inflate(R.layout.fragment_add_note, null);

    title = (EditText) rootView.findViewById(R.id.edTitle);
    note = (EditText) rootView.findViewById(R.id.edNote);

    builder.setTitle("Add a new note");
    builder.setView(inflater.inflate(R.layout.fragment_add_note, null))
            .setPositiveButton(R.string.add_note, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    String t = title.getText().toString();
                    String n = note.getText().toString();

                    Note note = new Note(t, n , false);
                    noteAdded.onNoteAdded(note);
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    AddNoteFragment.this.getDialog().cancel();
                }
            });
    return builder.create();
}

When clicking OK the EditTexts are always empty "".

View rootView = inflater.inflate(R.layout.fragment_add_note, null);

title = (EditText) rootView.findViewById(R.id.edTitle);
note = (EditText) rootView.findViewById(R.id.edNote);

Then in my setPositiveButton, OnClick method I get the Strings of these texts. By doing this String t = title.getText().toString(); String n = note.getText().toString(); They always return ""

In the MainActivity I then implement the interface like this @Override public void onNoteAdded(Note note) { noteAdapter.add(note); }


Solution

  • According to Layout Inflater API, Inflater's inflate() method returns a View object. You can hold the reference of this View object in a variable, which will act as a root view for the Dialog's screen.Using this root view, you can find all the child views contained in this root view's tree or simply, which are children of this root view using findViewById().

    Here is the snippet for onCreateDialog():-

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View rootView=inflater.inflate(R.layout.fragment_add_note, null)
    Button mydialogbutton=(Button) rootView.findViewById(R.id.my_dialog_button);
                //Button is exemplary but this is how you can access any child component of the root view
    
    builder.setTitle("Add a new note");
    builder.setView(rootView)
    .setPositiveButton(R.string.add_note, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            Note newNote = new Note(
                "hey",
                "notehey",
                false);
            noteAdded.onNoteAdded(newNote);
        }
    })
    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            AddNoteFragment.this.getDialog().cancel();
        }
    });
    return builder.create();
    

    The process to create and reference the view is same to that of onCreateView(). As in both onCreateDialog() and onCreateView(), you are using Layout Inflater to get a reference to the View object.