android fragment inside dialog
android, android-fragments
Solution
Usually you directly use DialogFragment which name is self explained.
here is an example of my code with an `int` send as arg.
So basically you create a `DialogFragment` which extends `DialogFragment`. You have to write `newInstance` and `onCreateDialog` methods. Then you create a new instance of that 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 i.e :
//tv = (TextView) view.findViewById(R.id.yourTextView);
//tv.setText("some text")
return view;
}
}
calling the dialog fragment is done from another fragment by doing this. Note that the value `0` is the int I send.
YourDialogFragment yourDialogFragment = YourDialogFragment.newInstance(0);
YourDialogFragment.show(getFragmentManager().beginTransaction(), "DialogFragment");
In your case if you don't need to pass anything, remove the corresponding lines in the DialogFragment and don't pass any value in the `YourDialogFragment.newInstance()`
EDIT/FOLLOW
Not sure to really understand your question. If you simply need to replace a fragment by another you use
getFragmentManager().beginTransaction().replace(R.id.your_fragment_container, new YourFragment()).commit();
Problem
I have an issue where I need to show a `fragment` in a `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> ``` what I want is to replace `marchecharts` by 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(); ```