Android: How to create a Dialog without a title?

AndroidAndroid LayoutDialog

Android Problem Overview


I'm trying to generate a custom dialog in Android. I create my Dialog like this:

dialog = new Dialog(this);
dialog.setContentView(R.layout.my_dialog);

Everythings works fine except for the title of the Dialog. Even if I don't set the title of the dialog the dialog popup has a blank space at the position of the dialog.

Is there any way to hide this part of the Dialog?

I tried it with an AlertDialog but it seems the layout is not set properly:

LayoutInflater inflater = 
    (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.map_dialog, null);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(view);

// dialog = new Dialog(this);
// dialog.setContentView(R.layout.map_dialog);

dialog = builder.create();

((TextView) dialog.findViewById(R.id.nr)).setText(number);

If I use this code I get a null Pointer Exception in the last line. The dialog is not null so the TextView I try to retrieve does not exist.
If I uncomment the part where I use the Dialog Constructor everything works fine but for the title above my dialog layout.

Android Solutions


Solution 1 - Android

FEATURE_NO_TITLE works when creating a dialog from scratch, as in:

Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

But it doesn't work when creating an AlertDialog (or using the Builder), because it already disables the title and use a custom one internally.

I have looked at the SDK sources, and I think that it can't be worked around. So to remove the top spacing, the only solution is to create a custom dialog from scratch IMO, by using the Dialog class directly.

Also, one can do that with a style, eg in styles.xml:

<style name="FullHeightDialog" parent="android:style/Theme.Dialog">
   <item name="android:windowNoTitle">true</item>
</style>

And then:

Dialog dialog = new Dialog(context, R.style.FullHeightDialog);

Solution 2 - Android

You can hide the title of a dialog using:

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);


Previous version of this answer, which is overcomplicated:

You need to use an AlertDialog. There's a good explanation on the Android Developer's site about custom dialogs.

In very short summary, you do this with code like copied below from the official website. That takes a custom layot file, inflates it, gives it some basic text and icon, then creates it. You'd show it then with alertDialog.show().

AlertDialog.Builder builder;
AlertDialog alertDialog;

Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater)
        mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
        (ViewGroup) findViewById(R.id.layout_root));

TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);

builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();

In response to comment:

I assume that TextView with the id nr is in the View you are inflating with View view = inflater..... If so, then you need to change just one bit: instead of dialog.findView... make it view.findView.... Then once you've done that, remember to use dialog.show(), or even builder.show() without bothering to do builder.create().

Solution 3 - Android

In your code add this line

requestWindowFeature(Window.FEATURE_NO_TITLE);	

Or in XML use a theme

android:theme="@android:style/Theme.NoTitleBar"

XML would be a better implementation as with the code version the title bar gets created and then removed which is a waste of resource

> Ok good try but it is not working. I > get: > android.view.WindowManager$BadTokenException: > Unable to add window -- token null is > not for an application if I want to > shwo the dialog.

Change the alert dialog type to system dialog ( e.g., TYPE_SYSTEM_OVERLAY ) and see if this resolves your issue

Solution 4 - Android

Use like this:

Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 

This will remove any title bar from dialog window.

Solution 5 - Android

Use below code before setcontentview :-

    Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    dialog.setContentView(R.layout.custom_dialog);

Note: You must have above code, in same order and line. requestWindowFeature must be before the setContentView line.

Solution 6 - Android

you can remove title by

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

where dialog is name of my dialog .

Solution 7 - Android

In your code if you use requestWindowFeature(Window.FEATURE_NO_TITLE); be sure that it goes before dialog.setContentView(); otherwise it causes the application to crash.

Solution 8 - Android

In your Custom_Dialog.java class add requestWindowFeature(Window.FEATURE_NO_TITLE)

public class Custom_Dialog extends Dialog {

    protected Custom_Dialog(Context context, int theme) {
        super(context, theme);
        // TODO Auto-generated constructor stub
        requestWindowFeature(Window.FEATURE_NO_TITLE); //This line 
    }
}

Solution 9 - Android

I found Three Way to do this >

  1. Using requestWindowFeature

    Dialog dialog = new Dialog(this); dialog.requestWindowFeature(dialog.getWindow().FEATURE_NO_TITLE);

  2. Using style (style.xml)

    Dialog dialog = new Dialog(context, R.style.FullHeightDialog);

  3. Using XML theme in AndroidManifest.xml

    android:theme="@android:style/Theme.NoTitleBar"

Solution 10 - Android

