Android: How to create a Dialog without a title?
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().
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);