Search code examples
androidviewandroid-dialogfragment

Accessing DialogFragment Views from another activity


I have a class that extends DialogFragment that uses a layout inflater to display different views

public class MyDialog extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater layoutInflater = getActivity().getLayoutInflater();
    builder.setView(layoutInflater.inflate(R.layout.dialog_pin_layout,null))
            .setTitle("Enter info")
            ...
    return builder.create();
}

The layout contains only an EditText :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/infoEdit" />

</RelativeLayout>

Now I'm trying to auto-fill this EditText from my MainActivity

//When the user clicks on the button in the main activity, prompt MyDialog
ImageButton usr = (ImageButton)findViewById(R.id.usrImageButton);

 usr.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new MyDialog().show(getFragmentManager(),"fragment_info");

            //If userInfo exists, fill the DialogFragment's EditText
            if(userInfo!=null){
               EditText txt = (EditText)findViewById(R.id.infoEdit);
               txt.setText(userInfo.toString());
            }
        }
    });

When I run the app, clicking the button throws a NullPointerException caused by : txt.setText(userInfo.toString());. By using the debugger, I found out that (EditText)findViewById(R.id.infoEdit) returns null, I guess that I'm not able to access the EditText View within my MainActivity by using findViewById. Is there a way to do so ?


Solution

  • Your best option is to pass the String to the DialogFragment and set it up there like so:

    public class MyDialog extends DialogFragment {
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
    
        Bundle args = getArguments();
        String text = args.getString("TEXT_TO_SET");
    
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater layoutInflater = getActivity().getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.dialog_pin_layout,null);
        EditText txt = (EditText) view.findViewById(R.id.infoEdit);
        txt.setText(text);
    
        builder.setView(view)
                .setTitle("Enter info")
                ...
        return builder.create();
    }
    

    and when creating the DialogFragment pass it the extra:

     DialogFragment fragment =  new MyDialog();
     Bundle bundle = new Bundle();
     bundle.putString("TEXT_TO_SET",userInfo.toString());
     fragment.setArguments(bundle);
     fragment.show(getFragmentManager(),"fragment_info");