简体   繁体   中英

android, respond to layout resize

I'm using an AdMob view which has no width/height until it's ready to be displayed and then it expands. is there a way to get notified when that happens? I need to resize my layout when that happens.

You could override the onLayout method of your view:

    View view = new View(this) {
        protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
            super.onLayout(changed, left, top, right, bottom);
            // Do some stuff
        }
    };

Try using this

AdView.setAdListener(new AdListener() {
  public void onRecieveAd(){
      //resize your view
  }
});

If you want to know when your adview has recieve an ad, then you can use an onFailedToRecieveAd listener.

AdView.setAdListener(new AdListener() {
  public void onFailedToRecieveAd(){
      //do something else
  }
});

If you use relative layouts that put the adMob in an effective location, then it works best. You can actually place the adMob image such that it is a part of the layout. When the image comes in, the layout gets shuffled around appropriately.

Alternatively, you can look into adListeners

public interface AdListener {
  public void onReceiveAd(Ad ad);
  public void onFailedToReceiveAd(Ad ad, AdRequest.ErrorCode error);
  public void onPresentScreen(Ad ad);
  public void onDismissScreen(Ad ad);
  public void onLeaveApplication(Ad ad);
}

Essentially, you can set the addListener and do something when an ad was received. But I recommend using a relative layout with the adMob included in the XML code, such that the layout automatically adjusts when the image appears/disappears.

you can use something like

public void onCreate(Bundle savedInstanceState) 
{
...
make admob...
...
 AdSize adSize = AdSize.createAdSize(AdSize.SMART_BANNER, this);
 int h = adSize.getHeightInPixels(getBaseContext());
 myListView.setPadding(0, 0, 0, h);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM