Skip to content

Commit c2968b9

Browse files
Refactor Ad Unit ID handling for Native Ads
This commit introduces the following changes: - Adds a new XML file `admob_ids.xml` to store AdMob ad unit IDs. - Adds a `nativeAdUnitId` attribute to the `NativeAdBannerView` custom view. - Updates `NativeAdBannerView` to use the `nativeAdUnitId` attribute or a fallback ID. - Updates `NativeAdLoader` to accept an optional `adUnitId` parameter and use a fallback if not provided. - Sets `nativeAdUnitId` for `NativeAdBannerView` instances in various layouts: - `lesson_description_section.xml` - `fragment_about.xml` - `activity_support.xml` - `fragment_code.xml` - `activity_help.xml` - `fragment_no_code.xml` - `fragment_same_code.xml` - `fragment_home.xml` - Updates `AndroidStudioFragment.java` to set the `nativeAdUnitId` programmatically for an ad view. - Updates `AppOpenAdManager.java` to load the ad unit ID from `admob_ids.xml`. - Removes ad unit ID string resources from `untranslatable_strings.xml` as they are now in `admob_ids.xml`.
1 parent 4a03669 commit c2968b9

File tree

15 files changed

+69
-9
lines changed

15 files changed

+69
-9
lines changed

app/src/main/java/com/d4rk/androidtutorials/java/ads/managers/AppOpenAd.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import androidx.lifecycle.OnLifecycleEvent;
1414
import androidx.lifecycle.ProcessLifecycleOwner;
1515

16+
import com.d4rk.androidtutorials.java.R;
1617
import com.d4rk.androidtutorials.java.ads.AdUtils;
1718
import com.google.android.gms.ads.AdError;
1819
import com.google.android.gms.ads.AdRequest;
@@ -83,15 +84,16 @@ public interface OnShowAdCompleteListener {
8384
}
8485

