I am referring this google tutorial to place interstitial ads in my app, https://developers.google.com/admob/android/interstitial.
I am able to successfully show banner ads in adview, but interstitial ads are not being shown. I am using the below code to load interstitial ads.
public class DashBoardFragment extends Fragment implements OnClickListener{
private ViewPager pager;
private ViewPagerAdapter adapter;
private SlidingTabLayout tabs;
private ArrayList<Thoughts_Model> list;
private Isconnected checkinternet;
private SessionManager session;
private JSONObject json_data;
private String userid = "", tag_json_arry = "json_array_req";
private int index = 0;
private TextView btn_favourite,btn_share;
private LinearLayout progressBar_container;
private OnPageChangeListener pageChangeListener;
private LinearLayout add_container;
private AdView mAdView;
InterstitialAd interstitial;
AdRequest adRequest;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.dashboard_fragment,container,false);
this.session = new SessionManager(getActivity());
userid = session.getuserid();
checkinternet = new Isconnected(getActivity());
mAdView = new AdView(getActivity());
mAdView.setAdSize(Utility.getAdSize(getActivity()));
mAdView.setAdUnitId(getResources().getString(R.string.banner_ad_unit_id));
add_container = (LinearLayout)v.findViewById(R.id.add_container);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_BOTTOM, RelativeLayout.TRUE);
add_container.addView(mAdView,params);
adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest); // this is loading banner ad in adview properly
interstitial = new InterstitialAd(getActivity());
interstitial.setAdUnitId(getResources().getString(R.string.banner_ad_unit_id));
interstitial.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
requestNewInterstitial();
}
});
requestNewInterstitial();
progressBar_container = (LinearLayout) v.findViewById(R.id.progressBar_container);
btn_favourite = (TextView) v.findViewById(R.id.btn_favourite);
btn_share = (TextView) v.findViewById(R.id.btn_share);
list = new ArrayList<Thoughts_Model>();
pager = (ViewPager) v.findViewById(R.id.pager);
pageChangeListener = new OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int arg0) { }
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) { }
@Override
public void onPageSelected(int position) {
index = position;
if(list.get(position).getIsFavorite().equals("1")){
btn_favourite.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_favorite_filled, 0, 0, 0);
}else{
btn_favourite.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
}
}
};
pager.addOnPageChangeListener(pageChangeListener);
tabs = (SlidingTabLayout) v.findViewById(R.id.tabs);
tabs.setDistributeEvenly(true);
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.colortabstrip);
}
});
btn_favourite.setOnClickListener(this);
btn_share.setOnClickListener(this);
return v;
}
private void requestNewInterstitial() {
// TODO Auto-generated method stub
interstitial.loadAd(adRequest);
}
@Override
public void onDestroy() {
mAdView.pause();
super.onDestroy();
}
@Override
public void onResume() {
super.onResume();
mAdView.resume();
AppController.getInstance().trackScreenView("Home Screen");
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_favourite:
if (checkinternet.isConnected()) {
AddFavorite();
} else {
Utility.CommanDialog(getActivity(),
getResources().getString(R.string.try_again),
getResources().getString(R.string.no_internet), true);
}
break;
case R.id.btn_share:
//this is supposed to load ad but it toasts the message.
if (interstitial != null && interstitial.isLoaded()) {
interstitial.setAdListener(new AdListener(){
public void onAdLoaded(){
interstitial.show();
}
});
} else {
Toast.makeText(getActivity(), "Ad failed to load", Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
private List<Fragment> getFragments(){
List<Fragment> fList = new ArrayList<Fragment>();
for (int i = 0; i < list.size(); i++) {
fList.add(ShowCategory.newInstance(list.get(i)));
}
return fList;
}
}
below is the code for xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.deadbrains.shareyourthought.commons.SlidingTabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="@color/colorPrimary"
android:elevation="2dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/progressBar_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black_trans"
android:gravity="center"
android:orientation="horizontal" >
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true" >
</android.support.v4.view.ViewPager>
</FrameLayout>
<LinearLayout
android:id="@+id/add_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/btn_favourite"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:drawablePadding="-10dp"
android:gravity="center"
android:paddingBottom="15dp"
android:paddingLeft="20dp"
android:paddingRight="5dp"
android:paddingTop="15dp"
android:text="Favourite"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/btn_share"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:drawableLeft="@drawable/ic_share_white"
android:drawablePadding="15dp"
android:paddingBottom="15dp"
android:paddingLeft="25dp"
android:paddingRight="5dp"
android:paddingTop="15dp"
android:text="Share"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
I've added ad activity in manifest as below,
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" >
</activity>
There are no errors but ads are not showing. Where am I going wrong? please help.
I think this may be the problem:
if (interstitial != null && interstitial.isLoaded()) {
interstitial.setAdListener(new AdListener(){
public void onAdLoaded(){
interstitial.show();
}
});
}
You're setting the event listener after you know that the interstitial has already loaded - so I suspect it's not firing a second time, so you'll never show the ad.
As per the documentation you linked to, change the onClick
handler to just have:
if (interstitial.isLoaded()) {
interstitial.show();
}
(I don't think you need to check for null, as you're assigning interstitial
a non-null value in onCreateView
... it will never be null when you click on the view, as far as I can tell.)
See more on this question at Stackoverflow