18

I got a problem with setting aligning my view programatically to the bottom of the screen. Basically I just want to place my admob advert at the screen bottom. I can't do it with XML because it would be even more troublesome. Right now I already got the advert working, and added it manually to the RelativeLayout, but by default it appears at the top of the screen. I tried to use the gravity command, but couldn't get it working.

Here's the code snippet:

    _glSurfaceView = new CCGLSurfaceView(this);

    adView = new AdView(this, AdSize.BANNER, "a14e04559caea39");
    adView.loadAd(new AdRequest());
    adView.setGravity(Gravity.BOTTOM);

    RelativeLayout rl = new RelativeLayout(this);

    rl.addView(_glSurfaceView);
    rl.addView(adView);     

    setContentView(rl);
    adView.loadAd(new AdRequest());

Can anyone help me out?

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
Nam
  • 410
  • 1
  • 4
  • 8

4 Answers4

37

dragonfly's answer worked for me. Here is the complete code snippet for the reference

adView = new AdView(this, AdSize.BANNER, "xxxxxxxxxxxxxxx");
panel = new MySurfaceView(this);

AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);

RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.WRAP_CONTENT, 
    RelativeLayout.LayoutParams.WRAP_CONTENT);

lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

rl.addView(panel);
rl.addView(adView, lay);

setContentView(rl);

adView.loadAd(adRequest);
Nir
  • 1,608
  • 1
  • 17
  • 29
11
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
 p.addRule(RelativeLayout.ALIGN_BOTTOM, tv.getId());
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
Abhijit S
  • 181
  • 1
  • 7
9

Add the advert to the RelativeLayout with LayoutParams as follows:

RelativeLayout.LayoutParams rparams = (LayoutParams) rl
            .getLayoutParams();
    rparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    ((ViewGroup) rl).addView(adView, rparams);

instead of just

rl.addView(adView);   

Good luck! :)

dragonfly
  • 411
  • 3
  • 11
  • Hey thanks a lot for the quick answers, but somehow it won't work. I keep getting a nullpointerexception + the app crashes, at the line "addRule". – Nam Jul 03 '11 at 14:04
  • I'm not sure but maybe it's because you're creating a RelativeLayout dynamically within the program...if you have a RelativeLayout already defined in an xml file and you refer rl to it by id, it'll work. Good luck anyhow! :) – dragonfly Jul 04 '11 at 12:05
0
    ImageButton buttonAdvance = new ImageButton(this);
    RelativeLayout.LayoutParams params = new       RelativeLayout.LayoutParams(160, 160);
    params.leftMargin = 5;
    params.bottomMargin = 4;
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    buttonAdvance.setLayoutParams(params);
takluiper
  • 679
  • 2
  • 8
  • 24