Android Custom PopupWindow/Dialog
Asked Answered
R

2

15

I'm trying to get a completely custom Dialog or PopupWindow, without any of the default Android UI controls (title, background, buttons, whatever).

Is this possible at all? I've spent hours searching for this, but no luck... It seems like this should be easily possible, but I can't find it.

Preferably this would be by inflating a View from XML, but at this point anything that would just work would be nice.

Thanks.

Roguery answered 5/5, 2010 at 13:17 Comment(1)
R
29

Steps I took:

  1. Create a class extending Dialog.
  2. In the onCreate, call setContentView(x, y) with x being your R.layout and y being R.style.popupStyle (see below).
  3. In your res/values/style.xml, you need to override the default DialogWindow style. I tried just making a style that has this one as its parent, but that still didn't clear all defaults. So I checked the Android git tree and got the default style, and just copy-pasted it. This is the one:
<style name="Theme.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowTitleStyle">@android:style/DialogWindowTitle</item>
    <item name="android:windowBackground">@android:drawable/panel_background</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>

You'll get a few errors, just solve them by copying more stuff from the official Android styles.xml and themes.xml files. Here's the contents of my styles.xml file: http://pastebin.com/RRR15YYS

That just gives you a white popup, no borders, nothing. Start customizing. :)

Thanks to mbaird for putting me on the right track.

[edit] I needed to look up my own answer again, and I spent at least ten minutes searching the official android styles/themes files, so here they are, for future reference: styles.xml and themes.xml.

Roguery answered 5/5, 2010 at 16:17 Comment(4)
@Roguery - I followed your steps, and I'm able to get the exact dialog theme as android from my own code. But my issue is repositioning the Dialog box. I want it to be placed at say (x,y) position. I was trying this with the layout margin. But then in Dialog.java (framework/base/core/java/android/app) at line 143 (android 1.6) I see a line w.setGravity(Gravity.CENTER);. Is this preventing my margin stuff? Because I just can't get it positioned at the place I want. The moment I add margin, things get screwed up.Mendenhall
For reference, I tried this approach but found the steps a bit confusing (although extremely helpful). I've accomplished a similar result without having to create a class extending the Dialog class. My method is here: #3729490Retractile
To avoid compilation error <style name="MyDialog" parent="android:style/Theme.Dialog">Gerdagerdeen
There is only setContentView (View view, ViewGroup.LayoutParams params). Where did you find the one with two View arguments?Flowering
E
8

It sounds like you are trying to really customize an AlertDialog. For what you are wanting to do you may be better off just creating your own class that extends Dialog, similar to how you create activities by writing a class that extends Activity.

You can set the layout XML by calling setContentView() inside the onCreate() method of your custom Dialog class, just like you would in an Activity.

I've run into limitations on how much you can customize AlertDialogs in the past, and I've just implemented my own Dialog classes to get the level of customization that I needed.

Empson answered 5/5, 2010 at 14:17 Comment(3)
That still gives me system background and border (like the screenshot over here: developer.android.com/guide/topics/ui/dialogs.html#CustomDialog). Is there a way to disable those? I just want my view -- and nothing but my view. I'll take care of drawing backgrounds and all the rest.Roguery
You are always going to get the default layout theme unless you override it. To do that, when you are instantiating your custom dialog class call the Dialog constructor that takes 2 arguments, and pass a custom theme as the 2nd argument.Empson
Yeah, I just figured that out. I had to jump through some more hoops to get exactly what I want. I'll post the full answer in a sec. Thanks a lot.Roguery

© 2022 - 2024 — McMap. All rights reserved.