17

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:

enter image description here

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:

ScrollView adds extra space on top

Community
  • 1
  • 1
Herr von Wurst
  • 2,571
  • 5
  • 32
  • 53
  • Possible duplicate of [How to create a DialogFragment without title?](http://stackoverflow.com/questions/15277460/how-to-create-a-dialogfragment-without-title) – Michael Allan Feb 05 '16 at 23:09

3 Answers3

25

Try to call getWindow().requestFeature(Window.FEATURE_NO_TITLE); right before setContentView(R.layout.<dialog_layout>); in onCreate() or onCreateView() method of your dialog.

sergej shafarenka
  • 20,071
  • 7
  • 67
  • 86
22

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;
}
letiagoalves
  • 11,224
  • 4
  • 40
  • 66
5

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.

Magnus
  • 17,157
  • 19
  • 104
  • 189
  • I used `setStyle(STYLE_NO_TITLE, android.R.style.Theme_Dialog)`, because `theme` from `getActivity().getApplicationInfo()` removes transparent black overlay. – Mikhail Sharin Dec 24 '21 at 09:52