Skip to content

Commit 7a5ccf8

Browse files
Merge pull request #138 from MihaiCristianCondrea/codex/rename-native_ad-and-create-small-ad-layout
Add small home banner ad layout and update native ad references
2 parents 4e298ce + 183dc29 commit 7a5ccf8

File tree

12 files changed

+193
-41
lines changed

12 files changed

+193
-41
lines changed

AGENTS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,8 @@ You are an experienced Android app developer.
1616

1717
## Testing
1818
- Run `./gradlew test` before committing changes.
19+
20+
## Native ads
21+
- Native ad XML layouts should wrap their content in a `MaterialCardView` with the ID `ad_card` using `@style/Widget.Material3.CardView.Outlined` and appropriate rounded corner overlays.
22+
- Use the shared `@layout/ad_attribution` snippet for displaying the ad attribution text.
23+
- Place the attribution include as the first child of the ad card and override its padding or text color attributes to fit each layout.

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
public class NativeAdLoader {
2525

2626
public static void load(@NonNull Context context, @NonNull ViewGroup container) {
27-
load(context, container, R.layout.native_ad);
27+
load(context, container, R.layout.large_home_banner_native_ad);
2828
}
2929

3030
public static void load(@NonNull Context context, @NonNull ViewGroup container, @LayoutRes int layoutRes) {
@@ -58,7 +58,9 @@ private static void populateNativeAdView(@NonNull NativeAd nativeAd, @NonNull Na
5858
adView.setHeadlineView(headlineView);
5959
adView.setBodyView(bodyView);
6060
adView.setCallToActionView(callToActionView);
61-
adView.setIconView(iconView);
61+
if (iconView != null) {
62+
adView.setIconView(iconView);
63+
}
6264
adView.setAdvertiserView(attributionView);
6365

6466
if (headlineView != null) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020
public class NativeAdBannerView extends FrameLayout {
2121

22-
private int layoutRes = R.layout.native_ad;
22+
private int layoutRes = R.layout.large_home_banner_native_ad;
2323

2424
public NativeAdBannerView(@NonNull Context context) {
2525
super(context);
@@ -39,7 +39,7 @@ public NativeAdBannerView(@NonNull Context context, @Nullable AttributeSet attrs
3939
private void init(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
4040
if (attrs != null) {
4141
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NativeAdBannerView, defStyleAttr, 0);
42-
layoutRes = a.getResourceId(R.styleable.NativeAdBannerView_nativeAdLayout, R.layout.native_ad);
42+
layoutRes = a.getResourceId(R.styleable.NativeAdBannerView_nativeAdLayout, R.layout.large_home_banner_native_ad);
4343
a.recycle();
4444
}
4545
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
3+
<solid android:color="@android:color/transparent" />
4+
<stroke android:width="1dp" android:color="?attr/colorPrimary" />
5+
<corners android:radius="4dp" />
6+
</shape>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<FrameLayout
4545
android:id="@+id/ad_container"
4646
android:layout_width="match_parent"
47-
android:layout_height="50dp"
47+
android:layout_height="wrap_content"
4848
app:layout_constraintBottom_toTopOf="@+id/nav_view">
4949

5050
<View
@@ -57,7 +57,7 @@
5757
android:id="@+id/ad_view"
5858
android:layout_width="match_parent"
5959
android:layout_height="match_parent"
60-
app:nativeAdLayout="@layout/native_ad" />
60+
app:nativeAdLayout="@layout/bottom_app_bar_native_ad" />
6161
</FrameLayout>
6262

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

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

Lines changed: 1 addition & 1 deletion
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-
app:nativeAdLayout="@layout/native_ad" />
129+
app:nativeAdLayout="@layout/large_home_banner_native_ad" />
130130
</androidx.appcompat.widget.LinearLayoutCompat>
131131
</me.zhanghai.android.fastscroll.FastScrollScrollView>
132132
</androidx.constraintlayout.widget.ConstraintLayout>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:id="@+id/ad_attribution"
4+
android:layout_width="wrap_content"
5+
android:layout_height="wrap_content"
6+
android:background="@drawable/bg_ad_attribution"
7+
android:paddingHorizontal="4dp"
8+
android:paddingVertical="2dp"
9+
android:text="@string/ad"
10+
android:textColor="?attr/colorPrimary"
11+
android:textAppearance="@style/TextAppearance.Material3.LabelSmall" />
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<com.google.android.gms.ads.nativead.NativeAdView xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="wrap_content">
5+
6+
<LinearLayout
7+
android:id="@+id/ad_host"
8+
android:layout_width="match_parent"
9+
android:layout_height="wrap_content"
10+
android:orientation="vertical"
11+
android:background="?attr/colorSurfaceContainer"
12+
android:paddingStart="16dp"
13+
android:paddingEnd="16dp"
14+
android:paddingTop="12dp"
15+
android:paddingBottom="12dp">
16+
17+
<include layout="@layout/ad_attribution" />
18+
19+
<LinearLayout
20+
android:layout_width="match_parent"
21+
android:layout_height="wrap_content"
22+
android:gravity="center_vertical"
23+
android:orientation="horizontal">
24+
25+
<ImageView
26+
android:id="@+id/ad_app_icon"
27+
android:layout_width="32dp"
28+
android:layout_height="32dp"
29+
android:layout_marginEnd="12dp" />
30+
31+
<LinearLayout
32+
android:layout_width="0dp"
33+
android:layout_height="wrap_content"
34+
android:layout_weight="1"
35+
android:orientation="vertical">
36+
37+
<TextView
38+
android:id="@+id/ad_headline"
39+
android:layout_width="wrap_content"
40+
android:layout_height="wrap_content"
41+
android:ellipsize="end"
42+
android:maxLines="1"
43+
android:textAppearance="@style/TextAppearance.Material3.BodyMedium" />
44+
45+
<TextView
46+
android:id="@+id/ad_body"
47+
android:layout_width="wrap_content"
48+
android:layout_height="wrap_content"
49+
android:ellipsize="end"
50+
android:maxLines="1"
51+
android:textAppearance="@style/TextAppearance.Material3.BodySmall" />
52+
</LinearLayout>
53+
54+
<com.google.android.material.button.MaterialButton
55+
android:id="@+id/ad_call_to_action"
56+
style="@style/Widget.Material3.Button.TextButton"
57+
android:layout_width="wrap_content"
58+
android:layout_height="wrap_content"
59+
android:minWidth="64dp"
60+
android:minHeight="36dp"
61+
android:layout_marginStart="8dp" />
62+
</LinearLayout>
63+
</LinearLayout>
64+
</com.google.android.gms.ads.nativead.NativeAdView>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@
174174
android:layout_height="wrap_content"
175175
android:layout_marginTop="16dp"
176176
app:layout_constraintTop_toBottomOf="@id/card_view_about"
177-
app:nativeAdLayout="@layout/native_ad" />
177+
app:nativeAdLayout="@layout/large_home_banner_native_ad" />
178178

179179
<com.airbnb.lottie.LottieAnimationView
180180
android:id="@+id/animation_about"

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
android:layout_marginHorizontal="16dp"
151151
android:layout_marginTop="16dp"
152152
android:layout_marginBottom="16dp"
153-
app:nativeAdLayout="@layout/native_ad" />
153+
app:nativeAdLayout="@layout/large_home_banner_native_ad" />
154154

155155
<androidx.appcompat.widget.LinearLayoutCompat
156156
android:id="@+id/other_apps_section"
@@ -188,7 +188,8 @@
188188
android:layout_width="match_parent"
189189
android:layout_height="wrap_content"
190190
android:layout_marginHorizontal="16dp"
191-
android:layout_marginBottom="16dp">
191+
android:layout_marginBottom="0dp"
192+
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.CardViewTopRounded">
192193

193194
<androidx.appcompat.widget.LinearLayoutCompat
194195
android:layout_width="match_parent"
@@ -219,9 +220,7 @@
219220
android:id="@+id/small_banner_ad"
220221
android:layout_width="match_parent"
221222
android:layout_height="wrap_content"
222-
android:layout_marginHorizontal="16dp"
223-
android:layout_marginTop="16dp"
224-
app:nativeAdLayout="@layout/native_ad" />
223+
app:nativeAdLayout="@layout/small_home_banner_native_ad" />
225224

226225
<com.airbnb.lottie.LottieAnimationView
227226
android:id="@+id/learningAnimation"

0 commit comments

Comments
 (0)