user2120449
user2120449

Reputation: 3

How do I set ad banner at top in landscape and at bottom in portrait?

I want to show my AdMob banner on the bottom of the screen when the app is in portrait mode, and on top of the screen when it is in landscape mode. So how can I add these to my app?

Currently I'm using a separate layout (layout-port and layout-land)

Upvotes: 0

Views: 1337

Answers (2)

serv-inc
serv-inc

Reputation: 38307

There are at least two ways to do it.

XML-based (recommended in many cases, also by @CommonsWare).

Something like

<com.google.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    app:adSize="BANNER"
    app:adUnitId="a1512f50d8c3692"
    app:loadAdOnCreate="true"
    app:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" >
</com.google.ads.AdView>

This should add it to the bottom via android:layout_alignParentBottom="true" and android:layout_alignParentLeft="true". If you want it at the top, try android:layout_alignParentTop instead.

You will probably need to adjust this some (adUnitId comes to mind). Then, in code, fill it with something like

AdView adView = (AdView) findViewById(R.id.adView);
adView.loadAd(new AdRequest());

Code-based

If you really want to do this the hard way, @ashutiwari4's answer looks good at first glance.

Upvotes: 0

MrDumb
MrDumb

Reputation: 2178

Why don't you try to recognize the screen orientation. whenever screen changes it recreates the activity you can get screen orientation via fallowing method

int orientation=this.getResources().getConfiguration().orientation;
if(orientation==Configuration.ORIENTATION_PORTRAIT){
   //code for portrait mode
}
else{
    //code for landscape mode
}

try to add your layout programmatically. If you are using Relative layout you can use addRule like this

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, height);
lp.addRule(RelativeLayout.BELOW, lastId);   

You can add rules as per requirement. above is the way to add a rule. similarly there is a method to remove a rule

params.removeRule(RelativeLayout.BELOW);

Or if you want, can inflate a new layout in condition as per your need. Hope this will help!

Upvotes: 1

Related Questions