1

Basically I have created a DialogFragment that contains 3 buttons and a message. The message is and integer value starting at 0 and the positive and negative buttons allow the user to increment and decrement the value in the message by 1 each time the button is clicked. once the user clicks the neutral button "Ok" I want to know is it possible to get the message as a string and return it back to the main activity? If not how is it possible to do this?

I know how to pass data between a fragment and activity i just want to know is it possible to get the message in a fragment and store it to a string variable

Many thanks

p.s sorry if i'm using this wrong its my first time here

public class AddEmployeesDialogFragment extends DialogFragment {

    //variables
    int value = 0;
    AlertDialog.Builder builder;
    AlertDialog alertDialog;

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

        builder.setTitle("How many Employees do you want to add to");
        builder.setMessage((new Integer(value)).toString());
        builder.setPositiveButton("+", null);
        builder.setNegativeButton("---", null);
        builder.setNeutralButton("Ok", null);

        // Create the AlertDialog object and return it
        return builder.create();
    }

    @Override
    public void onStart(){
        super.onStart();
        alertDialog = (AlertDialog)getDialog();
        if(alertDialog != null)
        {
            Button positiveButton = (Button)alertDialog.getButton(Dialog.BUTTON_POSITIVE);
            positiveButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    value++;
                    alertDialog.setMessage((new Integer(value)).toString());
                }
            });
            Button negativeButton = (Button)alertDialog.getButton(Dialog.BUTTON_NEGATIVE);
            negativeButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(value > 0) {
                        value--;
                    }
                    alertDialog.setMessage((new Integer(value)).toString());
                }
            });
            Button neutralButton = (Button)alertDialog.getButton(Dialog.BUTTON_NEUTRAL);
            neutralButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
    }
}
kyim3297
  • 13
  • 3
  • Possible duplicate of [Passing data between a fragment and its container activity](https://stackoverflow.com/questions/9343241/passing-data-between-a-fragment-and-its-container-activity) – Bö macht Blau Mar 13 '19 at 18:22
  • Hey kyim3297, the linked "original" should have some useful answers for you. (IMO the ones which suggest using an interface are better than the others) – Bö macht Blau Mar 13 '19 at 18:24
  • Hi 0x0nosugar I know how to pass data between an activity however I don't know if it is possible to get the message in a fragment and store it as a string value. Any help on this would be appreciated – kyim3297 Mar 13 '19 at 18:27
  • Then it seems I misunderstood your question. But I'm still not sure what you're looking for: you set `(new Integer(value)).toString()` as the message, so you already seem to have a way of creating a String from *value* (I like `String.valueOf(value)` better, but the result is the same) So is the question mainly about how to "store it as a string value"? – Bö macht Blau Mar 13 '19 at 18:37
  • I apologise, I completely forgot that I can just return the (value) variable back to the activity. I only had my mind set on returning what was stored in the actual message of the dialog (in "AlertDialog.setMessage") i am so stupid sorry for your time – kyim3297 Mar 13 '19 at 18:55
  • Possible duplicate of [Returning String from Dialog Fragment back to Activity](https://stackoverflow.com/questions/15121373/returning-string-from-dialog-fragment-back-to-activity) – Dharman Mar 13 '19 at 23:33

0 Answers0