olivierg's answer worked for me and is the best solution if creating a custom Dialog class is the route you want to go. However, it bothered me that I couldn't use the AlertDialog class. I wanted to be able to use the default system AlertDialog style. Creating a custom dialog class would not have this style.

So I found a solution (hack) that will work without having to create a custom class, you can use the existing builders.

The AlertDialog puts a View above your content view as a placeholder for the title. If you find the view and set the height to 0, the space goes away.

I have tested this on 2.3 and 3.0 so far, it is possible it doesn't work on every version yet.

Here are two helper methods for doing it:

/**
 * Show a Dialog with the extra title/top padding collapsed.
 * 
 * @param customView The custom view that you added to the dialog
 * @param dialog The dialog to display without top spacing
     * @param show Whether or not to call dialog.show() at the end.
 */
public static void showDialogWithNoTopSpace(final View customView, final Dialog dialog, boolean show) {
	// Now we setup a listener to detect as soon as the dialog has shown.
	customView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
	    
		@Override
	    public void onGlobalLayout() {
			// Check if your view has been laid out yet
	    	if (customView.getHeight() > 0) {
	    		// If it has been, we will search the view hierarchy for the view that is responsible for the extra space. 
	    		LinearLayout dialogLayout = findDialogLinearLayout(customView);
	    		if (dialogLayout == null) {
	    			// Could find it. Unexpected.
	    			
	    		} else {
	    			// Found it, now remove the height of the title area
	    			View child = dialogLayout.getChildAt(0);
	    			if (child != customView) {
	    				// remove height
	    				LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams();
	    				lp.height = 0;
	    				child.setLayoutParams(lp);
	    				
	    			} else {
	    				// Could find it. Unexpected.
	    			}
	    		}
	    		
	    		// Done with the listener
	    		customView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
	    	}
	     }
		
	});
	
	// Show the dialog
	if (show)
             dialog.show();
}

/**
 * Searches parents for a LinearLayout
 * 
 * @param view to search the search from
 * @return the first parent view that is a LinearLayout or null if none was found
 */
public static LinearLayout findDialogLinearLayout(View view) {
	ViewParent parent = (ViewParent) view.getParent();
	if (parent != null) {
		if (parent instanceof LinearLayout) {
			// Found it
			return (LinearLayout) parent;
			
		} else if (parent instanceof View) {
			// Keep looking
			return findDialogLinearLayout((View) parent);
			
		}
	}
	
	// Couldn't find it
	return null;
}

Here is an example of how it is used:

	Dialog dialog = new AlertDialog.Builder(this)
		.setView(yourCustomView)
		.create();
	
	showDialogWithNoTopSpace(yourCustomView, dialog, true);

If you are using this with a DialogFragment, override the DialogFragment's onCreateDialog method. Then create and return your dialog like the first example above. The only change is that you should pass false as the 3rd parameter (show) so that it doesn't call show() on the dialog. The DialogFragment will handle that later.

Example:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
	Dialog dialog = new AlertDialog.Builder(getContext())
		.setView(yourCustomView)
		.create();

	showDialogWithNoTopSpace(yourCustomView, dialog, false);
	return dialog;
}

As I test this further I'll be sure to update with any additional tweaks needed.

Solution 11 - Android

I don't know if this question is still actual, but in my case, when I switched from Dialog to DialogFragment,

requestWindowFeature(Window.FEATURE_NO_TITLE);

was not an option, but I could use

setStyle(STYLE_NO_TITLE, 0);

instead with the same result.

Solution 12 - Android

Set the title to empty string using builder.

    Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("");
...
    builder.show();

Solution 13 - Android

set the "gravity" attribute on the entire dialog to "center". Then you will need to override that setting to all of the child components in the dialog that you do not want centered.

Solution 14 - Android

dialog=new Dialog(YourActivity.this, 1);  // to make dialog box full screen with out title.
dialog.setContentView(layoutReference);
dialog.setContentView(R.layout.layoutexample);

Solution 15 - Android

in XML use a theme

android:theme="@android:style/Theme.NoTitleBar"

Solution 16 - Android

If we simply use the dialog without the setTitle(),then is that gonna work in removing the space of the title ?

mUSSDDialog = new AlertDialog.Builder(context).setView(dialogView)
.setPositiveButton(R.string.send_button,DialogListener)
.setNegativeButton(R.string.cancel,DialogListener)
.setCancelable(false).create();

Solution 17 - Android

Think you can just use this now:

AlertDialog dialog = new AlertDialog.Builder(this)
  .setView(view)
  .setTitle("")
  .create()

