7

I am doing task in which I need to show a dialog after clicking on EditText. In that dialog I show contents with RadioButtons using RecyclerView.

Now, what I want to do is, after selecting RadioButton (content which is in the RecyclerView) from dialog, it should return value of that content and then dialog should be dismissed.

To generate a dialog I've used a DialogFragment.

As I'm new in android development, I am totally confused and unable to find the solution.

Marko
  • 20,385
  • 13
  • 48
  • 64
Sanket Ranaware
  • 603
  • 2
  • 6
  • 13

1 Answers1

35

Because your dialog is a DialogFragment you can use two things

  1. If you are starting the dialog from a Activity, you can use an interface
  • create an interface

    public interface ISelectedData {
        void onSelectedData(String string);
    }
    
  • implement an interface in your Activity

    public class MyActivity implements ISelectedData {
    
        .....
    
        @Override 
        public void onSelectedData(String myValue) {
            // Use the returned value
        }
    }
    
  • in your Dialog, attach the interface to your Activity

    private ISelectedData mCallback;
    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    
        try {
            mCallback = (ISelectedData) activity;
        }
        catch (ClassCastException e) {
            Log.d("MyDialog", "Activity doesn't implement the ISelectedData interface");
        }
    }
    
  • when returning the value to Activity, just call in your Dialog

    mCallback.onSelectedData("myValue");
    

    Check the example on the Android developer site.

  1. If you are starting the dialog from a Fragment, you can use setTargetFragment(...)
  • starting the Dialog

    MyDialog dialog = new MyDialog();
    dialog.setTargetFragment(this, Constants.DIALOG_REQUEST_CODE);
    dialog.show(fragmentManager, Constants.DIALOG);
    
  • returning the value from the Dialog

    Bundle bundle = new Bundle();
    bundle.putString(Constants.MY_VALUE, "MyValue");
    
    Intent intent = new Intent().putExtras(bundle);
    
    getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent);
    
    dismiss();
    
  • getting the value in Fragment

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        if (requestCode == Constants.DIALOG_REQUEST_CODE) {
    
            if (resultCode == Activity.RESULT_OK) {
    
                if (data.getExtras().containsKey(Constants.MY_VALUE)) {
    
                    String myValue = data.getExtras().getString(Constants.MY_VALUE);
    
                    // Use the returned value
                }
            }
        }
    }     
    
Marko
  • 20,385
  • 13
  • 48
  • 64
  • And How can I set click event to the contents of the recyclerview which is within that dialog, so that on that click event i get values and set to the Edittextview.?? – Sanket Ranaware Dec 30 '15 at 10:01
  • If you need a click listener to your RecyclerView's row you can use something like [this](http://stackoverflow.com/questions/24885223/why-doesnt-recyclerview-have-onitemclicklistener-and-how-recyclerview-is-dif). Similar set listeners to views inside the row or use the answer with 277 upvotes [here](http://stackoverflow.com/questions/24471109/recyclerview-onclick). – Marko Dec 30 '15 at 10:05
  • Thanks buddy, I will try this solutions...@Marko – Sanket Ranaware Dec 30 '15 at 10:14
  • One more thing, your code for getting values, should I add it to onClick event ? – Sanket Ranaware Dec 30 '15 at 10:23
  • Which example, the second one? You have to override the `onActivityResult` method in your `Fragment` and the value will be automaticaly returned. If you want to return the value in onClick, you have to call `getTargetFragment().onActivityResult()...` to return the value. – Marko Dec 30 '15 at 10:42
  • @Marco Once,I clicked textView in recyclerView it should close the dialog. Here,if I put dismiss() in onClick() event, It gives me NullPointerException. – Sanket Ranaware Dec 30 '15 at 11:33
  • @PeterParker I do not know what happens because I do not have / see your code. Step through the code with a debugger to see what is happening. – Marko Dec 30 '15 at 12:23
  • I already waste 2 days on this but now your way save my more time on it. Thanks alot – androidXP Dec 23 '17 at 00:37
  • I'm a little confused: if we could trigger fragment `onActivityResult`, then why don't we also use it in first situation. like `if (getActivity() != null) getActivity().onActivityResult( /*...*/ )` ? – Samuel T. Chou May 27 '21 at 09:37
  • Ah, I see -- the `FragmentActivity.onActivityResult` method is protected, so you cannot call that from a (Dialog)Fragment. – Samuel T. Chou May 27 '21 at 09:40