I’m trying to create an imagebutton to close an ad from admob.
I’ve created the button in the xml with the visibility property set to “invisible” and then on java I set it to “visible” when the ad is received, but the button never become visible.
If I set it to “visible” hardcoded on the XML it appears on the screen normally.
Admob layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layout_main">
<ImageButton
android:id="@+id/close_ad"
android:visibility="invisible"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="-36dip"
android:layout_marginTop="12dip"
android:background="@drawable/btn_close_ad" />
</RelativeLayout>
Add ad:
private void addAd() {
rl = (RelativeLayout) activity.getLayoutInflater().inflate(R.layout.admob, null);
rl.setGravity(position);
activity.addContentView(rl, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
AdView admobView = new AdView(activity, AdSize.BANNER, Preferences.getAdmobKey());
admobView.loadAd(new AdRequest());
[ ... ]
admobView.setAdListener(new AdListener() {
@Override
public void onReceiveAd(Ad ad) {
Log.v("JeraAdmob", "onReceiveAd");
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
createCloseButton();
}
}, AD_SHOW_CLOSE);
}
});
}
Create button to close the ad:
private void createCloseButton() {
ImageButton button = (ImageButton) rl.findViewById(R.id.close_ad);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rl.removeAllViews();
handler.postDelayed(new Runnable() {
@Override
public void run() {
addAd();
rl.bringToFront();
}
}, AD_RELOAD);
}
});
button.setVisibility(View.VISIBLE);
button.bringToFront();
}
You should add the image button after the admob layout.
like this: