I try to show a native ad in the customlayoutdialog.
My code to generate a native ad is based in the code of Erel and it returns a panel.
If I show a custom dialog with ad one time, it works.
The problem is if I call the custom dialog a second time, I get the error:
I tried several codes to remove views, etc., but I don't know how to fix it.
In attached there are a small project that reproduces the problem:
- click in the button "Show ad" and you see the custom dialog with the panel.
- close the dialog and click in the button again to see the error.
Thanks in advance for any help.
My code to generate a native ad is based in the code of Erel and it returns a panel.
If I show a custom dialog with ad one time, it works.
The problem is if I call the custom dialog a second time, I get the error:
B4X:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I tried several codes to remove views, etc., but I don't know how to fix it.
In attached there are a small project that reproduces the problem:
- click in the button "Show ad" and you see the custom dialog with the panel.
- close the dialog and click in the button again to see the error.
Thanks in advance for any help.
B4X:
Sub Globals
Dim pAdmob As Panel
Private pNativeAd As Panel
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1")
pAdmob.Initialize("pAdmob")
pAdmob = CreatePanelNativeAdmob
End Sub
Sub btnEnterDetails_Click
Dim dialog As CustomLayoutDialog
Dim sf As Object = dialog.ShowAsync("Title", "Yes", "Cancel", "No", Null, False)
dialog.SetSize(100%x, 300dip)
Wait For (sf) Dialog_Ready (DialogPanel As Panel)
DialogPanel.LoadLayout("DetailsDialog")
Dim cd As ColorDrawable
cd.Initialize(Colors.RGB(Rnd(0,256), Rnd(0,256), Rnd(0,256)), 0)
pNativeAd.Background = cd
pNativeAd.AddView(pAdmob, 2%x, 2%y, 30%x, 20%y)
Wait For (sf) Dialog_Result (Result As Int)
If Result = DialogResponse.POSITIVE Then
Dim cs As CSBuilder
ToastMessageShow(cs.Initialize.Size(30).Color(Colors.Red).Append("Yes!!!").PopAll, True)
End If
End Sub
Sub CreatePanelNativeAdmob As Panel
Dim pnl As Panel
pnl.Initialize("pnl")
Dim cd As ColorDrawable
cd.Initialize(Colors.RGB(Rnd(0,200), Rnd(0,200), Rnd(0,200)), 0)
pnl.Background = cd
Dim lb As Label
lb.Initialize("")
lb.TextColor = Colors.White
lb.textsize = 12
lb.Text = "NATIVE AD"
pnl.AddView(lb, 1%x, 1%x, 15%x, 8%y)
Return pnl
End Sub