0

[ How to take text input with DialogFragment in Android? ] here in this question same as mine we bring out the text from the editText in the MainActivity .

 @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    listener = (AddcustomerListener) getActivity();
    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    final LayoutInflater inflater = getActivity().getLayoutInflater();
    view = inflater.inflate(R.layout.add_customer_dialog, null);
    builder.setView(view)
            .setPositiveButton("save", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                   /////////////here////////////

                    EditText user = view.findViewById(R.id.username);
                    Log.i( "sad",user.getText().toString());
                    //listener.onDialogAddCustomer(AddCustomerDialog.this);
                }
            })
            .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                }
            });
    return builder.create();
}

I used the approach we use in a ViewHolder of a RecyclerView adapter to access the elements . I had multiple editTexts and was trying to work around , my code works but is there any harm in using the builder's view this way to access the edit text . Still learning :)

Gautam Surani
  • 1,136
  • 10
  • 21
  • You want to get Input Text values from `DialogFragment` to your `Fragment` when your `DialogFragment` is closed. Is this what you want? – Varad Mondkar Aug 01 '18 at 07:25
  • Create interface and pass data into the interested party from your DialogFragment. Don't directly refer to another Fragment by findviewbyid – Sreehari Aug 01 '18 at 07:52
  • @Sreehari why not ? where else do I access the editText and how ? I have an Interface available below the very concerned line of code . – satyam singhal Aug 01 '18 at 08:31

0 Answers0