Solution 18 - Android

ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "", 
                             "Loading. Please wait...", true);

creates a title less dialog

Solution 19 - Android

public static AlertDialog showAlertDialogWithoutTitle(Context context,String msg) 
	 {
	  AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
	  alertDialogBuilder.setMessage(msg).setCancelable(false)
	    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
	     public void onClick(DialogInterface dialog, int id) {
	      
	     }
	    });

	   return alertDialogBuilder.create(); 
	 }

Solution 20 - Android

While using AlertDialog, not using setTitle() makes the title disappear

Solution 21 - Android

After a bunch of hacking, I got this to work:

            Window window = dialog.getWindow();
            View view = window.getDecorView();
            final int topPanelId = getResources().getIdentifier( "topPanel", "id", "android" );
            LinearLayout topPanel = (LinearLayout) view.findViewById(topPanelId);
            topPanel.setVisibility(View.GONE);

Solution 22 - Android

You Can do this without using AlertDialog by defining new Class that extends from Dialog Class like this:

public class myDialog extends Dialog {
    public myDialog(Context context) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    }
}

Solution 23 - Android

Here's something you can do with AlertBuilder to make the title disappear:

TextView title = new TextView(this);
title.setVisibility(View.GONE);
builder.setCustomTitle(title);

Solution 24 - Android

Use this

    Dialog dialog = new Dialog(getActivity());
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialog.setCancelable(true);
    dialog.setContentView(R.layout.image_show_dialog_layout);

Solution 25 - Android

dialog_custom .requestWindowFeature(Window.FEATURE_NO_TITLE);

this will remove title from cutsom dialog.

Note add these line before adding content.. for eg

     dialog_custom = Dialog(activity!!)
    dialog_custom.requestWindowFeature(Window.FEATURE_NO_TITLE)
    dialog_custom.setContentView(R.layout.select_vehicle_type)
    dialog_custom.setCanceledOnTouchOutside(false)
    dialog_custom.setCancelable(true)

Solution 26 - Android

I try requestWindowFeature(Window.FEATURE_NO_TITLE);
but not working for me, if you are like me so do this :

Passing a theme to your dialog can remove the title bar for you.

    <style name="NoTitleDialog" parent="Theme.AppCompat.Dialog">
   <item name="android:windowNoTitle">true</item>
    </style>

Pass the theme to your dialog:

Dialog dialog = new Dialog(this, R.style.NoTitleDialog);

that's it very simple

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
QuestionJanuszView Question on Stackoverflow
Solution 1 - AndroidoliviergView Answer on Stackoverflow
Solution 2 - AndroidSteve HaleyView Answer on Stackoverflow
Solution 3 - AndroidDonal RaffertyView Answer on Stackoverflow
Solution 4 - Androidalok tiwariView Answer on Stackoverflow
Solution 5 - AndroiddugguView Answer on Stackoverflow
Solution 6 - AndroidshailendraView Answer on Stackoverflow
Solution 7 - AndroidSzymon MorawskiView Answer on Stackoverflow
Solution 8 - AndroidM_KView Answer on Stackoverflow
Solution 9 - AndroidNirav RanparaView Answer on Stackoverflow
Solution 10 - AndroidcottonBallPawsView Answer on Stackoverflow
Solution 11 - AndroidAnalizerView Answer on Stackoverflow
Solution 12 - AndroidSzere DyeriView Answer on Stackoverflow
Solution 13 - AndroidMattView Answer on Stackoverflow
Solution 14 - AndroidBamadevaView Answer on Stackoverflow
Solution 15 - AndroidAsad RaoView Answer on Stackoverflow
Solution 16 - AndroidjadView Answer on Stackoverflow
Solution 17 - AndroidJ2KView Answer on Stackoverflow
Solution 18 - AndroidPraveen KondapalliView Answer on Stackoverflow
Solution 19 - AndroidSandeep Kumar PatilView Answer on Stackoverflow
Solution 20 - AndroidkostikusView Answer on Stackoverflow
Solution 21 - AndroidChuck DView Answer on Stackoverflow
Solution 22 - AndroidKhaled AbuShqearView Answer on Stackoverflow
Solution 23 - AndroidrundavidrunView Answer on Stackoverflow
Solution 24 - AndroidHabibur Rahman OvieView Answer on Stackoverflow
Solution 25 - Androidindrajeet jyotiView Answer on Stackoverflow
Solution 26 - Androidjenos konView Answer on Stackoverflow