I did a DialogFragment with my layout .

  AlertDialog.Builder adb = new AlertDialog.Builder(getActivity()) .setView(layout) .setCancelable(true); 

So in this layout which is set in DialogFragment there is an EditText . The bottom line is that when a DialogFragment appears DialogFragment user must enter a comment into EditText and click the send button.

There was a problem with the fact that I could not perform findViewById() because DialogFragment does not inherit from Activity , but I sort of solved this problem in this way

 LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.activity_note_dialog, null); EditText etNoteFromWeb = (EditText)view.findViewById(R.id.etNoteFromWeb); String response = etNoteFromWeb.getText().toString(); 

And there seems to be no mistakes and everything is OK, but with this approach, it always returns "" not null namely the empty line ...

Tell me how to get the text from EditText .

What am I doing wrong?

Here is the full class code

 public class MyDialog extends DialogFragment { final String LOG_TAG = "MyDialog"; private static MyDialog myDialog; private EditText etNoteFromWeb; int layout; @Override public void onCreate(Bundle savedState) { super.onCreate(savedState); Bundle args = this.getArguments(); layout = args.getInt("layout"); } public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder adb = new AlertDialog.Builder(getActivity()) .setView(layout) .setCancelable(true); if (layout == R.layout.activity_note_dialog) { LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(layout, null); LinearLayout llSent = (LinearLayout) view.findViewById(R.id.llSent); LinearLayout llSkip = (LinearLayout) view.findViewById(R.id.llSkip); llSent.setOnClickListener(listener); llSkip.setOnClickListener(listener); adb.setView(view); } return adb.create(); } View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { if (layout == R.layout.activity_note_dialog) { LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.activity_note_dialog, null); etNoteFromWeb = (EditText) view.findViewById(R.id.etNoteFromWeb); String response = etNoteFromWeb.getText().toString(); // String response = "etNoteFromWeb.getText().toString()"; // Здесь в System.out.println ничего нет...9(( но когда раскоментирована строка // выше и указываю статический текст, то все работает, проблема именно в том, // что не достает текст из EditText System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! " + response); switch (v.getId()) { case R.id.llSent: onClick.onDialogClickListener(response); myDialog.dismiss(); break; case R.id.llSkip: onClick.onDialogClickListener(null); myDialog.dismiss(); break; } } } }; public void dismissDialog(final MyDialog dialog, int daleyTime) { Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { try { dialog.dismiss(); } catch (IllegalStateException e) { e.printStackTrace(); } } }, daleyTime); } public void onDismiss(DialogInterface dialog) { super.onDismiss(dialog); Log.d(LOG_TAG, "MyDialog: onDismiss"); } public void onCancel(DialogInterface dialog) { super.onCancel(dialog); Log.d(LOG_TAG, "MyDialog: onCancel"); } public static MyDialog newInstance(int layout) { myDialog = new MyDialog(); Bundle args = new Bundle(); args.putInt("layout", layout); myDialog.setArguments(args); return myDialog; } // Этот интерфейс я реализовал для того чтоб можно было передавать данные из DialogFragment public interface OnButtonClick { void onDialogClickListener(String response); } OnButtonClick onClick; @Override public void onAttach(Activity activity) { super.onAttach(activity); try { onClick = (OnButtonClick) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement onDialogClickListener"); } } @Override public void onDetach() { super.onDetach(); onClick = null; } } 
  • one
    These are two different editText-a, one in the dialogue the other in the view. - Kota1921
  • @nekaneka What do you mean? And how can this be fixed? - Aleksey Timoshchenko

2 answers 2

At the class level, declare a variable:

 EditText et; 

Initialize it when creating a dialog, but before displaying it:

 AlertDialog.Builder adb = new AlertDialog.Builder(getActivity()) .setView(layout) .setCancelable(true); Dialog d = builder.build(); et = d.findViewById(R.id.etNoteFromWeb); 

Now you can get to it from other methods while the dialogue exists.

  • I added the code to the question, it seems to me that your answer will not help me as I try to take EditText from the linsector ... The dialogue and that moment was probably already created - Aleksey Timoshchenko
  • @AlekseyTimoshchenko, bring EditText et into the class variable and it will work. - Yuriy SPb
  • Updated code, EditText rendered up as a global variable, but nothing has changed ... - Aleksey Timoshchenko
  • @AlekseyTimoshchenko, you are not using the same EditText. Inflater loads the markup but it is not the one you use in the dialog. When creating a dialog, initialize as in my response. The inflater is not needed in the trap. - Yuriy SPb
  • one
    All figured out, the answer was written ... Thanks for the help! - Aleksey Timoshchenko

In the end, as I understood, the point was that I created a new layout object and tried to get a string from its EditText . It was necessary to declare EditText as a global variable and initialize it here like this:

public Dialog onCreateDialog (Bundle savedInstanceState) {

  AlertDialog.Builder adb = new AlertDialog.Builder(getActivity()) .setView(layout) .setCancelable(true); if (layout == R.layout.activity_note_dialog) { LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(layout, null); -------> etNoteFromWeb = (EditText) view.findViewById(R.id.etNoteFromWeb); LinearLayout llSent = (LinearLayout) view.findViewById(R.id.llSent); LinearLayout llSkip = (LinearLayout) view.findViewById(R.id.llSkip); llSent.setOnClickListener(listener); llSkip.setOnClickListener(listener); adb.setView(view); } return adb.create(); } 

And it all worked