'Android: How to create a Dialog without a title?
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.
Solution 1:[1]
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:[2]
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 3:[3]
Use like this:
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
This will remove any title bar from dialog window.
Solution 4:[4]
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 5:[5]
you can remove title by
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
where dialog is name of my dialog .
Solution 6:[6]
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 7:[7]
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)
<style name="FullHeightDialog" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
Dialog dialog = new Dialog(context, R.style.FullHeightDialog);
3) Using XML theme in AndroidManifest.xml
android:theme="@android:style/Theme.NoTitleBar"
Solution 8:[8]
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:[9]
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 10:[10]
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 11:[11]
Set the title to empty string using builder.
Builder builder = new AlertDialog.Builder(context);
builder.setTitle("");
...
builder.show();
Solution 12:[12]
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 13:[13]
dialog=new Dialog(YourActivity.this, 1); // to make dialog box full screen with out title.
dialog.setContentView(layoutReference);
dialog.setContentView(R.layout.layoutexample);
Solution 14:[14]
in XML use a theme
android:theme="@android:style/Theme.NoTitleBar"
Solution 15:[15]
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 16:[16]
Think you can just use this now:
AlertDialog dialog = new AlertDialog.Builder(this)
.setView(view)
.setTitle("")
.create()
Solution 17:[17]
ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "",
"Loading. Please wait...", true);
creates a title less dialog
Solution 18:[18]
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 19:[19]
While using AlertDialog, not using setTitle() makes the title disappear
Solution 20:[20]
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 21:[21]
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 22:[22]
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 23:[23]
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 24:[24]
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 25:[25]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
