1

I'm using AlertDialog with 1 EditText. I want to know how i can send String from AlertDialog to Fragment.

Please check my code below.

 @Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.action_datasource){
        enterdatasourceId();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

The code for enterdatasourceId is:

 public void enterdatasourceId() {
    LayoutInflater inflater = LayoutInflater.from(this);
    final View dialogView = inflater.inflate(R.layout.cutom_dialog,null);
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

    dialogBuilder.setTitle("Enter you unique ID");
    dialogBuilder.setCancelable(false);
    dialogBuilder.setView(dialogView);

    final EditText datasource1 = (EditText) dialogView.findViewById(R.id.datasource_id);

    dialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            String dsource = datasource1.getText().toString().trim();
            Intent intent = new Intent();
            intent.putExtra("Datasource",dsource);
        }
    });
    dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.cancel();
        }
    });
    AlertDialog customDialog = dialogBuilder.create();
    customDialog.show();
}

The following is the code of the fragment where i want to receive that String

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    Datasource = getActivity().getIntent().getStringExtra("Datasource");
}

In Fragment i want to receive the String that i entered in AlertDialog and from here i'm sending that String as a parameter in my other class. I want to make it clear that the Activity where i'm using AlertDialog is dashboad2.java and the fragment where i want to receive that String is within the same Activity (dashboard2.java)

Ajeet Choudhary
  • 1,969
  • 1
  • 17
  • 41

1 Answers1

0

You can create a DialogFragment. Create an interface in the dialogFragment. Implement the interface in your parent fragment.

Here is the sample how you can continue.

public class YourDialogFragment extends DialogFragment {

    OnSubmitListener mListener;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
         Dialog dialog = new Dialog(getActivity());
         dialog.setContentView(R.layout.your_custom_layout);
         dialog.show();

         Button proceed = (Button) dialog.findViewById(R.id.proceedBtn);
         Button cancel = (Button) dialog.findViewById(R.id.cancelBtn);
         EditText datasource1 = (EditText)dialog.findViewById(R.id.datasource_id);

    proceed.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          mListener.GetDataSource(datasource1.getText().toString().trim());
         } 
         dismiss();
        }
    });

    cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismiss();
        }
    });
         return dialog;
    }



public interface OnSubmitListener {
                void GetDataSource(String dataSrc);
            }
}

Implement YourDialogFragment.OnSubmitListener in your fragment. It will make you override GetDataSource(String) method, where you will get the string.

if (id == R.id.action_datasource){
     //        enterdatasourceId();
     YourDialogFragment fragment = new YourDialogFragment ();
     fragment.mListener = getActivity();
     fragment.show((Activity) context).getFragmentManager(), "dialogFragment"); 
     return true;
}
debugger
  • 580
  • 1
  • 6
  • 16
  • But how i can get the string from edittext in dialog fragmnet and pass it to other fragmnet –  Jul 11 '17 at 12:06
  • You are using interface for that. **mListener.GetDataSource(datasource1.getText().toString().trim());** when you press the proceed button in dialogfragment, you will get the string in the edittext on Overridden method in the parent fragment – debugger Jul 11 '17 at 12:08
  • and in other fragment how i will get the passed string –  Jul 11 '17 at 12:11
  • please share some code how i will get the passed string in other fragment –  Jul 11 '17 at 12:13
  • Implement **YourDialogFragment.OnSubmitListener**, it will force you to override the method **GetDataSource(String dataSrc)** .. This string **dataSrc** will be the value of your edittext in the dialogFragment. – debugger Jul 11 '17 at 12:16
  • would you please share some detail code of other fragment where i will override the method please –  Jul 11 '17 at 12:18
  • "how to send String from Alertdialog to **Fragment** " implement on the same Fragment – debugger Jul 11 '17 at 12:26
  • Yes i want to send the string from alert dialog to myfragment –  Jul 11 '17 at 12:30
  • make it like: public class MyFragment extends Fragment implements YourDialogFragment.OnSubmitListener. I hope you already have your myFragment class, so just implement YourDialogFragment.OnSubmitListener interface – debugger Jul 11 '17 at 12:34
  • i'm using your code. i'm getting error on dismiss(); –  Jul 11 '17 at 12:40
  • i have solved that error but now i'm getting error in this code –  Jul 11 '17 at 12:44
  • if (id == R.id.action_datasource){ YourDialogFragment fragment = new YourDialogFragment (); fragment.mListener = getActivity(); fragment.show((Activity) context).getFragmentManager(), "dialogFragment"); return true; } –  Jul 11 '17 at 12:44
  • i put this code in mainactivity where there is options menu. error is mListener is not public –  Jul 11 '17 at 12:46