pipeur10
pipeur10

Reputation: 73

Display dialogfragment inside Fragment occupying a portion of the screen?

I have been battling with this for some days now.

I have an activity with two fragments A and B.

I want to display a dialog fragment inside Fragment B (only).

Instead the dialog fragment show in the middle of activity (see picture below).

enter image description here

What I want is below

enter image description here

Below is a snippet of my code for FragmentB:

public class FragmentB extends Fragment {


    //...Some code....

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.new_order_create_fragment, container, false);


    }





    void showDialog(int position, Product item){
        MyDialogFragment dialogFragment=new MyDialogFragment();
        dialogFragment.show(getChildFragmentManager(), "MY_DIALOG_FRAG");
    }
    

    //...More Code....

}

PS: showDialog(int position, Product item) is used to show the dilogFragment.

I'm using getChildFragmentManager() But I have also tried getFragmentManager()

This the code for MyDialogFragment:

public class MyDialogFragment extends AppCompatDialogFragment  {

    //...Some code....

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(AppCompatDialogFragment.STYLE_NO_TITLE, R.style.CustomDialog);

    }

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


    //...More code....
}

Thank you.

Upvotes: 1

Views: 671

Answers (1)

ephemient
ephemient

Reputation: 204886

Like other fragments, you can attach a DialogFragment to a particular container. For example, if you had a <FrameLayout android:id="@+id/container"/> in your Fragment B, you can use

void showDialog(int position, Product item) {
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    MyDialogFragment dialogFragment = new MyDialogFragment();
    ft.add(R.id.container, dialogFragment, "MY_DIALOG_FRAG");
    ft.commit();
}

Upvotes: 1

Related Questions