Skip to content

Instantly share code, notes, and snippets.

@ishitcno1
Created November 20, 2013 09:40
Show Gist options
  • Save ishitcno1/7560443 to your computer and use it in GitHub Desktop.
Save ishitcno1/7560443 to your computer and use it in GitHub Desktop.
A Preference to show admob banner ads.
import android.app.Activity;
import android.content.Context;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
public class AdPreference extends Preference {
public AdPreference(Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle);}
public AdPreference(Context context, AttributeSet attrs) {super(context, attrs);}
public AdPreference(Context context) {super(context);}
@Override
protected View onCreateView(ViewGroup parent) {
parent.setPadding(0, 0, 0, 0);
View view = super.onCreateView(parent);
Activity activity = (Activity)getContext();
AdView adView = new AdView(activity, AdSize.BANNER, "admob publisher id");
((LinearLayout)view).addView(adView);
AdRequest request = new AdRequest();
adView.loadAd(request);
return view;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment