Home > Software engineering >  open dialog fragment in Fragment
open dialog fragment in Fragment

Time:08-09

I have DialogFragment within just some text and I want open it in Fragment

It's Dialog Fragment first

public class subscribe_dialog_frag extends DialogFragment {

    @Override
    @Nullable
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
        super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.subscribe_dialog_fragment, container, false);


    }
}

and it's my fragment code

public class Main4Fragment extends Fragment {

    private ImageButton btn_money;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main4, container, false);

btn_money = (ImageButton)view.findViewById(R.id.btn_momey);

        btn_money.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Fragment subscribe_dialog_frag = new subscribe_dialog_frag();
                FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
                transaction.replace(R.id.btn_momey, subscribe_dialog_frag).commit();
            }
        });

        return view;
    }
}

I just open the dialogFragment.

possible?? or i do another way?

CodePudding user response:

first use naming convention for classes/variables... Rename your dialog to:

  public class SubscribeDialog extends DialogFragment {

    @Override
    @Nullable
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
        super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.subscribe_dialog_fragment, container, false);


    }
}

in your Main4Fragment call something like this:

btn_money.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
    SubscribeDialog subscribeDialog = SubscribeDialog();
    subscribeDialog.show(getChildFragmentManager(),"SubscribeDialog");
   }
});
  • Related