ads Configuration - mayurparmar2/AlarmDemo GitHub Wiki

Configuration

test.java
 <!-- App Open | ca-app-pub-3940256099942544/3419835294 -->
 <!-- Banner | ca-app-pub-3940256099942544/6300978111 -->
 <!-- Interstitial | ca-app-pub-3940256099942544/1033173712 -->
 <!-- Interstitial Video | ca-app-pub-3940256099942544/8691691433 -->
 <!-- Rewarded | ca-app-pub-3940256099942544/5224354917 -->
 <!-- Rewarded Interstitial | ca-app-pub-3940256099942544/5354046379 -->
 <!-- Native Advanced | ca-app-pub-3940256099942544/2247696110 -->
 <!-- Native Advanced Video | ca-app-pub-3940256099942544/1044960115 -->
 <!--  https://mayurparmar2.github.io/ads/FlightTracker.json  -->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-3940256099942544/3419835294"/>

    <string name="Banner_ID">ca-app-pub-3940256099942544/6300978111</string>
    <string name="Native_Large_ID">ca-app-pub-3940256099942544/2247696110</string>
    <string name="Interstitial_ID">ca-app-pub-3940256099942544/1033173712</string>
    <string name="App_Open_ID">ca-app-pub-3940256099942544~3347511713</string>

Banner ads

Banner .xml
<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-3940256099942544/6300978111" />

Banner ads

Banner .java
private AdView adView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    adView = findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);
}

// Add the following code to handle the ad lifecycle

@Override
protected void onResume() {
    super.onResume();
    if (adView != null) {
        adView.resume();
    }
}

@Override
protected void onPause() {
    if (adView != null) {
        adView.pause();
    }
    super.onPause();
}

@Override
protected void onDestroy() {
    if (adView != null) {
        adView.destroy();
    }
    super.onDestroy();
}

interstitial ads

interstitial.java
// Inside your activity class
private InterstitialAd interstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AdRequest adRequest = new AdRequest.Builder().build();

    InterstitialAd.load(this, "YOUR_INTERSTITIAL_AD_UNIT_ID", adRequest, new InterstitialAdLoadCallback() {
        @Override
        public void onAdLoaded(@NonNull InterstitialAd ad) {
            interstitialAd = ad;
            showInterstitialAd();
        }

        @Override
        public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
            interstitialAd = null;
        }
    });
}

private void showInterstitialAd() {
    if (interstitialAd != null) {
        interstitialAd.show(this);
    }
}

Implement native ads xml

native.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/ad_headline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/ad_body"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp" />

    <!-- Add other views for the ad's icon, images, etc. -->

</LinearLayout>

Implement native ads adapter java

adapter.java
import com.google.android.gms.ads.nativead.NativeAd;
import com.google.android.gms.ads.nativead.NativeAdView;
import com.google.android.gms.ads.nativead.NativeAdViewHolder;

// Inside your adapter class

private static final int VIEW_TYPE_ITEM = 0;
private static final int VIEW_TYPE_NATIVE_AD = 1;

// ...

@Override
public int getItemViewType(int position) {
    // Return the view type based on the position
    // For example, return VIEW_TYPE_NATIVE_AD for the desired positions where you want to display native ads
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());

    if (viewType == VIEW_TYPE_NATIVE_AD) {
        View nativeAdViewLayout = inflater.inflate(R.layout.item_native_ad, parent, false);
        return new NativeAdViewHolder(nativeAdViewLayout);
    } else {
        // Inflate and return the ViewHolder for regular items
    }
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    if (holder.getItemViewType() == VIEW_TYPE_NATIVE_AD) {
        NativeAdViewHolder nativeAdViewHolder = (NativeAdViewHolder) holder;
        populateNativeAdView(nativeAdViewHolder.nativeAdView, nativeAd);
    } else {
        // Bind data to the ViewHolder for regular items
    }
}

