4

I need a little help from people with expirience. I hope it's easy. I just want to show new View (creating it without XML layouts) above main program's view. It's to integrate AdMob.com ad block:

I wrote such code:

AdView ad = new AdView(this); 
ad.layout(10, 10, 100, 100); 
ad.setVisibility(View.VISIBLE); 
ad.bringToFront(); 
ad.requestFocus(); 
ad.invalidate();

As you see - nothing helped, no window visible
What do I make incorrectly? Thanks!

P.S. I made in in Activity's onCreate(Bundle), but I tried in other locations too.

Has AlTaiar
  • 4,052
  • 2
  • 36
  • 37
Vadim
  • 1,582
  • 2
  • 15
  • 16

1 Answers1

6

You haven't actually told the system to draw anything or where to draw it. You'll want to look at the documentation for setContentView(view) on your Activity. If you're trying to get this to draw over your current screen, look at the documentation for Dialog (and setContentView).

When I'm programmatically creating arbitrary views to draw within an existing layout, I usually add a FrameLayout tag to my layout XML, then in the code call findViewById(), then I can use that FrameLayout view to add the view to (addView()).

I hope that helps some without writing your code for you.

Has AlTaiar
  • 4,052
  • 2
  • 36
  • 37
lilbyrdie
  • 1,887
  • 2
  • 20
  • 24
  • Thank you for your reply. I already solved problem remaking all application with XML and using RelativeLayout or AbsoluteLayout. – Vadim Aug 01 '09 at 11:20
  • @lilbyrdie: I am using the same approach as you but the application force closes when I try to add the view that I built programmatically to my frame layout that is declared in XML. So when I do, Frame Layout myFrameView.addView(viewBuiltProgrammatically) it fails. Why do you think that is? – Namratha Nov 11 '11 at 05:49
  • @Namratha: You'll want to check the error output to see what the exception text says. Without any information to go on, I'd guess you may have missed a setting or set something that isn't allowed. – lilbyrdie Nov 21 '11 at 00:14
  • I have the dreaded IllegalStateException: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. I tried to call ((ViewGroup)child.getParent()).removeView(child)); but it doesn't work. In one case, the child.getParent() returns null, in another case, the removeView just seems to have no effect at all, (calling addView afterwards still results in IllegalStateException) – Zennichimaro Oct 09 '12 at 02:25