-1

I am new to Android just 3 months old, In my code Interstitial ads are displayed every few seconds not getting displayed on every click not able to figure out the issue, Not able to find any example similar to my requirement.

My Requirement is on every click it should display Interstitial Ads.

Any suggestion or Help is highly appreciated.

Based on stack-overflow post i have integrated ads but its not working as per my requirement and help is highly appreciated.

Code:

public class AttractionListFragment extends Fragment {

private AttractionAdapter mAdapter;
private LatLng mLatestLocation;
private int mImageSize;
private boolean mItemClicked;
private InterstitialAd mInterstitialAd;

public AttractionListFragment() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Load a larger size image to make the activity transition to the detail screen smooth
    mImageSize = getResources().getDimensionPixelSize(R.dimen.image_size)
            * Constants.IMAGE_ANIM_MULTIPLIER;

    mLatestLocation = Utils.getLocation(getActivity());
    List<Attraction> attractions = loadAttractionsFromLocation(mLatestLocation);
    mAdapter = new AttractionAdapter(getActivity(), attractions);

    final View view = inflater.inflate(R.layout.fragment_main, container, false);
    AttractionsRecyclerView recyclerView =
            (AttractionsRecyclerView) view.findViewById(android.R.id.list);
    recyclerView.setEmptyView(view.findViewById(android.R.id.empty));
    recyclerView.setHasFixedSize(true);
    recyclerView.setAdapter(mAdapter);

    MobileAds.initialize(getActivity(), "ca-app-pub-3940256099942544~3347511713");
    mInterstitialAd = new InterstitialAd(this.getContext());
    mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");

    AdRequest request = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();

    mInterstitialAd.loadAd(request);

    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            if (mInterstitialAd.isLoaded())
            mInterstitialAd.show();
        }

     /*   public void onAdClosed() {
            mInterstitialAd.loadAd(new AdRequest.Builder().build());
        } */
    });

    return view;
}

@Override
public void onResume() {
    super.onResume();
    mItemClicked = false;
    LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
            mBroadcastReceiver, UtilityService.getLocationUpdatedIntentFilter());
}

@Override
public void onPause() {
    super.onPause();
    LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mBroadcastReceiver);
}

private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Location location =
                intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED);
        if (location != null) {
            mLatestLocation = new LatLng(location.getLatitude(), location.getLongitude());
            mAdapter.mAttractionList = loadAttractionsFromLocation(mLatestLocation);
            mAdapter.notifyDataSetChanged();
        }
    }
};

private static List<Attraction> loadAttractionsFromLocation(final LatLng curLatLng) {
    String closestCity = TouristAttractions.getClosestCity(curLatLng);
    if (closestCity != null) {
        List<Attraction> attractions = ATTRACTIONS.get(closestCity);
        if (curLatLng != null) {
            Collections.sort(attractions,
                    new Comparator<Attraction>() {
                        @Override
                        public int compare(Attraction lhs, Attraction rhs) {
                            double lhsDistance = SphericalUtil.computeDistanceBetween(
                                    lhs.location, curLatLng);
                            double rhsDistance = SphericalUtil.computeDistanceBetween(
                                    rhs.location, curLatLng);
                            return (int) (lhsDistance - rhsDistance);
                        }
                    }
            );
        }
        return attractions;
    }
    return null;
}

private class AttractionAdapter extends RecyclerView.Adapter<ViewHolder>
        implements ItemClickListener {

    public List<Attraction> mAttractionList;
    private Context mContext;

    public AttractionAdapter(Context context, List<Attraction> attractions) {
        super();
        mContext = context;
        mAttractionList = attractions;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.list_row, parent, false);
        return new ViewHolder(view, this);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Attraction attraction = mAttractionList.get(position);

        holder.mTitleTextView.setText(attraction.name);
        holder.mDescriptionTextView.setText(attraction.description);
        Glide.with(mContext)
                .load(attraction.imageUrl)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .placeholder(R.drawable.empty_photo)
                .override(mImageSize, mImageSize)
                .into(holder.mImageView);

        String distance =
                Utils.formatDistanceBetween(mLatestLocation, attraction.location);
        if (TextUtils.isEmpty(distance)) {
            holder.mOverlayTextView.setVisibility(View.GONE);
        } else {
            holder.mOverlayTextView.setVisibility(View.VISIBLE);
            holder.mOverlayTextView.setText(distance);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mAttractionList == null ? 0 : mAttractionList.size();
    }

    @Override
    public void onItemClick(View view, int position) {
        if (!mItemClicked) {
            mItemClicked = true;
            View heroView = view.findViewById(android.R.id.icon);
            DetailActivity.launch(
                    getActivity(), mAdapter.mAttractionList.get(position).name, heroView);
        }

    }
}

