368

I'm creating a DialogFragment to show some help messages regarding my app. Everything works fine besides one thing: There is a black stripe at the top of the window that shows the DialogFragment, that I presume is reserved for the title, something I don't want to use.

This is specially painful since my custom DialogFragment uses a white background, so the change is way too notorious to be left aside.

Let me show you this in a more graphical manner:

enter image description here

Now the XML code for my DialogFragment is as follows:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:id="@+id/holding" 
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:background="@drawable/dialog_fragment_bg"
        >
        <!-- Usamos un LinearLayout para que la imagen y el texto esten bien alineados -->
        <LinearLayout
            android:id="@+id/confirmationToast" 
            android:orientation="horizontal" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            >

            <TextView android:id="@+id/confirmationToastText" 
            android:layout_width="wrap_content"
            android:layout_height="fill_parent" 
            android:text="@string/help_dialog_fragment"
            android:textColor="#AE0000"
            android:gravity="center_vertical"
            />

        </LinearLayout>
        <LinearLayout
            android:id="@+id/confirmationButtonLL" 
            android:orientation="horizontal" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            android:gravity="center_horizontal"
            >    
            <Button android:id="@+id/confirmationDialogButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_marginBottom="60dp"
                android:background="@drawable/ok_button">
            </Button>
        </LinearLayout>
    </LinearLayout>
</ScrollView>

And the code of the class that implements the DialogFragment:

public class HelpDialog extends DialogFragment {

    public HelpDialog() {
        // Empty constructor required for DialogFragment
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //Inflate the XML view for the help dialog fragment
        View view = inflater.inflate(R.layout.help_dialog_fragment, container);
        TextView text = (TextView)view.findViewById(R.id.confirmationToastText);
        text.setText(Html.fromHtml(getString(R.string.help_dialog_fragment)));
        //get the OK button and add a Listener
        ((Button) view.findViewById(R.id.confirmationDialogButton)).setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                 // When button is clicked, call up to owning activity.
                HelpDialog.this.dismiss();
             }
         });
        return view;
    }

}

And the creation process in the main Activity:

/**
 * Shows the HelpDialog Fragment
 */
private void showHelpDialog() {
    android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
    HelpDialog helpDialog = new HelpDialog();
    helpDialog.show(fm, "fragment_help");
}

I really don't know if this answer, related with a Dialog, fits here also Android: How to create a Dialog without a title?

How can I get rid of this title area?

Community
  • 1
  • 1
AlejandroVK
  • 7,373
  • 13
  • 54
  • 77

7 Answers7

595

Just add this line of code in your HelpDialog.onCreateView(...)

getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

This way you're explicitly asking to get a window without title :)


EDIT

As @DataGraham and @Blundell pointed out on the comments below, it's safer to add the request for a title-less window in the onCreateDialog() method instead of onCreateView(). This way you can prevent ennoying NPE when you're not using your fragment as a Dialog:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
  Dialog dialog = super.onCreateDialog(savedInstanceState);

  // request a window without the title
  dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
  return dialog;
}
a.bertucci
  • 12,142
  • 2
  • 31
  • 32
  • 63
    Of course you can also use the convenience method `getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE)` as well... – a.bertucci Mar 07 '13 at 19:40
  • This is, indeed, a perfect answer! Thanks for the help Archano. – AlejandroVK Mar 07 '13 at 20:47
  • 8
    May want to do this in onCreateDialog instead, in case your fragment is shown *not* as a dialog. – DataGraham Jul 25 '13 at 17:24
  • 2
    @a.bertucci you're the best! However if you ever add that fragment as a normal Fragment the `getDialog` will return you a NPE – Blundell Sep 02 '13 at 09:32
  • 1
    No @Blundell, YOU are the best, sir. And yes - as @DataGraham already pointed out - it's safer to put that code in the `onCreateDialog()` instead. I'll update the answer accordingly :) – a.bertucci Sep 02 '13 at 10:22
  • 4
    This shrinks the width of my dialog. Any workarounds for this issue? – clocksmith Jul 23 '14 at 18:13
  • Hmmm... I don't like requesting features. I like demanding them. Is there a `demandFeature()` function? – JDJ Nov 26 '14 at 14:23
  • 2
    @clocksmith is the `android:layout_width` attribute for the root view in the layout for your dialog set to `wrap_content`? – ataulm Dec 30 '14 at 04:07
  • Calling `DialogFragment.setStyle(STYLE_NO_TITLE, 0)` actually does the same thing behind the scenes. – BladeCoder Feb 18 '15 at 13:00
  • Thank you, please not this line must be called before `dialog.setContentView()` or it will through Exception – Ahmed Mostafa Dec 31 '17 at 06:50
  • @BladeCoder Sure, it do the same thing in this case ... So, which is the best choice ? – Bulma Jan 09 '19 at 08:11
