Skip to content

Commit 97ed26a

Browse files
Merge pull request #134 from MihaiCristianCondrea/codex/update-native-ads-to-match-design
feat(ads): support per-screen native ad layouts
2 parents b499dd9 + 2d20a05 commit 97ed26a

File tree

35 files changed

+91
-10
lines changed

35 files changed

+91
-10
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@ private static void populateNativeAdView(@NonNull NativeAd nativeAd, @NonNull Na
5252
TextView bodyView = adView.findViewById(R.id.ad_body);
5353
Button callToActionView = adView.findViewById(R.id.ad_call_to_action);
5454
ImageView iconView = adView.findViewById(R.id.ad_app_icon);
55+
TextView attributionView = adView.findViewById(R.id.ad_attribution);
5556

5657
adView.setMediaView(mediaView);
5758
adView.setHeadlineView(headlineView);
5859
adView.setBodyView(bodyView);
5960
adView.setCallToActionView(callToActionView);
6061
adView.setIconView(iconView);
62+
adView.setAdvertiserView(attributionView);
6163

6264
if (headlineView != null) {
6365
headlineView.setText(nativeAd.getHeadline());
@@ -81,6 +83,15 @@ private static void populateNativeAdView(@NonNull NativeAd nativeAd, @NonNull Na
8183
}
8284
}
8385

86+
if (attributionView != null) {
87+
String adLabel = adView.getContext().getString(R.string.ad);
88+
if (nativeAd.getAdvertiser() == null) {
89+
attributionView.setText(adLabel);
90+
} else {
91+
attributionView.setText(adLabel + " " + nativeAd.getAdvertiser());
92+
}
93+
}
94+
8495
if (iconView != null) {
8596
if (nativeAd.getIcon() == null) {
8697
iconView.setVisibility(View.GONE);

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.d4rk.androidtutorials.java.ads.views;
22

33
import android.content.Context;
4+
import android.content.res.TypedArray;
45
import android.util.AttributeSet;
56
import android.widget.FrameLayout;
67

8+
import androidx.annotation.LayoutRes;
79
import androidx.annotation.NonNull;
810
import androidx.annotation.Nullable;
911

12+
import com.d4rk.androidtutorials.java.R;
1013
import com.google.android.gms.ads.AdRequest;
1114
import com.d4rk.androidtutorials.java.ads.managers.NativeAdLoader;
1215

@@ -16,19 +19,36 @@
1619
*/
1720
public class NativeAdBannerView extends FrameLayout {
1821

22+
private int layoutRes = R.layout.native_ad;
23+
1924
public NativeAdBannerView(@NonNull Context context) {
2025
super(context);
26+
init(context, null, 0);
2127
}
2228

2329
public NativeAdBannerView(@NonNull Context context, @Nullable AttributeSet attrs) {
2430
super(context, attrs);
31+
init(context, attrs, 0);
2532
}
2633

2734
public NativeAdBannerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
2835
super(context, attrs, defStyleAttr);
36+
init(context, attrs, defStyleAttr);
37+
}
38+
39+
private void init(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
40+
if (attrs != null) {
41+
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NativeAdBannerView, defStyleAttr, 0);
42+
layoutRes = a.getResourceId(R.styleable.NativeAdBannerView_nativeAdLayout, R.layout.native_ad);
43+
a.recycle();
44+
}
2945
}
3046

3147
public void loadAd(AdRequest adRequest) {
32-
NativeAdLoader.load(getContext(), this);
48+
NativeAdLoader.load(getContext(), this, layoutRes);
49+
}
50+
51+
public void setNativeAdLayout(@LayoutRes int layoutRes) {
52+
this.layoutRes = layoutRes;
3353
}
3454
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
android:id="@+id/ad_view"
5858
android:layout_width="match_parent"
5959
android:layout_height="match_parent"
60-
/>
60+
app:nativeAdLayout="@layout/native_ad" />
6161
</FrameLayout>
6262

6363
<com.google.android.material.bottomnavigation.BottomNavigationView

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
android:layout_height="wrap_content"
127127
android:layout_marginTop="16dp"
128128
android:layout_marginBottom="16dp"
129-
/>
129+
app:nativeAdLayout="@layout/native_ad" />
130130
</androidx.appcompat.widget.LinearLayoutCompat>
131131
</me.zhanghai.android.fastscroll.FastScrollScrollView>
132-
</androidx.constraintlayout.widget.ConstraintLayout>
132+
</androidx.constraintlayout.widget.ConstraintLayout>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@
173173
android:layout_width="match_parent"
174174
android:layout_height="wrap_content"
175175
android:layout_marginTop="16dp"
176-
app:layout_constraintTop_toBottomOf="@id/card_view_about" />
176+
app:layout_constraintTop_toBottomOf="@id/card_view_about"
177+
app:nativeAdLayout="@layout/native_ad" />
177178

178179
<com.airbnb.lottie.LottieAnimationView
179180
android:id="@+id/animation_about"

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
android:layout_height="wrap_content"
150150
android:layout_marginTop="16dp"
151151
android:layout_marginBottom="16dp"
152-
/>
152+
app:nativeAdLayout="@layout/native_ad" />
153153

154154
<androidx.appcompat.widget.LinearLayoutCompat
155155
android:id="@+id/other_apps_section"
@@ -219,7 +219,7 @@
219219
android:layout_width="match_parent"
220220
android:layout_height="wrap_content"
221221
android:layout_marginTop="16dp"
222-
/>
222+
app:nativeAdLayout="@layout/native_ad" />
223223

224224
<com.airbnb.lottie.LottieAnimationView
225225
android:id="@+id/learningAnimation"

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
android:orientation="vertical"
1818
android:padding="8dp">
1919

20+
<TextView
21+
android:id="@+id/ad_attribution"
22+
android:layout_width="wrap_content"
23+
android:layout_height="wrap_content"
24+
android:text="@string/ad"
25+
android:textAppearance="@style/TextAppearance.Material3.LabelSmall" />
26+
2027
<com.google.android.gms.ads.nativead.MediaView
2128
android:id="@+id/ad_media"
2229
android:layout_width="match_parent"

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,20 @@
1616
android:layout_height="match_parent"
1717
android:padding="8dp">
1818

19+
<com.google.android.material.textview.MaterialTextView
20+
android:id="@+id/ad_attribution"
21+
android:layout_width="wrap_content"
22+
android:layout_height="wrap_content"
23+
android:text="@string/ad"
24+
android:textAppearance="@style/TextAppearance.Material3.LabelSmall"
25+
app:layout_constraintTop_toTopOf="parent"
26+
app:layout_constraintStart_toStartOf="parent" />
27+
1928
<androidx.appcompat.widget.AppCompatImageView
2029
android:id="@+id/ad_app_icon"
2130
android:layout_width="48dp"
2231
android:layout_height="48dp"
23-
app:layout_constraintTop_toTopOf="parent"
32+
app:layout_constraintTop_toBottomOf="@id/ad_attribution"
2433
app:layout_constraintStart_toStartOf="parent"
2534
app:layout_constraintEnd_toEndOf="parent" />
2635

app/src/main/res/values-ar-rEG/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<string name="get_on_google_play">نزّله من على Google Play</string>
2222
<string name="learn_more">اعرف المزيد</string>
2323
<string name="play_store">متجر Play</string>
24+
<string name="ad">إعلان</string>
2425
<string name="search_tutorials_hint">ابحث عن الدروس</string>
2526
<string name="search_tutorials_content_description">ابحث عن الدروس</string>
2627
<string name="search_lessons_hint">ابحث عن الدروس</string>

app/src/main/res/values-bg-rBG/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<string name="get_on_google_play">Вземете от Google Play</string>
2525
<string name="learn_more">Научете повече</string>
2626
<string name="play_store">Play Store</string>
27+
<string name="ad">Реклама</string>
2728
<string name="search_tutorials_hint">Търсене на уроци</string>
2829
<string name="search_tutorials_content_description">Търсене на уроци</string>
2930
<string name="search_lessons_hint">Търсене на уроци</string>

0 commit comments

Comments
 (0)