Custom AlertDialog - problem with android.R.id.custom

I am sure there is not enough meaning here, so I hope someone can explain.

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

If you want to display a more complex view, find a 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)); 

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 above, where I addView throws addView NullPointerException which makes me think that there is a problem with android.R.id.custom .

So, the question is what is wrong with the above, and is there also a better way to create your own pop-up window (possibly using the Dialog class or its extension)?

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

+4
source share
4 answers

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

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

For more information, see application topics .

+1
source

Check out the configuration examples for the Mossila AlertDialog. I found them more useful than Google examples.

I cut and pasted Mosila code directly into my project, and it just worked :-) Then I made a few settings to suit my needs.

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

0
source

Use AlertDialog.setView (View view) to add a custom view to the AlertDialog. Mark my answer to a similar question .

0
source

I think your problem is that you are not โ€œinflatingโ€ the layout. With FrameLayout you need to use LayoutInflater

use the following code:

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

This should work with FrameLayout. Read more about this on the Android Layout page . Tricks

Also check out LayoutInflater

edit: I also noticed that there is a similar article here: How to implement custom mode AlertDialog View

-1
source

Source: https://habr.com/ru/post/1335907/


All Articles