How to switch from fragment to dialog fragment - android

I am using a dialog fragment inside a fragment . Now i want to switch from my root fragment to dialog fragment on button click. how is it possible.
Please help me...
dialog fragment code is...
public class MyDialogFragment extends DialogFragment implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.MY_DIALOG);
}
#Override
public void onStart() {
super.onStart();
Dialog d = getDialog();
if (d!=null){
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.MATCH_PARENT;
d.getWindow().setLayout(width, height);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.my_fragment, container, false);
return root;
}

on your button click you can use myDialogFragment.show(), just posting a code snippet :
MyDialogFragment myDialogFragment = new MyDialogFragment();
// You cannot access `getFragmentManager()` directly inside fragment so use getActivity()
myDialogFragment.show(getActivity().getFragmentManager(), "DialogFragment");
you should use getFragmentManager() or getSupportFragmentManager depending on which Fragment subclass you are using

Write following code in your button click :
MyDialogFragment myDialogFragment = new MyDialogFragment();
myDialogFragment.show(getFragmentManager(), "myDialogFragment");

If you are displaying the DialogFragment from another fragment. You could try calling getChildFragmentManager() instead of getFragmentManager()
so your code would look like
MyDialogFragment mdf = new MyDialogFragment();
mdf.show(getChildFragmentManager(), "tag");

Related

App title doesn't change after returning to previous fragment

