Android findViewById in DialogFragment

To get a specific view in Fragment / Fragment dialog you should use onCreateView(). Here is an example how to do that :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // R.layout.my_layout - that's the layout where your textview is placed
    View view = inflater.inflate(R.layout.my_layout, container, false);
    TextView mTextView = (TextView) view.findViewById(R.id.colors);
    // you can use your textview.
    return view;
}

Check first if onCreateDialog() is called before onCreateView() and if that's true try using something like this :

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout. activity_register_screen, null);
    builder.setView(view);
    // Create the AlertDialog object and return it
    return builder.create();
}

There is more simple way to find views in DialogFragment.

@Override
public void onStart() {
    super.onStart();
    mTextView = (TextView) getDialog().findViewById(R.id.tvDate);
}

Tags:

Java

Android