LoginSignup
6
6

More than 5 years have passed since last update.

AdMobのレクタングル広告が表示されたりされなかったりで詰んだ

Last updated at Posted at 2015-02-20

Admobだとリクエストを投げる場合に様々なサイズの広告の設定ができるのですがレクタングル広告を設定したあの日、少し様子が変なのでした。

Activity.java

/** AdView */
private static AdView rectangle = null;

// 表示するメソッド
public static void show() {
    if(rectangle == null) {
        // 初期化
        rectangle = new AdView(activity);

        // ユニットIDを設定
        rectangle.setAdUnitId(ADMOB_RECTANGLE_UNIT_ID);

        // 広告のサイズを指定
        rectangle.setAdSize(AdSize.MEDIUM_RECTANGLE);

        // Activityにビューを追加
        this.addContentView(rectangle,new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, Gravity.CENTER);

        // リクエスト送信    
        AdRequest adRequest = new AdRequest.Builder().build();
        rectangle.loadAd(adRequest);

    } else {
        // 表示
        rectangle.setVisibility(View.VISIBLE);
    }
}


public static void hide() {

    // 初期化されていなければリターン
    if(rectangle == null) return;

    // 隠す
    rectangle.setVisibility(View.INVISIBLE);
}

// アクティビティがバックグラウンドから復帰した時
public void onResume() {
    super.onResume();
    if(rectangle != null) rectangle.resume();
}

// アクティビティがバックグラウンドに行く時
public void onPause() {
    super.onPause();
    if(rectangle != null) rectangle.pause();
}

と大体こんな感じで実装していたのですが、フルHD端末だとうまくいくのですが、他の端末GalaxyS3,Xperia SPなどだとバックグラウンドが復帰時や
他のスレッドで他の広告を表示した後などは表示されない。いや、厳密にはビューは存在していてタッチするとリンクに飛ぶ(広告の機能はある)けど
表示されていないという謎の現象が発生していました。

というわけで、いろいろ調べたところ。

Activity.java
// 表示するメソッド
public static void show() {
    if(rectangle == null) {
        // 初期化
        rectangle = new AdView(activity);

        // ユニットIDを設定
        rectangle.setAdUnitId(ADMOB_RECTANGLE_UNIT_ID);

        // 広告のサイズを指定
        rectangle.setAdSize(AdSize.MEDIUM_RECTANGLE);

        // Activityにビューを追加
        this.addContentView(rectangle,new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, Gravity.CENTER);

        // リクエスト送信    
        AdRequest adRequest = new AdRequest.Builder().build();
        rectangle.loadAd(adRequest);

    } else {
        // コレヲヨブトウマクイク
        rectangle.requestLayout();

        // 表示
        rectangle.setVisibility(View.VISIBLE);
    }
}

ということになりました。そして、requestLayoutは
「Call this when something has changed which has invalidated the layout of this view. This will schedule a layout pass of the view tree. This should not be called while the view hierarchy is currently in a layout pass (isInLayout(). If layout is happening, the request may be honored at the end of the current layout pass (and then layout will run again) or after the current frame is drawn and the next layout occurs.」

とのこと。えと、要するにViewのレイアウトが効かなくなったたときにコレを呼ぶとよいらしい。ちゃんとレイアウト組んでるのに表示されないなどの現象があればとりあえず

// レイアウトを再要求する
rectangle.requestLayout();

ですよみなさま。

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6