mAdRelativeLayout = (RelativeLayout) findViewById(R.id.adRelativeLayout);
adView = new AdView(this);
adView.setId(View.generateViewId());
adView.setAdSize(AdSize.SMART_BANNER);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
adView.setLayoutParams(params);
mAdRelativeLayout.addView(adView);relativeLayout = new RelativeLayout(this);
relativeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
relativeParams.addRule(RelativeLayout.ABOVE, adView.getId());
relativeLayout.setLayoutParams(relativeParams);
mAdRelativeLayout.addView(relativeLayout);
tView = new TextView(this);
tView.setText("Bottom Text");
params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
tView.setLayoutParams(params1);
relativeLayout.addView(tView); @Override
public void onClick(View arg0) {
if(!isDestroyed) {
adView.destroy();
isDestroyed = true;
Log.i("shown", "isShown");
mAdRelativeLayout.removeView(adView);
relativeParams.removeRule(RelativeLayout.ABOVE);
relativeParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
} else {
mAdRelativeLayout.addView(adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
Log.i("shown", "notShown");
isDestroyed = false;
relativeParams.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeParams.addRule(RelativeLayout.ABOVE, adView.getId());
}
relativeLayout.setLayoutParams(relativeParams);
}<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:id="@+id/principalLayout">
...
</RelativeLayout>