Haddad
Haddad

Reputation: 307

How to access EditText data in DialogFragment

My question is how to get data that users input into my EditText then show the data by using Toast.makeTest method.

I don't know how to access to the EditText in my fragment and this link didn't worked for me: getText from a EditText in a DialogFragment

This is my code:

public class FragmentDialog extends DialogFragment {
EditText entry;
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)

 {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(R.layout.alert_dialog).setTitle("Add your text").setMessage("HElllloooooooo").setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            Toast.makeText(getActivity(), entry.getText().toString(), Toast.LENGTH_SHORT).show();
        }
    }).setPositiveButton("ADD", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            Toast.makeText(getActivity(),  entry.getText().toString(), Toast.LENGTH_SHORT).show();
        }
    });
    return builder.create();
}
}

Upvotes: 0

Views: 2037

Answers (2)

Badran
Badran

Reputation: 535

How Edit Text Works:

First you should bind it:

entry =  view.findViewById(R.id.MyEdtText);

Second To access the written data(text) in the edit you should call:

entry.getText().toString()

To Show the written text in Toast you have to:

if(TextUtils.isEmpty(entry.getText().toString()){
     Toast.makeText(getActivity(), "No Text Entered", Toast.LENGTH_SHORT).show();
}
else{
    Toast.makeText(getActivity(), "The Text Entered Is"+  entry.getText().toString(), Toast.LENGTH_SHORT).show();
}

The specific solution for your case is:

    public class FragmentDialog extends DialogFragment {
   private EditText entry;



    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)

    {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();

        View DialogView = inflater.inflate(R.layout.alert_dialog,null);

        entry = DialogView.findViewById(R.id.entryEditText); // replace it with the correct XML ID

        builder.setView(DialogView).setTitle("Add your text").setMessage("HElllloooooooo").setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(getActivity(), "canceled operation", Toast.LENGTH_SHORT).show();
            }
        }).setPositiveButton("ADD", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(getActivity(), "the edit text value: "+entry.getText().toString(), Toast.LENGTH_SHORT).show();
            }
        });
        return builder.create();
    }
}

And to show the dialog , call in your activity the following code:

FragmentDialog dialog = new FragmentDialog();
dialog.show(getSupportFragmentManager(), "YourDialog");

Proof of concept(See how it works):

blob:https://imgur.com/1c2f5429-d11f-4f5e-b276-7b7fb84abaf8

Upvotes: 2

Xay
Xay

Reputation: 248

You have to findViewbyId on onCreateView like below

  public class FragmentDialog extends DialogFragment {
    EditText entry;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_username, container);
          entry = (EditText) view.findViewById(R.id.username);
          mEditText.requestFocus();

          return view;
        }}

Upvotes: 0

Related Questions