Layout margin/padding at the top of dialog fragment
Solution 1
Try to call getWindow().requestFeature(Window.FEATURE_NO_TITLE);
right before setContentView(R.layout.<dialog_layout>);
in onCreate()
or onCreateView()
method of your dialog.
Solution 2
There are two ways to reuse/remove that empty space at the top of the DialogFragment
.
If you want to simple remove it:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); // <--
View view = inflater.inflate(R.layout.fragment_dialog, container, false);
return view;
}
If you want to reuse it for a dialog title:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().setTitle("Some title..."); // <--
View view = inflater.inflate(R.layout.fragment_dialog, container, false);
return view;
}
Solution 3
Yet another solution:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_TITLE, getActivity().getApplicationInfo().theme);
}
This example uses the applications default theme. Note also that setStyle()
needs to be called in onCreate()
as opposed to onCreateView()
to have an effect.
Related videos on Youtube
Comments
-
Herr von Wurst almost 2 years
When using fragments my layouts get disturbed by an additional space at the top and I don't know where this is coming from. It looks like this:
What are possible sources for this empty space? The theme or some style settings I have not yet found, or is this space reserved for an action bar? I would really like to get rid of this.
Here are the corresponding layout xml and the code to instantiate the dialog:
XML:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_margin="5dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <ProgressBar android:layout_margin="5dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/search_progress" style="?android:attr/progressBarStyle" android:indeterminate="true" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/search_progress" android:text="Searching for:" /> <TextView android:id="@+id/searchingNameTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:layout_alignBottom="@+id/search_progress" android:layout_alignLeft="@+id/textView1" android:text="Barcelona" /> </RelativeLayout>
Java:
FragmentManager fm = this.getSupportFragmentManager(); mSearchingDialog = new SearchingDialogFragment(); Bundle bundle = new Bundle(); bundle.putString("name", mCityToAdd.userName()); mSearchingDialog.setArguments(bundle); mSearchingDialog.show(fm, TAG);
I also asked this question here, thinking it would be a problem correlated with a scrollview, but the space appears in all my fragments:
-
Herr von Wurst over 10 yearsThank you, but the fragment does not have a getWindon() method. The code above is all I do to instantiate the dialog fragment, so there is no onCreate() either.
-
sergej shafarenka over 10 yearsTo access window call, getActivity().getWindow(). Call it in onCreateView() method.
-
Herr von Wurst over 10 yearsI did
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
in theonCreateView()
method and it works. Thank you! -
leonardkraemer about 6 yearsShould be the accepted answer, because
getWindow()
doesn't wok in aDialogFragment
-
Mikhail Sharin over 2 yearsI used
setStyle(STYLE_NO_TITLE, android.R.style.Theme_Dialog)
, becausetheme
fromgetActivity().getApplicationInfo()
removes transparent black overlay.