private static class ViewHolder extends RecyclerView.ViewHolder
        implements View.OnClickListener {

    TextView mTitleTextView;
    TextView mDescriptionTextView;
    TextView mOverlayTextView;
    ImageView mImageView;
    ItemClickListener mItemClickListener;

    public ViewHolder(View view, ItemClickListener itemClickListener) {
        super(view);
        mTitleTextView = (TextView) view.findViewById(android.R.id.text1);
        mDescriptionTextView = (TextView) view.findViewById(android.R.id.text2);
        mOverlayTextView = (TextView) view.findViewById(R.id.overlaytext);
        mImageView = (ImageView) view.findViewById(android.R.id.icon);
        mItemClickListener = itemClickListener;
        view.setOnClickListener(this);

    }

    @Override
    public void onClick(View v)
    {
        mItemClickListener.onItemClick(v, getAdapterPosition());

    }
}

interface ItemClickListener {
    void onItemClick(View view, int position);
}

}

Bhavesh
  • 909
  • 2
  • 23
  • 38

3 Answers3

0

In your code you are showing the ad as soon as its loaded instead of showing it when the user click on the button

@Override
    public void onAdLoaded() {
        super.onAdLoaded();
        if (mInterstitialAd.isLoaded())
        mInterstitialAd.show();
    }

Here you need to remove the mInterstitialAd.show(); as it cause the ad to show as soon as its loaded

Instead show the ad on your button click event:

myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mInterstitialAd.isLoaded())
                mInterstitialAd.show();
        }
    });
Michael A
  • 5,770
  • 16
  • 75
  • 127
0

After spending 2 days I figure out the issue The solution for the above question is as follows:

Necessary Code changes:

  private static class ViewHolder extends RecyclerView.ViewHolder
        implements View.OnClickListener {

    TextView mTitleTextView;
    TextView mDescriptionTextView;
    TextView mOverlayTextView;
    ImageView mImageView;
    ItemClickListener mItemClickListener;
    private InterstitialAd mInterstitialAd;



    public ViewHolder(View view, ItemClickListener itemClickListener) {
        super(view);
        mTitleTextView = (TextView) view.findViewById(android.R.id.text1);
        mDescriptionTextView = (TextView) view.findViewById(android.R.id.text2);
        mOverlayTextView = (TextView) view.findViewById(R.id.overlaytext);
        mImageView = (ImageView) view.findViewById(android.R.id.icon);
        mItemClickListener = itemClickListener;
        view.setOnClickListener(this);

    MobileAds.initialize(view.getContext(), "ca-app-pub-3940256099942544~3347511713");
    mInterstitialAd = new InterstitialAd(view.getContext());
    mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");

    AdRequest request = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();

    mInterstitialAd.loadAd(request);

    mInterstitialAd.setAdListener(new AdListener() {

        public void onAdClosed() {
            mInterstitialAd.loadAd(new AdRequest.Builder().build());
        }
    });

    }

    @Override
    public void onClick(View v)
    {
        mItemClickListener.onItemClick(v, getAdapterPosition());
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        }
    }
}

interface ItemClickListener {
    void onItemClick(View view, int position);
}
}
Bhavesh
  • 909
  • 2
  • 23
  • 38
-1

you can show progressdialog till the interstitial ad is not shown.

jigar savaliya
  • 474
  • 1
  • 8
  • 21