Mopub ads are not displaying

58
September 17, 2018, at 3:40 PM

I am a beginner at android. I am using the android studio to create an app. I have created a RecyclerView which fetches data from a json. I want to add mopub ads in between my RecyclerView. i used an adapter library provided by mopub. I am not getting any errors. but ads are not showing in my Fragment.

My Fragment Code

public class Tab1 extends Fragment implements ExampleAdapter.onItemClickListener {
    private RecyclerView mRecyclerView;
    private ExampleAdapter mExampleAdapter;
    private ArrayList<ExampleItem> mExampleList;
    private RequestQueue mRequestQueue;
    public static final String EXTRA_URL = "weburl";
    private ShimmerFrameLayout mShimmerViewContainer;
    private MoPubRecyclerAdapter mMoPubRecyclerAdapter;
    private static final String AD_UNIT_ID = "11a17b188668469fb0412708c3d16813";
    Button sharebuttonz;
    File imagePathz;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ((AppCompatActivity) getActivity()).getSupportActionBar().hide();
        View rootView = inflater.inflate(R.layout.tab1, container, false);
        mRecyclerView = rootView.findViewById(R.id.recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mShimmerViewContainer = rootView.findViewById(R.id.shimmer_view_container);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mExampleList = new ArrayList<>();
        mRequestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
        // Set the adapter here.
        // Use getActivity() instead of getContext()
        mExampleAdapter = new ExampleAdapter(getActivity(), mExampleList);
        mMoPubRecyclerAdapter = new MoPubRecyclerAdapter(getActivity(), mExampleAdapter, new MoPubNativeAdPositioning.MoPubClientPositioning().enableRepeatingPositions(2));
        ViewBinder mViewBinder = new ViewBinder.Builder(R.layout.native_ad_layout)
                .mainImageId(R.id.native_main_image)
                .iconImageId(R.id.native_icon_image)
                .titleId(R.id.native_title)
                .textId(R.id.native_text)
                .privacyInformationIconImageId(R.id.native_privacy_information_icon_image)
                .build();
        MoPubStaticNativeAdRenderer myRenderer = new MoPubStaticNativeAdRenderer(mViewBinder);
        mMoPubRecyclerAdapter.registerAdRenderer(myRenderer);
        // Set up the RecyclerView and start loading ads
        mRecyclerView.setAdapter(mMoPubRecyclerAdapter);
        mMoPubRecyclerAdapter.loadAds(AD_UNIT_ID);
        mExampleAdapter.setOnItemClickListener(Tab1.this);
        sharebuttonz = (Button) rootView.findViewById(R.id.sharebutton);//get id of button 1
        sharebuttonz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bitmap = takeScreenshot();
                saveBitmap(bitmap);
                shareIt();
            }
        });
        parseJson();
        return rootView;
    }
    public Bitmap takeScreenshot() {
        View rootView = getActivity().findViewById(android.R.id.content).getRootView();
        rootView.setDrawingCacheEnabled(true);
        return rootView.getDrawingCache();
    }
    public void saveBitmap(Bitmap bitmap) {
        imagePathz = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(imagePathz);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            Log.e("GREC", e.getMessage(), e);
        } catch (IOException e) {
            Log.e("GREC", e.getMessage(), e);
        }
    }
    private void shareIt() {
        Uri uri = Uri.fromFile(imagePathz);
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("image/*");
        String shareBody = "In Tweecher, My highest score with screen shot";
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Tweecher score");
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
        sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
        startActivity(Intent.createChooser(sharingIntent, "Share via"));
    }
    private void parseJson() {
        String url = "http://maranamassapp.cf/json_getdata.php";
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("server_response");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject ser = jsonArray.getJSONObject(i);
                                String creatorname = ser.getString("head");
                                String imageUrl = ser.getString("image");
                                String cat = ser.getString("content");
                                String weburlz = ser.getString("weburl");
                                String dateall = ser.getString("date");
                                int type = ser.getInt("type");
                                mExampleList.add(new ExampleItem(imageUrl, creatorname, cat, weburlz, dateall, type));
                            }

                            // Just call notifyDataSetChanged here
                            mExampleAdapter.notifyDataSetChanged();
                            mShimmerViewContainer.stopShimmerAnimation();
                            mShimmerViewContainer.setVisibility(View.GONE);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        mRequestQueue.add(request);
    }
    @Override
    public void onResume() {
        super.onResume();
        mShimmerViewContainer.startShimmerAnimation();
    }
    @Override
    public void onPause() {
        mShimmerViewContainer.stopShimmerAnimation();
        super.onPause();
    }
    public void onItemClick(int position) {
        Intent detailintent = new Intent(getContext(), DetailActivity.class);
        ExampleItem clickeditem = mExampleList.get(position);
        detailintent.putExtra(EXTRA_URL, clickeditem.getmWebUrl());
        startActivity(detailintent);
    }
    public void onDestroy() {
        super.onDestroy();
        mMoPubRecyclerAdapter.destroy();
        mMoPubRecyclerAdapter.setAdLoadedListener(null);
        mRecyclerView = null;
    }
}
READ ALSO
android imageview onclick behaviour

android imageview onclick behaviour

Can you create different actions for clicking on the same ImageView but different parts? Say I have an ImageView and I want for it to act differently if I click on the top of the ImageView and differently if I click on the middle part of the ImageViewCan...

77
How to get the values from the RecyclerView using Espresso

How to get the values from the RecyclerView using Espresso

I want to get the all values from the RecyclerView using EspressoI am able to get only the visible values from the recyclerView

46