Layout margin/padding at the top of dialog fragment

15,781

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.

Share:
15,781

Related videos on Youtube

Herr von Wurst
Author by

Herr von Wurst

List item SOreadytohelp

Updated on July 08, 2022

Comments

  • Herr von Wurst
    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:

    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

  • Herr von Wurst
    Herr von Wurst over 10 years
    Thank 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
    sergej shafarenka over 10 years
    To access window call, getActivity().getWindow(). Call it in onCreateView() method.
  • Herr von Wurst
    Herr von Wurst over 10 years
    I did getDialog().getWindow().requestFeature(Window.FEATURE_NO_TIT‌​LE); in the onCreateView() method and it works. Thank you!
  • leonardkraemer
    leonardkraemer about 6 years
    Should be the accepted answer, because getWindow() doesn't wok in a DialogFragment
  • Mikhail Sharin
    Mikhail Sharin over 2 years
    I used setStyle(STYLE_NO_TITLE, android.R.style.Theme_Dialog), because theme from getActivity().getApplicationInfo() removes transparent black overlay.