views:

97

answers:

1

I want to add a new view to my scene that will contain content that will change programatically throughout the course of my application. When it does change, it needs to pop up on the screen for 3 seconds (or a click) then disappear.

This view will change in size according to its content WRAP_CONTENT, but ideally I'd like it centered horizontally and vertically on the screen.

I'm stuck on three parts: 1) what type of view should I use for this...I was thinking Relative, but all of my playing with it has yielded no good results for what I'm trying to do 2) with respect to #1 (trying relative view), I could not get it centered properly (tried using param.leftMargin and param.topMargin with varying values but could not get it to work on different devices with different resolutions 3) also with respect to #1, I couldn't make this float over everything else on my screen (need something like a z-index or the like).

any ideas, code examples would be wonderful.

TIA

A: 

Use a custom dialog, i.e. a LinearLayout with android:theme="@android:style/Theme.Dialog" and for the class, it would be something like

public class YourCustomDialog extends Dialog implements DialogInterface

where you can implement you custom logic of what to display. Such dialog is floating and modal on top of all other views then and you can also optionally set the background to blurry, etc.

This is a typical constructor of my custom dialog - the layout would be defined in an xml layout file, which in my case is my_custom_dialog.xml:

public MyCustomDialog(Context context) {
    super(context, android.R.style.Theme);

    Window window = getWindow();
    window.requestFeature(Window.FEATURE_NO_TITLE);
    window.setGravity(Gravity.BOTTOM);
    window.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.empty));

    setContentView(R.layout.my_custom_dialog);

    // actually not necessary as it's already the default value:
    window.setLayout(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    ...
}
Mathias Lin
thanks for the quick response. This will be my first go at creating a custom dialog. I will look for some resources on how to do it. To get me started, can you tell me, do I need to include any new files in the res folder or subfolders or just create this class within src?
Kyle
you define an layout for the dialog itself in a separate xml layout file. then you use setContentView to assign it to the dialog, see added code sample above.
Mathias Lin
ok this is a great start. do I include android:theme="@android:style/Theme.Dialog" in this custom layout file or does that go in the main.xml file (in a new linear layout)?
Kyle
I figured out enough to get started based on your information and this link: http://developer.android.com/guide/topics/ui/dialogs.html.Thanks again.
Kyle
The problem with using a dialog or an activity with a dialog theme is that you can click elsewhere to dismiss it, you have to press the backbutton. If you want to emulate the true ipad popover style you are going to have to use a widget and use either a frame or relative layout.
schwiz
@schwiz: I'm glad you posted that comment. I was just about to ask how I can use a clickevent to dismiss the dialog as I couldn't find a listener in that class for click. I really don't want to have to add a button to dismiss the dialog...so there is no other way to do this?
Kyle
@schwiz: this seems to work - setCanceledOnTouchOutside(true); though it'd be nicer if you could have on the touch of the dialog, not outside of it.
Kyle
yep, once I thought about it the contact popup in android 2.x is doing exactly what you want, and they use an activity. Here is the source for it you probably don't have to make is freaking complicated though :P http://android.git.kernel.org/?p=platform/packages/apps/Contacts.git;a=blob;f=src/com/android/contacts/ui/QuickContactActivity.java;h=7f6e39bb718265d9ec8e14c66f13f30fd989411a;hb=froyo
schwiz
@schwiz: " you can click elsewhere to dismiss it, you have to press the backbutton": but you can just override the backbutton behaviour then - of the dialog?!
Mathias Lin