8586
private static class AppOpenAdManager {
86-
private static final String AD_UNIT_ID = "ca-app-pub-5294151573817700/9123330876";
8787
private final Application application;
88+
private final String adUnitId;
8889
private com.google.android.gms.ads.appopen.AppOpenAd appOpenAd = null;
8990
private boolean isLoadingAd = false;
9091
private boolean isShowingAd = false;
9192
private long loadTime = 0;
9293

9394
public AppOpenAdManager(Application application) {
9495
this.application = application;
96+
this.adUnitId = application.getString(R.string.app_open_ad_unit_id);
9597
}
9698

9799
private void loadAd(Context context) {
@@ -101,7 +103,7 @@ private void loadAd(Context context) {
101103
}
102104
isLoadingAd = true;
103105
AdRequest request = new AdRequest.Builder().build();
104-
com.google.android.gms.ads.appopen.AppOpenAd.load(context, AD_UNIT_ID, request, new AppOpenAdLoadCallback() {
106+
com.google.android.gms.ads.appopen.AppOpenAd.load(context, adUnitId, request, new AppOpenAdLoadCallback() {
105107
@Override
106108
public void onAdLoaded(@NonNull com.google.android.gms.ads.appopen.AppOpenAd ad) {
107109
appOpenAd = ad;

app/src/main/java/com/d4rk/androidtutorials/java/ads/managers/NativeAdLoader.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.d4rk.androidtutorials.java.ads.managers;
22

33
import android.content.Context;
4+
import android.text.TextUtils;
45
import android.util.Log;
56
import android.view.LayoutInflater;
67
import android.view.View;
@@ -30,26 +31,39 @@ public class NativeAdLoader {
3031
private static final String TAG = "NativeAdLoader";
3132

3233
public static void load(@NonNull Context context, @NonNull ViewGroup container) {
33-
load(context, container, R.layout.ad_home_banner_large, new AdRequest.Builder().build(), null);
34+
load(context, container, R.layout.ad_home_banner_large, null, new AdRequest.Builder().build(), null);
3435
}
3536

3637
public static void load(@NonNull Context context, @NonNull ViewGroup container, @LayoutRes int layoutRes) {
37-
load(context, container, layoutRes, new AdRequest.Builder().build(), null);
38+
load(context, container, layoutRes, null, new AdRequest.Builder().build(), null);
3839
}
3940

4041
public static void load(@NonNull Context context,
4142
@NonNull ViewGroup container,
4243
@LayoutRes int layoutRes,
4344
@androidx.annotation.Nullable AdListener listener) {
44-
load(context, container, layoutRes, new AdRequest.Builder().build(), listener);
45+
load(context, container, layoutRes, null, new AdRequest.Builder().build(), listener);
4546
}
4647

4748
public static void load(@NonNull Context context,
4849
@NonNull ViewGroup container,
4950
@LayoutRes int layoutRes,
5051
@NonNull AdRequest adRequest,
5152
@androidx.annotation.Nullable AdListener listener) {
52-
AdLoader.Builder builder = new AdLoader.Builder(context, context.getString(R.string.native_ad_banner_unit_id))
53+
load(context, container, layoutRes, null, adRequest, listener);
54+
}
55+
56+
public static void load(@NonNull Context context,
57+
@NonNull ViewGroup container,
58+
@LayoutRes int layoutRes,
59+
@androidx.annotation.Nullable String adUnitId,
60+
@NonNull AdRequest adRequest,
61+
@androidx.annotation.Nullable AdListener listener) {
62+
String resolvedAdUnitId = TextUtils.isEmpty(adUnitId)
63+
? context.getString(R.string.native_ad_fallback_unit_id)
64+
: adUnitId;
65+
66+
AdLoader.Builder builder = new AdLoader.Builder(context, resolvedAdUnitId)
5367
.forNativeAd(nativeAd -> {
5468
LayoutInflater inflater = LayoutInflater.from(context);
5569
NativeAdView adView = (NativeAdView) inflater.inflate(layoutRes, container, false);

app/src/main/java/com/d4rk/androidtutorials/java/ads/views/NativeAdBannerView.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
import android.content.Context;
44
import android.content.res.TypedArray;
5+
import android.text.TextUtils;
56
import android.util.AttributeSet;
67
import android.widget.FrameLayout;
78

89
import androidx.annotation.LayoutRes;
910
import androidx.annotation.NonNull;
1011
import androidx.annotation.Nullable;
12+
import androidx.annotation.StringRes;
1113

1214
import com.d4rk.androidtutorials.java.R;
1315
import com.d4rk.androidtutorials.java.ads.managers.NativeAdLoader;
@@ -21,6 +23,8 @@
2123
public class NativeAdBannerView extends FrameLayout {
2224

2325
private int layoutRes = R.layout.ad_home_banner_large;
26+
@Nullable
27+
private String adUnitId;
2428

2529
public NativeAdBannerView(@NonNull Context context) {
2630
super(context);
@@ -38,9 +42,15 @@ public NativeAdBannerView(@NonNull Context context, @Nullable AttributeSet attrs
3842
}
3943

4044
private void init(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
45+
adUnitId = context.getString(R.string.native_ad_fallback_unit_id);
46+
4147
if (attrs != null) {
4248
try (TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NativeAdBannerView, defStyleAttr, 0)) {
4349
layoutRes = a.getResourceId(R.styleable.NativeAdBannerView_nativeAdLayout, R.layout.ad_home_banner_large);
50+
CharSequence adUnitValue = a.getText(R.styleable.NativeAdBannerView_nativeAdUnitId);
51+
if (!TextUtils.isEmpty(adUnitValue)) {
52+
adUnitId = adUnitValue.toString();
53+
}
4454
}
4555
}
4656
}
@@ -58,10 +68,22 @@ public void loadAd(@NonNull AdRequest request) {
5868
}
5969

6070
public void loadAd(@NonNull AdRequest request, @Nullable AdListener listener) {
61-
NativeAdLoader.load(getContext(), this, layoutRes, request, listener);
71+
NativeAdLoader.load(getContext(), this, layoutRes, adUnitId, request, listener);
6272
}
6373

6474
public void setNativeAdLayout(@LayoutRes int layoutRes) {
6575
this.layoutRes = layoutRes;
6676
}
77+
78+
public void setNativeAdUnitId(@Nullable String adUnitId) {
79+
if (TextUtils.isEmpty(adUnitId)) {
80+
this.adUnitId = getContext().getString(R.string.native_ad_fallback_unit_id);
81+
} else {
82+
this.adUnitId = adUnitId;
83+
}
84+
}
85+
86+
public void setNativeAdUnitId(@StringRes int adUnitIdRes) {
87+
setNativeAdUnitId(getContext().getString(adUnitIdRes));
88+
}
6789
}

app/src/main/java/com/d4rk/androidtutorials/java/ui/screens/android/AndroidStudioFragment.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
372372
ViewGroup.LayoutParams.MATCH_PARENT,
373373
ViewGroup.LayoutParams.WRAP_CONTENT));
374374
adView.setNativeAdLayout(R.layout.ad_android_studio_list);
375+
adView.setNativeAdUnitId(R.string.native_ad_lessons_list_unit_id);
375376
return new AdHolder(adView);
376377
} else if (viewType == TYPE_CATEGORY) {
377378
View view = LayoutInflater.from(parent.getContext())

app/src/main/res/layout/activity_help.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
android:layout_width="match_parent"
5151
android:layout_height="wrap_content"
5252
android:layout_marginVertical="2dp"
53+
app:nativeAdUnitId="@string/native_ad_help_unit_id"
5354
app:nativeAdLayout="@layout/ad_help" />
5455

5556
<com.google.android.material.card.MaterialCardView

app/src/main/res/layout/activity_support.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
android:layout_width="match_parent"
124124
android:layout_height="wrap_content"
125125
android:layout_marginTop="16dp"
126+
app:nativeAdUnitId="@string/native_ad_support_unit_id"
126127
app:nativeAdLayout="@layout/ad_support" />
127128

128129
<com.google.android.gms.ads.AdView

app/src/main/res/layout/fragment_about.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@
167167
android:layout_width="match_parent"
168168
android:layout_height="wrap_content"
169169
android:layout_marginTop="2dp"
170+
app:nativeAdUnitId="@string/native_ad_about_unit_id"
170171
app:nativeAdLayout="@layout/ad_about" />
171172

172173
<com.airbnb.lottie.LottieAnimationView

app/src/main/res/layout/fragment_code.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@
3636
app:layout_constraintBottom_toBottomOf="parent"
3737
app:layout_constraintEnd_toEndOf="parent"
3838
app:layout_constraintStart_toStartOf="parent"
39+
app:nativeAdUnitId="@string/native_ad_code_example_unit_id"
3940
app:nativeAdLayout="@layout/ad_lesson_code" />
4041
</androidx.constraintlayout.widget.ConstraintLayout>

app/src/main/res/layout/fragment_home.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
android:layout_marginHorizontal="16dp"
151151
android:layout_marginTop="16dp"
152152
android:layout_marginBottom="16dp"
153+
app:nativeAdUnitId="@string/native_ad_home_unit_id"
153154
app:nativeAdLayout="@layout/ad_home_banner_large" />
154155

155156
<androidx.appcompat.widget.LinearLayoutCompat
@@ -218,6 +219,7 @@
218219
android:id="@+id/small_banner_ad"
219220
android:layout_width="match_parent"
220221
android:layout_height="wrap_content"
222+
app:nativeAdUnitId="@string/native_ad_home_unit_id"
221223
app:nativeAdLayout="@layout/ad_home_banner_small" />
222224

223225
<com.airbnb.lottie.LottieAnimationView

app/src/main/res/layout/fragment_no_code.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@
3333
app:layout_constraintBottom_toBottomOf="parent"
3434
app:layout_constraintEnd_toEndOf="parent"
3535
app:layout_constraintStart_toStartOf="parent"
36+
app:nativeAdUnitId="@string/native_ad_code_example_unit_id"
3637
app:nativeAdLayout="@layout/ad_lesson_code" />
3738
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)