// Helper method to populate native ad views
private void populateNativeAdView(NativeAdView nativeAdView, NativeAd nativeAd) {
    // Bind native ad data to the views in the nativeAdView layout
    // For example:
    TextView headlineView = nativeAdView.findViewById(R.id.ad_headline);
    TextView bodyView = nativeAdView.findViewById(R.id.ad_body);
    // Set the headline and body text from the native ad object
    headlineView.setText(nativeAd.getHeadline());
    bodyView.setText(nativeAd.getBody());

    // ...
}

Implement native ads main java

main.java
// Inside your activity or fragment
private List<Object> dataset;  // The dataset for the RecyclerView

private void loadNativeAd() {
    AdLoader adLoader = new AdLoader.Builder(this, "YOUR_NATIVE_AD_UNIT_ID")
        .forNativeAd(new NativeAd.OnNativeAdLoadedListener() {
            @Override
            public void onNativeAdLoaded(@NonNull NativeAd nativeAd) {
                // Insert the native ad into the dataset at the desired position
                int adPosition = 3;  // For example, insert the ad at position 3
                dataset.add(adPosition, nativeAd);

                // Notify the adapter of the dataset changes
                adapter.notifyDataSetChanged();
            }
        })
        .build();

    adLoader.loadAd(new AdRequest.Builder().build());
}

Rewarded Ads

RewardedAd.java
// Inside your activity class

private RewardedAd rewardedAd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    loadRewardedAd();
}

private void loadRewardedAd() {
    AdRequest adRequest = new AdRequest.Builder().build();

    RewardedAd.load(this, "YOUR_REWARDED_AD_UNIT_ID", adRequest, new RewardedAdLoadCallback() {
        @Override
        public void onAdLoaded(@NonNull RewardedAd ad) {
            rewardedAd = ad;

            // Add ad event listeners if needed

            showRewardedAd();
        }

        @Override
        public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
            rewardedAd = null;
        }
    });
}

private void showRewardedAd() {
    if (rewardedAd != null) {
        rewardedAd.show(this, new RewardedAdCallback() {
            @Override
            public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
                // Handle the reward given to the user
            }

            // Add other callback methods as needed
        });
    }
}

function to get Json java

Json.java
 public DataModel readJsonFromUrl(Context context, String url) {
        DataModel dataModel = new DataModel("", "", "", "", "", ""); // Create an instance of DataModel
        // Instantiate the RequestQueue.
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        // Create a JSON object request.
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        // JSON parsing and storing data in DataModel.
                        try {
                            JSONObject jsonObject = response.getJSONObject("ads");
                            // Store data in DataModel
                            dataModel.setBanner(jsonObject.getString("Banner"));
                            dataModel.setInterstitial(jsonObject.getString("Interstitial"));
                            dataModel.setRewardedInterstitial(jsonObject.getString("Rewarded_interstitial"));
                            dataModel.setRewarded(jsonObject.getString("Rewarded"));
                            dataModel.setNativeAdvanced(jsonObject.getString("Native_advanced"));
                            dataModel.setAppOpen(jsonObject.getString("App_open"));
                            // Access the stored data
                            Log.d(TAG, "Banner: " + dataModel.getBanner());
                            Log.d(TAG, "Interstitial: " + dataModel.getInterstitial());
                            Log.d(TAG, "Rewarded Interstitial: " + dataModel.getRewardedInterstitial());
                            Log.d(TAG, "Rewarded: " + dataModel.getRewarded());
                            Log.d(TAG, "Native Advanced: " + dataModel.getNativeAdvanced());
                            Log.d(TAG, "App Open: " + dataModel.getAppOpen());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // Handle error.
                        Log.e(TAG, "Error: " + error.getMessage());
                    }
                });
        // Add the request to the RequestQueue.
        requestQueue.add(jsonObjectRequest);
        return dataModel;
    }
⚠️ **GitHub.com Fallback** ⚠️