How to create a DialogFragment without title?

AndroidAndroid Dialogfragment

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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;
}

Solution 2 - Android

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;
}

Solution 3 - Android

FragmentManager manager = getSupportFragmentManager();
SettingsDialog sd = new SettingsDialog();
sd.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
sd.show(manager, "settings_dialog");

Solution 4 - Android

Try easy way

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NO_TITLE, 0);
}

Solution 5 - Android

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);
}

Solution 6 - Android

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;
    }	
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlejandroVKView Question on Stackoverflow
Solution 1 - Androida.bertucciView Answer on Stackoverflow
Solution 2 - AndroidMax ChView Answer on Stackoverflow
Solution 3 - AndroidMosiurView Answer on Stackoverflow
Solution 4 - AndroidsonidaView Answer on Stackoverflow
Solution 5 - Androiduser2910855View Answer on Stackoverflow
Solution 6 - AndroidZulfiqarView Answer on Stackoverflow