79

Dialog fragment has setStyle method, which should be called before view creation Java Doc. Also style of the dialog can be set with the same method

public static MyDialogFragment newInstance() {
        MyDialogFragment mDialogFragment = new MyDialogFragment();
        //Set Arguments here if needed for dialog auto recreation on screen rotation
        mDialogFragment.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
        return mDialogFragment;
}
Max Ch
  • 1,247
  • 10
  • 13
  • 3
    Max, I tried to use setStype(STYLE_NO_TITLE, 0). But the result is that my dialog is cut to 1/4 size of the width, so the message in the dialog is not shown all. – vliux Nov 03 '13 at 14:22
  • @vliux hard to say what is going on without seeing your code, but I assume you can fix the problem by specifying width of the child view to something which will fit your message – Max Ch Nov 05 '13 at 18:27
  • 1
    I think the two ways are actually the same in the code, from looking at the android sources. I'm having the same issue where it's cutting into 1/4 the size too though :(. Haven't solved it yet. – abhishekmukherg Apr 07 '14 at 23:04
  • 1
    This is the best way to do it. – Psypher Apr 26 '14 at 10:19
  • @BladeCoder are you sure about that? seems to work fine for me. – Sam Mar 03 '15 at 14:50
  • 1
    @Sam You're right, the Window style is persisted in onSaveInstanceState and restored afterwards. I still believe it's a better practice to initialize everything in onCreate() in general. – BladeCoder Mar 03 '15 at 16:55
  • If you wanted the style of your dialog to be externally configurable, you'd have to do the onSaveInstanceState stuff yourself in order to make the style available in onCreate. I'd still probably come down on the side of using the APIs provided – Sam Mar 03 '15 at 19:29
  • who's getting cut to 1/4 try this setStyle(STYLE_NORMAL, android.support.v7.appcompat.R.style.Base_Theme_AppCompat_Light_Dialog); – Ivan Vazhnov Sep 09 '15 at 13:14
  • 1
    All direct children of a root view of your layout have to be a width of match_parant. Then you'll get a dialog with a normal width. – nicolausYes Feb 20 '16 at 15:14
23
FragmentManager manager = getSupportFragmentManager();
SettingsDialog sd = new SettingsDialog();
sd.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
sd.show(manager, "settings_dialog");
Mosiur
  • 1,342
  • 13
  • 16
19

Try easy way

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NO_TITLE, 0);
}
Linh
  • 57,942
  • 23
  • 262
  • 279
sonida
  • 4,411
  • 1
  • 38
  • 40
11

Set the style to Theme_Holo_Dialog_NoActionBar:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NORMAL, android.R.style.Theme_Holo_Dialog_NoActionBar);
}
user2910855
  • 111
  • 1
  • 3
11
public class LoginDialog extends DialogFragment {   
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.login_dialog, null);
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        return view;
    }   
}
Tim S. Van Haren
  • 8,861
  • 2
  • 30
  • 34
Sher Ali
  • 5,513
  • 2
  • 27
  • 29
0

I could not get the suggested methods to work when using a androidx.preference.PreferenceDialogFragmentCompat.

What ultimately worked was adding the following method to the PreferenceDialogFragmentCompat:

/**
 * This is needed to get a dialog without a title.
 */
@Override
protected void onPrepareDialogBuilder(@NonNull AlertDialog.Builder builder) {
    super.onPrepareDialogBuilder(builder);
    builder.setTitle(null);
}
dazed
  • 352
  • 3
  • 9