Search code examples
javaandroidandroid-recyclerviewandroid-dialogfragment

Android: open dialog on adapter


How do I open a DialogFragment inside a RecyclerView Adapter, or how do I do it through the main activity? I need to open it by clicking on every view that is added into the RecyclerView.


Solution

  • Set this on the DialogFragment:

     public static myFragment newInstance() {
        return new myFragment();
    }
    

    Set this on the adapter:

    public Activity mcontext;
    
    public SubjectsAdapter(Activity context) {
    // Here we're getting the activity's context, 
    // by setting the adapter on the activity with (this)   
    this.mcontext=context;
    
    }
    

    Show it in through the adapter like this:

     myFragment newFragment = myFragment.newInstance();
     newFragment.show(mcontext.getFragmentManager(), "Title");