Reputation: 2133
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?
Upvotes: 212
Views: 222771
Reputation: 43
This was a productive find to run a full screen dialog box:
AlertDialog.Builder(activity, R.style.Theme_AppName_PopupOverlay)
with the corresponding theme:
<!--night\theme-->
<style name="Theme.AppName.PopupOverlay" parent="ThemeOverlay.AppCompat.Light">
<!--text-->
<item name="colorPrimary">@color/white</item>
<!--title-->
<item name="android:textColor">@color/white</item>
<!--window color-->
<item name="android:backgroundTint">@color/grey_dark</item>
</style>
The Alert Dialog Builder also needs the reference to the PopupOverlay for the day theme. So I just have:
<style name="Theme.AppName.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
Upvotes: 0
Reputation: 123
class NameDialog : DialogFragment(){
lateinit var mDataBinding:NameYourLayoutBinding
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
super.onCreateView(inflater, container, savedInstanceState)
mDataBinding=DataBindingUtil.inflate(
LayoutInflater.from(getContext()),
R.layout.YOUR.LAYOUT.NAME,
null,
false
)
dialog?.let {
it.window?.requestFeature(Window.FEATURE_NO_TITLE)
it.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
}
return mDataBinding.root
}
override fun onStart() {
super.onStart()
dialog?:return
val width = ViewGroup.LayoutParams.MATCH_PARENT
val height = ViewGroup.LayoutParams.MATCH_PARENT
dialog?.window?.setLayout(width, height)
}
}
Upvotes: 0
Reputation: 2190
The easiest way to do achieve this is :
Add the following theme to your styles.xml
<style name="DialogTheme" parent="AppTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">false</item>
<item name="android:windowIsFloating">false</item>
</style>
and in your class extending DialogFragment, override
@Override
public int getTheme() {
return R.style.DialogTheme;
}
This will work on Android OS 11(R) as well.
https://anubhav-arora.medium.com/android-full-screen-dialogfragment-1410dbd96d37
Upvotes: 7
Reputation: 33
No more style that need more complex code.. with status bar..
public static void ShowFullScreenDialog(Context context, View contentView, string header)
{
using (Android.Support.V7.App.AlertDialog.Builder builder = new Android.Support.V7.App.AlertDialog.Builder(context, Android.Resource.Style.ThemeBlackNoTitleBarFullScreen))
{
var view = UIHelper.InflaterView(context, Resource.Layout.dialog_full_screen);
builder.SetView(view);
Dialog dialog = builder.Create();
dialog.Window.SetBackgroundDrawableResource(ThemeHelper.GetMainActivityThemeDrawable());
dialog.Window.SetLayout(context.Resources.DisplayMetrics.WidthPixels, context.Resources.DisplayMetrics.HeightPixels);
dialog.Window.DecorView.SystemUiVisibility = StatusBarVisibility.Visible;
//fullLinear
var headtxt = view.FindViewById<TextView>(Resource.Id.headertxt);
headtxt.SetTextColor(Color.White);
headtxt.Text = header;
var fullLinear = view.FindViewById<LinearLayout>(Resource.Id.fullLinear);
var closeBttn = view.FindViewById<ImageButton>(Resource.Id.closeBttn);
closeBttn.ImageTintList = ColorHelper.ConvertColorToStateList(Color.White);
closeBttn.Click += delegate
{
dialog.Hide();
};
if (contentView.Parent != null)
((ViewGroup)contentView.Parent).RemoveView(contentView); // <- fix
fullLinear.AddView(contentView);
dialog.Show();
}
}
Important
dialog.Window.SetLayout(context.Resources.DisplayMetrics.WidthPixels, context.Resources.DisplayMetrics.HeightPixels);
Upvotes: 0
Reputation: 36059
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;
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(DialogFragment.STYLE_NO_FRAME, R.style.FullScreenDialog)
}
Upvotes: 74
Reputation: 48
the following solution worked for me other solution gave me some space in the sides i.e not full screen
You need to make changes in onStart and onCreate method
@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);
}
}
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
final Dialog dialog = new Dialog(requireContext());
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
Upvotes: 0
Reputation: 1927
Add this below lines in style of the full screen dialog.
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowIsFloating">false</item>
Upvotes: 1
Reputation: 2133
Worked for me in Kotlin,
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
dialog?.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
}
Upvotes: 0
Reputation: 5495
And the kotlin version!
override fun onStart() {
super.onStart()
dialog?.let {
val width = ViewGroup.LayoutParams.MATCH_PARENT
val height = ViewGroup.LayoutParams.MATCH_PARENT
it.window?.setLayout(width, height)
}
}
Upvotes: 16
Reputation: 8713
A solution by using the new ConstraintLayout
is wrapping the ConstraintLayout
in a LinearLayout
with minHeight and minWidth fixed. Without the wrapping, ConstraintLayout is not getting the right size for the Dialog.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="1000dp"
android:minHeight="1000dp"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_color"
android:orientation="vertical">
<!-- some constrained views -->
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
Upvotes: 0
Reputation: 1927
This below answer works for me in fragment dialog.
Dialog dialog = getDialog();
if (dialog != null)
{
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.MATCH_PARENT;
dialog.getWindow().setLayout(width, height);
}
Upvotes: 0
Reputation: 59004
Note : Even one can find right answer here. But I want clarify a confusion.
android.app.DialogFragment
@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);
}
}
android.support.v4.app.DialogFragment
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
}
Upvotes: 8
Reputation: 11921
This is the solution how I figured out this issue:
@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));
}
}
Upvotes: 4
Reputation: 1944
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL,
android.R.style.Theme_Black_NoTitleBar_Fullscreen);
}
Upvotes: 180
Reputation: 1692
Create below theme in your style.xml:
<style name="DialogTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:paddingRight">0dp</item>
<item name="android:paddingLeft">0dp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:windowNoTitle">true</item>
</style>
then set the style in DialogFragment
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogTheme);
}
Upvotes: 3
Reputation: 191
The following way will work even if you are working on a relative layout. Follow the following steps:
Go to styles.xml and then under the theme just added, add these two attributes:
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
My theme setting for the dialog is as under:
<style name="DialogTheme" parent="Theme.AppCompat.Dialog" >
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
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); }
Upvotes: 3
Reputation: 1869
I am very late for answering this question still i want to share this answer so that in future anyone can use this.
I have used this code in my project it works in lower version as well as higher version.
Just use this theme inside onCreateDialog() like this :
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_pump_details, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), android.R.style.Theme_Black_NoTitleBar_Fullscreen);
return builder.create();
}
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.
<!-- Variant of {@link #Theme_Black} that has no title bar and
no status bar. This theme
sets {@link android.R.attr#windowFullscreen} to true. -->
<style name="Theme.Black.NoTitleBar.Fullscreen">
<item name="windowFullscreen">true</item>
<item name="windowContentOverlay">@null</item>
</style>
Please let me know if anyone face any issue. Hope this is helpful. Thanks :)
Upvotes: 1
Reputation: 1187
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:
this.mFragmentToShow.show(this.mFragmentManager, LOGTAG);
Hope it helps.
EDIT
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:
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Translucent_NoTitleBar);
}
Hope it helps.
Upvotes: 13
Reputation: 4220
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;
}
Upvotes: 52
Reputation: 2380
This is what you need to set to fragment:
/* theme is optional, I am using leanback... */
setStyle(STYLE_NORMAL, R.style.AppTheme_Leanback);
In your case:
DialogFragment newFragment = new DetailsDialogFragment();
newFragment.setStyle(STYLE_NORMAL, R.style.AppTheme_Leanback);
newFragment.show(ft, "dialog");
And why? Because DialogFragment (when not told explicitly), will use its inner styles that will wrap your custom layout in it (no fullscreen, etc.).
And layout? No hacky way needed, this is working just fine:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
</RelativeLayout>
Enjoy
Upvotes: 1
Reputation: 538
Try this for common fragment dialog for multiple uses. Hope this will help you bettor
public class DialogFragment extends DialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_visit_history_main, container, false);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
initializeUI(rootView);
return rootView;
}
@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);
}
}
private void initializeUI(View rootView) {
//getChildFragmentManager().beginTransaction().replace(R.id.fv_container,FragmentVisitHistory.getInstance(), AppConstant.FRAGMENT_VISIT_HISTORY).commit();
}
}
Upvotes: 1
Reputation: 3938
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>
Upvotes: 73
Reputation: 1477
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(getActivity(), android.R.style.Theme_Holo_Light);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
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.
Upvotes: 1
Reputation: 4451
In my case I used the following approach:
@Override
public void onStart() {
super.onStart();
getDialog().getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
}
}
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:
<style name="Dialog.NoTitle" parent="Theme.AppCompat.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:padding">0dp</item>
<item name="android:windowBackground">@color/window_bg</item>
</style>
And of course this style needs to be used when we create the dialog like the following:
public static DialogFragment createNoTitleDlg() {
DialogFragment frag = new Some_Dialog_Frag();
frag.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.Dialog_NoTitle);
return frag;
}
Upvotes: 19
Reputation: 19111
window.setLayout
isn't enough for older devices.
Here is what I do:
try {
ViewGroup parent = (ViewGroup) view;
do {
parent = (ViewGroup) parent.getParent();
if (parent == null)
break;
parent.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
parent.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
parent.requestLayout();
} while (true);
} catch (Exception e){}
Upvotes: 0
Reputation: 37596
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
Upvotes: 247
Reputation: 1139
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:
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
Window window = getDialog().getWindow();
LayoutParams attributes = window.getAttributes();
//must setBackgroundDrawable(TRANSPARENT) in onActivityCreated()
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
if (needFullScreen)
{
window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
}
}
Upvotes: 15
Reputation: 431
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:
@Override public void 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:
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog);
}
Upvotes: 9
Reputation: 327
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.
Upvotes: 2
Reputation: 641
Try to use setStyle() in onCreate and override onCreateDialog make dialog without title
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
or just override onCreate() and setStyle fellow the code.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme);
}
Upvotes: 13