views:

2516

answers:

5

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.

+8  A: 

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

Donal Rafferty
+4  A: 

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

Edit in response to shown code:

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().

Steve H
I think you may have misread the Question? Janusz already has the custom dialog displaying and just requires information on removing the title
Donal Rafferty
Well, according to the official documentation, "A dialog made with the base Dialog class must have a title. If you don't call setTitle(), then the space used for the title remains empty, but still visible. If you don't want a title at all, then you should create your custom dialog using the AlertDialog class." I haven't personally experimented with it, but that would suggest that even using a custom dialog layout or themes, it's impossible to remove the title space.
Steve H
2nd thought: I think we're understanding "title" differently. I assume he's talking about the space at the top of the pop-up window, not the title at the top of the app.
Steve H
Not sure, I have an activity in my application with a dialog theme so it appears like a dialog and the code in my answer removes the space at the top of the pop up. I believe I done the same successfully for a custom dialog as in the question but I dont have the code at the min so cant be sure and may not be correct.
Donal Rafferty
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.
Janusz
Edit your question and show us the code you used. The above principle works, I've done it myself, so you must have done something wrong in your implementation. Also, did you read the developer's website instructions or did you just copy/paste?
Steve H
I have added more info to my answer regarding your new issue, report back if that helps or not
Donal Rafferty
Still now working. The problem is that in the custom dialog page you linked is a title shown that is not created through their xml file. It seems that every dialog needs this title...
Janusz
@Janusz: Edited my answer.
Steve H
Thanks for the work and trying all that. view.findViewById is working and the dialog has no title.
Janusz
+2  A: 

use like this

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

this will remove any title bar from dialog window.

thanks, -alok

alok tiwari
+1  A: 

you can remove title by

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

where dialog is name of my dialog .

shailendra
+4  A: 

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