I've been developing an android app which I included the default Navigation-Drawer from Android Studio and so on. In my home fragment, I've implemented CarViews, and then set those cardview(s) OnClickListener to replace the fragment with traditional procedure.
After the fragment replacement and new page comes, I wanted to change the Actionbar title.
So in the onCreateView(...) method, I tried,
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("B");
It worked. But after pressing the hardware back button to go back to the stacked fragment, the title remains changed & it doesn't change to "Home" again. I've tried other ways. Here's my following codes. Thanks in advance.
public class HomeFragment extends Fragment implements View.OnClickListener {
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_home, container, false);
Objects.requireNonNull(((AppCompatActivity) Objects.requireNonNull(getActivity())).getSupportActionBar()).setTitle("Home");
CardView cardView1 = root.findViewById(R.id.doctor_on);
CardView cardView2 = root.findViewById(R.id.ambulance_e);
CardView cardView3 = root.findViewById(R.id.maintainance_s);
cardView1.setOnClickListener(this);
cardView2.setOnClickListener(this);
cardView3.setOnClickListener(this);
return root;
}
#Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.doctor_on:
FragmentTransaction fragmentTransaction = Objects.requireNonNull(getActivity()).getSupportFragmentManager().beginTransaction();
Fragment fragment1 = new doctors();
fragmentTransaction.replace(R.id.container1, fragment1).addToBackStack(getString(R.string.menu_home)).commit();
return;
case R.id.ambulance_e:
//Put Actions
FragmentTransaction fragmentTransaction2 = Objects.requireNonNull(getActivity()).getSupportFragmentManager().beginTransaction();
Fragment fragment2 = new ambulance();
fragmentTransaction2.replace(R.id.container1, fragment2).addToBackStack(getString(R.string.menu_home)).commit();
return;
case R.id.maintainance_s:
//Put Actions
FragmentTransaction fragmentTransaction3 = Objects.requireNonNull(getActivity()).getSupportFragmentManager().beginTransaction();
Fragment fragment3 = new maintanance();
fragmentTransaction3.replace(R.id.container1, fragment3).addToBackStack(getString(R.string.menu_home)).commit();
return;
}
}
#Override
public void onResume() {
super.onResume();
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Home");
}
}
To the next fragment(where I change the titlebar and pressed back button):
public class doctors extend Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("B");
View root = inflater.inflate(R.layout.fragment_doctors, container, false);
return root;
}
}
And in the doctors Fragment... Should fix the title error.
#Override
public void onDestroyView() {
super.onDestroyView();
Objects.requireNonNull(((AppCompatActivity) Objects.requireNonNull(getActivity()))
.getSupportActionBar())
.setTitle(getString(R.string.your_title_here));
}
You have to override the method onBackPressed() and write the code:
#Override
public View onBackPressed(){
//Here goes the code that head back to your main fragment
FragmentTransaction fragmentTransaction3 = Objects.requireNonNull(getActivity()).getSupportFragmentManager().beginTransaction();
Fragment fragment3 = new maintanance();
fragmentTransaction3.replace(R.id.container1, fragment3).addToBackStack(getString(R.string.menu_home)).commit();
}
}`
Add below code in Home Fragment...
#Override
public void onHiddenChanged(Boolean hidden) {
super.onHiddenChanged(hidden);
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Home");
}

DialogFragment opened from Activity or Fragment?

I want to detect whether the DialogFragment is opened from an Activity or Fragment. Because the calling Activity or Fragment will have an Interface attached for a listener.
if Activity is used to show the dialog:
(inside DialogFragment I will write)
listener = (MyListener) getActivity();
else if Fragment is calling the dialog:
listener = (MyListener) getParentFragment;
So, I need to detect who is calling the dialog fragment!
If you ask me to edit your code then do this.
void showDialog() {
DialogFragment newFragment = new MyAlertDialogFragment();
newFragment.setFromActivity(true); pass here.
newFragment.show(getFragmentManager(), "dialog");
}
In your DialogFragment
public static class MyAlertDialogFragment extends DialogFragment {
boolean isFromActivity;
public void setFromActivity(boolean isFromActivity){
this.isFromActivity = isFromActivity;
}
}
If you ask me a suggestion - Pass listener instead of checking from Activity or Fragment.
You should do common code by using setters, so that in future you can just pass listener.
DialogFragment newFragment = new MyAlertDialogFragment();
newFragment.setListener(this); // or use anonymous deriving like new Listener()...
I am using the below style for my question, posting as an answer because it might help someone.
public MyDialog extends DialogFragment{
private MyListener listener;
public static MyDialog newInstance(MyListener callback){
MyDialog dialog = new MyDialog();
dialog.listener = callback;
return dialog;
}
//rest of the Dialog code such as onCreate() etc..
}
And calling from Any Activity or Fragment
ACTIVITY
public MyActivity extends AppCompatActivity implements MyListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
MyDialog dialog = MyDialog.newInstance(this);
dialog.show(getSupportFragmentManager, "TAG");
}
}
FRAGMENT
public MyFragment extends Fragment implements MyListener{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.my_frag_layout, container, false);
MyDialog dialog = MyDialog.newInstance(this);
dialog.show(getChildFragmentManager, "TAG");
return view;
}
}
Please comment if there is any possiblity of error or conditions where it can crash. Thank you!

Android Fragment in Activity and isFinishing

In my application i am using some fragments, in a viewpager.
I want to show a dialog in a fragment like this:
final Dialog dialog = new Dialog(activity, R.style.DialogTheme);
dialog.show();
The activity is set in the onCreateView like this:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
activity = getActivity();
}
This is working perfectly, but in some cases like if the app goes in background, and the user comes back to the app, i got an error "Fragment not attached to activity" in the line "dialog.show()".
So to prevent this error i use this:
if(!activity.isFinishing())
dialog.show();
else
Toast.makeText(activity, "Error", Toast.LENGTH_SHORT).show();
I think this is definitly not the best way...
Is there maybe a solution like reloading the app if the activity isFinishing or even a better solution?
override onAttach method
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof YourActiivty) {
//here is your code
} else {
}
}
public class MyFragment extends Fragment
#Override
public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
if (visible) {
...
}
}
Try to use DialogFragment rather than showing dialog by Dialog class, it will give you more stable view ,just extend your class by "DialogFragment"
public class DemoDialogFragment extends DialogFragment{
public DemoDialogFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_demo_dialog, container, false);
-----------
return rootView;
}
and make function in you activity for calling it
public void showDialogFrag(DialogFragment dialogFragment, String tag) {
dialogFragment.show(getSupportFragmentManager(), tag);
}
Then call this function by this from any fragment
((MainActivity) getActivity()).showDialogFrag(new DemoDialogFragment(), Constant.FragmentTags.DemoDialogFragment);

how to show full screen fragment activity in android?

I know how to show full screen activity but it is not working in fragment activity ?
public class Dialog extends Fragment {
public Dialog() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.dialog, container, false);
}
}
Try to use DialogFragment:
public class FullScreenDialogFragment extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.dialog, container, false);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
#Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null) {
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
}
}
In your question you have asked how to show full-screen fragment activity?
If you know how to make how to make an activity to full screen. You have to use the same way to make the FragmentActivity full screen. Because FragmentActivity is a subclass of Activity.
↳ android.app.Activity
↳ android.support.v4.app.FragmentActivity
Also in your question, The class you have included is Fragment, not FragmnetActivity.
If you need to make your fragment full screen you have to make your hosting activity fullscreen.
use getActivity() method to get hosting activity from the fragment. Then you can make the fragment fullscreen.
The answer by #shmakova worked for me, but then my colleague pointed out a simpler solution. It is enough to have the dialog's style inherit from a style, whose name ends with NoTitleBar.Fullscreen and add a windowNoTitle element. For example:
In my Dialog:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AppCompatDialog(getActivity(), R.style.MyDialogStyle);
}
in src/main/res/values/styles.xml:
<style name="MyDialogStyle" parent="#android:style/Theme.Light.NoTitleBar.Fullscreen">
<item name="windowNoTitle">true</item>
</style>
There are several advantages of such an approach: you don't have to override onCreateDialog and can have multiple styles.xml instances depending on device features. For example, you can have your dialog extend to full screen only on the smaller screens.
try this solution
public class setWallpaperFragment extends DialogFragment {
public setWallpaperFragment() {
// Required empty public constructor
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int theme = android.R.style.Theme_Black_NoTitleBar_Fullscreen;
setStyle(DialogFragment.STYLE_NO_TITLE,theme);
}

Re-Use DialogFragment as a Fragment in Activity Layout

Was trying to reuse a complex DialogFragment as a Fragment in an activity layout. I don't want to have to re-write this whole DialogFragment class as it is quite complex. In one spot only does the designers want this layout not as a popup but in a page. Is there a way to get around the DialogFragment from throwing this (From DialogFragment.java):
if (view != null) {
if (view.getParent() != null) {
throw new IllegalStateException("DialogFragment can not be attached to a container view");
}
mDialog.setContentView(view);
}
I went so far as to null out the creation of Dialog in the OnCreateDialog() overridden method and added the overridden method onCreateView(). But still view is not null and throws the IllegalStateException. I have the fragment embedded in the activity layout
<fragment
android:id="#+id/fragment_mine"
android:name="com.test.MyDialogFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible" />
So question is, is there anyway to re-use a DialogFragment as a Fragment in an Activity's layout?
Cant you just use your DialogFragment as a regular fragment ?.
Like so:
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle state) {
super.onCreate(state);
final int FRAGMENT_ID = 100;
LinearLayout contentView = new LinearLayout(this);
contentView.setOrientation(LinearLayout.VERTICAL);
Button showButton = new Button(this);
showButton.setText("Show Dialog");
showButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//using TestDialogFragment as a dialog
new TestDialogFragment().show(getSupportFragmentManager(), "dialog");
}
});
contentView.addView(showButton);
final LinearLayout fragmentContainer = new LinearLayout(this);
fragmentContainer.setId(FRAGMENT_ID);
contentView.addView(fragmentContainer);
setContentView(contentView);
//using TestDialogFragment as a Fragment
getSupportFragmentManager().beginTransaction()
.replace(FRAGMENT_ID, new TestDialogFragment()).commit();
}
public static class TestDialogFragment extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView view = new TextView(getActivity());
view.setText("Test Fragment");
return view;
}
}
}

Categories

Resources