I'm trying to show an almost fullscreen DialogFragment. But I'm somehow not able to do so.
The way I am showing the Fragment is straight from the android developer documentation
FragmentManager f =((Activity)getContext()).getFragmentManager();FragmentTransaction ft = f.beginTransaction();Fragment prev = f.findFragmentByTag("dialog");if(prev !=null){ ft.remove(prev);} ft.addToBackStack(null);// Create and show the dialog.DialogFragment newFragment =newDetailsDialogFragment(); newFragment.show(ft,"dialog");
I know naively tried to set the RelativeLayout in the fragment to fill_parent and some minWidth and minHeight.
I would know expect the DialogFragment to fill the majority of the screen. But I only seems to resize vertically but only until some fixed width horizontally.
I am probably misunderstanding something about how Android handles Dialogs, as I am brand new to it. How can I do something like this? Are there any alternative ways to reach my goal?
Android Device: Asus EeePad Transformer Android 3.0.1
Update: I now managed to get it into full screen, with the following code in the fragment
@OverridepublicDialog onCreateDialog(finalBundle savedInstanceState){// the contentfinalRelativeLayout root =newRelativeLayout(getActivity()); root.setLayoutParams(newViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));// creating the fullscreen dialogfinalDialog dialog =newDialog(getActivity()); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(root); dialog.getWindow().setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT)); dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);return dialog;}
回答5:
I met the issue before when using a fullscreen dialogFragment: there is always a padding while having set fullscreen. try this code in dialogFragment's onActivityCreated() method:
Plus LinearLayout to fill all the space with content.
But there were still small gaps between left and right edges of the dialog and the screen edges on some Lollipop+ devices (e.g. Nexus 9).
It was not obvious but finally I figured out that to make it full width across all the devices and platforms window background should be specified inside styles.xml like the following:
And of course this style needs to be used when we create the dialog like the following:
As far as the Android API got updated, the suggested method to show a full screen dialog is the following:
FragmentTransaction transaction =this.mFragmentManager.beginTransaction();// For a little polish, specify a transition animation transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);// To make it fullscreen, use the 'content' root view as the container// for the fragment, which is always the root view for the activity transaction.add(android.R.id.content,this.mFragmentToShow).commit();
otherwise, if you don't want it to be shown fullscreen you can do this way:
Be aware that the solution I gave works but has got a weakness that sometimes could be troublesome. Adding the DialogFragment to the android.R.id.content container won't allow you to handle the DialogFragment#setCancelable() feature correctly and could lead to unexpected behaviors when adding the DialogFragment itself to the back stack as well.
So I'd suggested you to simple change the style of your DialogFragment in the onCreate method as follow:
It can indeed depends on how the layout is defined. But to ensure that the dialog gets the required size, the best solution is to provide the LayoutParams once the dialog is shown (and not on creation). On a DialogFragment the dialog is shown on the onStart method, so a valid method to get full width is:
@Overridepublicvoid onStart(){super.onStart();Dialog d = getDialog();if(d!=null){ d.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);}}
To also provide a theme, or style, like a NO_TITLE style, the best location is on the onCreate method:
In case anyone else comes across this, I had a similar experience to this but it turns out that the issue was that I had forgotten to return the inflated view from onCreateView (instead returning the default super.onCreateView). I simply returned the correct inflated view and that solved the problem.
回答13:
Chirag Nagariya is right except the '_Fullscreen' addition. it can be solved using any base style which not derived from Dialog style. 'android.R.style.Theme_Black_NoTitleBar' can be used as well.
This solution applies a full screen theme on the dialog, which is similar to Chirag's setStyle in onCreate. A disadvantage is that savedInstanceState is not used.
回答15:
Try this for common fragment dialog for multiple uses. Hope this will help you bettor
android.R.style.Theme_Black_NoTitleBar_Fullscreen - Here is the source code for this theme you can see only this theme is enough to make DialogFragment appear in full screen.
Please let me know if anyone face any issue. Hope this is helpful. Thanks :)
回答18:
The following way will work even if you are working on a relative layout. Follow the following steps:
Go to theme editor ( Available under Tools-> Android -> Theme Editor)
Select show all themes. Select the one with AppCompat.Dialog
Choose the option android window background if you want it to be of any specific colored background or a transparent one.
Select the color and hit OK.Select a name of the new theme.
Go to styles.xml and then under the theme just added, add these two attributes:
truefalse
My theme setting for the dialog is as under:
Make sure that the theme has a parent as Theme.AppCompat.Dialog Another way would be just make a new style in styles.xml and change it as per the code above.
Go to your Dialog Fragment class and in the onCreate() method, set the style of your Dialog as:
@Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setStyle(DialogFragment.STYLE_NORMAL,R.style.DialogTheme); }