5

I'm sure I'm missing the point here so I'm hoping someone can explain.

I want to create a popup when a user touches an ImageView. I had a look at AlertDialog and the docs say...

If you want to display a more complex view, look up the FrameLayout called "custom" and add your view to it:

...with the following code...

    FrameLayout fl = (FrameLayout) findViewById(android.R.id.custom);
    fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));

So as a test I tried the following in my onCLick() method...

    TextView tv = new TextView(this);
    tv.setText("Hello World");
    FrameLayout customFrameLayout = (FrameLayout) findViewById(android.R.id.custom);
    customFrameLayout.addView(tv, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

The last line of the above where I'm calling addView throws a NullPointerException which makes me think there's a problem with android.R.id.custom.

So the question is, what is wrong with the above and also is there a better way of creating a custom popup (perhaps by using the Dialog class or extending it)?

NOTE: I'm only using TextView in this example as a test, I want to add something more complex for my actual popup.

Lii
  • 11,553
  • 8
  • 64
  • 88
Squonk
  • 48,735
  • 19
  • 103
  • 135
  • Use AlertDialog.setView(View view) to add a custom view to an AlertDialog. Check out [my answer to a similar question](https://stackoverflow.com/a/28801551). – Jung Soo Kim Mar 02 '15 at 01:53

3 Answers3

1

One option is to create an Activity and style it using the dialog theme:

<activity android:theme="@android:style/Theme.Dialog">

See applying themes for more information.

Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
  • This looks like a much better way to do what I need using startActivityForResult() - thanks. – Squonk Jan 18 '11 at 10:04
0

Checkout Mossila's AlertDialog customization examples. I found them more helpful than Google's examples.

I cut-and-pasted Mossila's code directly into my project and it just worked:-) Then I made a few tweaks to meet my needs.

http://mossila.wordpress.com/2011/05/10/android-dialog-single-choice-items/

devdanke
  • 1,309
  • 15
  • 27
-1

I think your problem is because you dont "inflate" the layout. With a FrameLayout you need to use the LayoutInflater

use the following code:

LayoutInflater.from(context).inflate(android.R.id.custom, this, true)

This should work with FrameLayout. Read up more about this at the Android Layout tricks page

Also check out LayoutInflater

edit: i have noticed aswell that there is an identical article to this problem here too: How to implement a custom AlertDialog View

Community
  • 1
  • 1
Hakan Ozbay
  • 4,639
  • 2
  • 22
  • 28