Fragment of an android inside a dialogue

I have a problem when I need to show fragment in android.app.Dialog

here is the xml code

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/marchecharts" android:layout_width="match_parent" android:layout_height="match_parent" > </FrameLayout> </LinearLayout> 

I want to replace marchecharts my fragment, can anyone help

thanks

 Dialog dialog = new Dialog(getActivity()); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.marche_charts_parent); //this is the part I think I need Fragment fragment = new MarcheChartsFragment(); FragmentTransaction ft = ((FragmentActivity) dialog.getOwnerActivity()).getFragmentManager().beginTransaction(); ft.replace(R.id.marchecharts, fragment); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); ft.addToBackStack(null); ft.commit(); dialog.setCanceledOnTouchOutside(true); dialog.getWindow().setLayout(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT); dialog.show(); 
+6
source share
1 answer

Usually you directly use DialogFragment , whose name is explained independently.

here is an example of my code with int send as arg.

So basically you create a DialogFragment that extends DialogFragment . You must write the newInstance and onCreateDialog . Then you create a new instance of this fragment in the calling fragment.

 public class YourDialogFragment extends DialogFragment { public static YourDialogFragment newInstance(int myIndex) { YourDialogFragment yourDialogFragment = new YourDialogFragment(); //example of passing args Bundle args = new Bundle(); args.putInt("anIntToSend", myIndex); yourDialogFragment.setArguments(args); return yourDialogFragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //read the int from args int myInteger = getArguments().getInt("anIntToSend"); View view = inflater.inflate(R.layout.your_layout, null); //here read the different parts of your layout ie : //tv = (TextView) view.findViewById(R.id.yourTextView); //tv.setText("some text") return view; } } 

A call to a dialog fragment is performed from another fragment, doing this. Note that the value 0 is int i send.

 YourDialogFragment yourDialogFragment = YourDialogFragment.newInstance(0); YourDialogFragment.show(getFragmentManager().beginTransaction(), "DialogFragment"); 

In your case, if you do not need to skip anything, delete the corresponding lines in the DialogFragment dialog box and do not pass any value to YourDialogFragment.newInstance()

EDIT / NEXT

Not sure to really understand your question. If you just need to replace the fragment with another, you use

 getFragmentManager().beginTransaction().replace(R.id.your_fragment_container, new YourFragment()).commit(); 
+8
source

Source: https://habr.com/ru/post/958846/


All Articles