Full Screen DialogFragment in Android
Solution 1
Try switching to a LinearLayout
instead of RelativeLayout
. I was targeting the 3.0 Honeycomb api when testing.
public class FragmentDialog extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.show);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog();
}
});
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
void showDialog() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
DialogFragment newFragment = MyDialogFragment.newInstance();
newFragment.show(ft, "dialog");
}
public static class MyDialogFragment extends DialogFragment {
static MyDialogFragment newInstance() {
MyDialogFragment f = new MyDialogFragment();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_dialog, container, false);
return v;
}
}
}
and the layouts: fragment_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="1000dp"
android:minHeight="1000dp">
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<Button android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="show">
</Button>
</LinearLayout>
Solution 2
To get DialogFragment on full screen
Override onStart
of your DialogFragment like this:
@Override
public void onStart()
{
super.onStart();
Dialog dialog = getDialog();
if (dialog != null)
{
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.MATCH_PARENT;
dialog.getWindow().setLayout(width, height);
}
}
And thanks very much to this post: The-mystery-of-androids-full-screen-dialog-fragments
Solution 3
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL,
android.R.style.Theme_Black_NoTitleBar_Fullscreen);
}
Solution 4
Make a Full screen DialogFragment by using only the style
First solution
1. Add to your style.xml
:
<style name="FullScreenDialog" parent="Theme.AppCompat.Light.Dialog">
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:padding">0dp</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowCloseOnTouchOutside">false</item>
</style>
2. Add to your DialogFragment:
@Override
public int getTheme() {
return R.style.FullScreenDialog;
}
Alternative solution
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(DialogFragment.STYLE_NO_FRAME, R.style.FullScreenDialog)
}
Solution 5
According to this link DialogFragment fullscreen shows padding on sides this will work like a charm.
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
// the content
final RelativeLayout root = new RelativeLayout(getActivity());
root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// creating the fullscreen dialog
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(root);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
return dialog;
}
Related videos on Youtube
Eric
Updated on July 21, 2022Comments
-
Eric almost 2 years
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 = new DetailsDialogFragment(); newFragment.show(ft, "dialog");
I know naively tried to set the RelativeLayout in the fragment to fill_parent and some minWidth and minHeight.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:minWidth="1000px" android:minHeight="600px" android:background="#ff0000">
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 also tried to set the Window Attributes in code, as suggested here: http://groups.google.com/group/android-developers/browse_thread/thread/f0bb813f643604ec. But this didn't help either.
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
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setStyle(STYLE_NO_FRAME, android.R.style.Theme_Holo_Light); }
Unfortunately, this is not quite want I want. I definitely need a small "padding" around the dialog to show the background.
Any ideas how to accomplish that?
-
kouretinho over 9 yearsgood read: techrepublic.com/article/…
-
-
Peter Ajtai over 12 yearsAnd for me, I needed to use a RelativeLayout, but the dialog width wasn't adjusting properly to the contents, so I nested the RelativeLayout in a LinearLayout whose only child was the RelativeLayout... this triggered the proper width adjustments.
-
Till almost 12 yearsI tested everything, but I can confirm the only thing that worked for my DialogFragment was wrapping my whole layout in a LinearLayout. That way I could set width and height of my (now wrapped) original layout...wasted hours on this
-
Gerrit Begher over 9 yearsSetting minWidth and minHeight to be somehing large seems to be the crucial part; but this is a hack instead of a clean solution. The answer given by @David below - explained further in the link he gives - also works and is clean.
-
user2274431 about 9 yearsI agree. Here's a full explanation from the Android Developer guide: link
-
WhiteHorse almost 9 yearsExactly this works, but i tried with Videoview and it doesnt works for that. StatusBar still shows. So this fixed me below code: dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
-
starkej2 over 8 years@tir38 I don't think that's true because onCreateDialog() already has an implementation in the superclass.
-
Ashu Kumar over 8 yearsfinally sort out, thansk for suggestion @David
-
dazed over 8 yearsThis worked nicely for me. I used
ViewGroup.LayoutParams.WRAP_CONTENT
for the height. Thanks. -
VonSchnauzer over 7 yearsThis is the answer. Works great.
-
Breeno about 7 yearsI tried most of the answers on this thread, some worked, but I lost all my app compat theme styles. I hoped this one would work as parent theme was AppCompat. It almost worked. For me, I had to add 2 additional styles:
<item name="android:windowFullscreen">true</item> <item name="android:windowIsFloating">false</item>
Source: TechRepublic -
Aman Verma almost 7 yearsplease refer to this question - stackoverflow.com/questions/45494644/…
-
rommex over 6 yearsthis is clean and non-hacky
-
Abdul Waheed over 6 yearsthis is not working for my case.. it showing some padding from all corners. any help?
-
MeLean over 6 yearsThis is more elegant way than bradley4`s answer.
-
AutonomousApps over 6 yearsWorks great, even in 2018! (nothing else did) Thanks.
-
Sagar Nayak about 6 yearsdialog comes to fullscreen but everything became black except a button. any idea ??
-
Taufik Nur Rahmanda about 6 yearsThis is the perfect answer even in Android Studio 3.1+ in 2018, better than other answers exist here. The other answer will broke the android UI like cardview, spinner, etc.
-
Yhondri almost 6 yearsIt works, other solutions changes the theme of buttons and other elements of the fragment.
-
me_ almost 6 yearsperfect one line solution
-
mutiemule about 5 yearsThis should be the accepted answer. Elaborate and clear
-
Janusz Hain almost 5 yearsBoth solutions worked, but the first one caused bug with clipboard popup. Alternative works fine
-
Yue Yin over 4 yearsWorks charmingly in 2020!
-
Felipe Ribeiro R. Magalhaes over 4 yearsYep, also shows some weird margins on mine and completely ignores the margin I set manually on the fragment's root view, which I really need to control manually.
-
Felipe Ribeiro R. Magalhaes over 4 yearsWorks but if you want to define margins in your XML layout you will have to wrap your views with another layout, otherwise the margins will be ignored.
-
DIRTY DAVE about 4 yearsThat is so weird...switching it fixed the issue.
-
Georgiy Chebotarev about 4 yearsIt really works! Also I want to add, developer should wrap dialog content by transparent FrameLayout, to avoid content deformation.
-
nikhil123 about 4 yearsfor me, it showed margins horizontally, but adding the following lines in
onCreateDialog
method solved my problemfinal Dialog dialog = new Dialog(requireContext()); dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
-
welshk91 over 3 years@AbdulWaheed You need to also set a style adjusting
windowNoTitle
,windowFullscreen
, andwindowIsFloating
in the DialogFragment'sonCreate
-
Itay Feldman over 3 yearsRegarding the margins people face, in continuation to what nikhil123 suggested, you can also just set the background to white instead of transparent and it will also get rid of the margins.