Skip to content

Commit 97575b7

Browse files
Unify ad attribution styling and refine native banners
1 parent f9ffb25 commit 97575b7

File tree

8 files changed

+66
-78
lines changed

8 files changed

+66
-78
lines changed

AGENTS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ You are an experienced Android app developer.
2020
## Native ads
2121
- 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.
2222
- 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, background, or text color attributes to fit each layout.
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
33
<solid android:color="@android:color/transparent" />
4-
<stroke android:width="1dp" android:color="?attr/colorOutline" />
4+
<stroke android:width="1dp" android:color="?attr/colorPrimary" />
55
<corners android:radius="4dp" />
66
</shape>

app/src/main/res/drawable/bg_ad_attribution_large.xml

Lines changed: 0 additions & 6 deletions
This file was deleted.

app/src/main/res/drawable/bg_ad_attribution_small.xml

Lines changed: 0 additions & 6 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
android:paddingHorizontal="4dp"
88
android:paddingVertical="2dp"
99
android:text="@string/ad"
10-
android:textColor="?attr/colorOutline"
10+
android:textColor="?attr/colorPrimary"
1111
android:textAppearance="@style/TextAppearance.Material3.LabelSmall" />

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

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
layout="@layout/ad_attribution"
1616
android:layout_margin="16dp"
1717
android:layout_gravity="top|start"
18-
android:background="@drawable/bg_ad_attribution_large"
1918
android:paddingHorizontal="8dp"
2019
android:paddingVertical="4dp" />
2120

@@ -43,36 +42,40 @@
4342
<LinearLayout
4443
android:layout_width="match_parent"
4544
android:layout_height="wrap_content"
45+
android:layout_marginTop="8dp"
4646
android:gravity="center_vertical"
4747
android:orientation="horizontal">
4848

49-
<ImageView
50-
android:id="@+id/ad_app_icon"
51-
android:layout_width="40dp"
52-
android:layout_height="40dp"
53-
android:layout_marginEnd="8dp" />
54-
55-
<TextView
56-
android:id="@+id/ad_headline"
49+
<LinearLayout
5750
android:layout_width="0dp"
5851
android:layout_height="wrap_content"
5952
android:layout_weight="1"
60-
android:textAppearance="@style/TextAppearance.Material3.TitleMedium" />
61-
</LinearLayout>
53+
android:orientation="vertical">
6254

63-
<TextView
64-
android:id="@+id/ad_body"
65-
android:layout_width="match_parent"
66-
android:layout_height="wrap_content"
67-
android:layout_marginTop="4dp"
68-
android:textAppearance="@style/TextAppearance.Material3.BodySmall" />
55+
<TextView
56+
android:id="@+id/ad_headline"
57+
android:layout_width="match_parent"
58+
android:layout_height="wrap_content"
59+
android:ellipsize="end"
60+
android:maxLines="1"
61+
android:textAppearance="@style/TextAppearance.Material3.TitleMedium" />
6962

70-
<com.google.android.material.button.MaterialButton
71-
android:id="@+id/ad_call_to_action"
72-
android:layout_width="wrap_content"
73-
android:layout_height="wrap_content"
74-
android:layout_gravity="end"
75-
android:layout_marginTop="8dp" />
63+
<TextView
64+
android:id="@+id/ad_body"
65+
android:layout_width="match_parent"
66+
android:layout_height="wrap_content"
67+
android:ellipsize="end"
68+
android:maxLines="1"
69+
android:layout_marginTop="4dp"
70+
android:textAppearance="@style/TextAppearance.Material3.BodySmall" />
71+
</LinearLayout>
72+
73+
<com.google.android.material.button.MaterialButton
74+
android:id="@+id/ad_call_to_action"
75+
android:layout_width="wrap_content"
76+
android:layout_height="wrap_content"
77+
android:layout_marginStart="8dp" />
78+
</LinearLayout>
7679
</LinearLayout>
7780
</com.google.android.material.card.MaterialCardView>
7881
</com.google.android.gms.ads.nativead.NativeAdView>

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

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,58 +14,53 @@
1414
android:layout_marginBottom="16dp"
1515
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.CardViewBottomRounded">
1616

17+
<include
18+
layout="@layout/ad_attribution"
19+
android:layout_gravity="top|start"
20+
android:layout_margin="8dp" />
21+
1722
<LinearLayout
1823
android:layout_width="match_parent"
1924
android:layout_height="wrap_content"
20-
android:orientation="vertical"
25+
android:orientation="horizontal"
26+
android:gravity="center_vertical"
2127
android:padding="16dp">
2228

23-
<include
24-
layout="@layout/ad_attribution"
25-
android:background="@drawable/bg_ad_attribution_small"
26-
android:paddingHorizontal="4dp"
27-
android:paddingVertical="2dp"
28-
android:textColor="?attr/colorPrimary" />
29+
<ImageView
30+
android:id="@+id/ad_app_icon"
31+
android:layout_width="40dp"
32+
android:layout_height="40dp"
33+
android:layout_marginEnd="16dp" />
2934

3035
<LinearLayout
31-
android:layout_width="match_parent"
36+
android:layout_width="0dp"
3237
android:layout_height="wrap_content"
33-
android:layout_marginTop="8dp"
34-
android:gravity="center_vertical"
35-
android:orientation="horizontal">
36-
37-
<ImageView
38-
android:id="@+id/ad_app_icon"
39-
android:layout_width="40dp"
40-
android:layout_height="40dp"
41-
android:layout_marginEnd="16dp" />
38+
android:layout_weight="1"
39+
android:orientation="vertical">
4240

43-
<LinearLayout
44-
android:layout_width="0dp"
41+
<TextView
42+
android:id="@+id/ad_headline"
43+
android:layout_width="wrap_content"
4544
android:layout_height="wrap_content"
46-
android:layout_weight="1"
47-
android:orientation="vertical">
48-
49-
<TextView
50-
android:id="@+id/ad_headline"
51-
android:layout_width="wrap_content"
52-
android:layout_height="wrap_content"
53-
android:textAppearance="@style/TextAppearance.Material3.TitleMedium" />
54-
55-
<TextView
56-
android:id="@+id/ad_body"
57-
android:layout_width="wrap_content"
58-
android:layout_height="wrap_content"
59-
android:textAppearance="@style/TextAppearance.Material3.BodySmall" />
60-
</LinearLayout>
45+
android:ellipsize="end"
46+
android:maxLines="1"
47+
android:textAppearance="@style/TextAppearance.Material3.TitleMedium" />
6148

62-
<com.google.android.material.button.MaterialButton
63-
android:id="@+id/ad_call_to_action"
64-
style="@style/Widget.Material3.Button.TextButton"
49+
<TextView
50+
android:id="@+id/ad_body"
6551
android:layout_width="wrap_content"
66-
android:layout_height="wrap_content" />
52+
android:layout_height="wrap_content"
53+
android:ellipsize="end"
54+
android:maxLines="1"
55+
android:textAppearance="@style/TextAppearance.Material3.BodySmall" />
6756
</LinearLayout>
57+
58+
<com.google.android.material.button.MaterialButton
59+
android:id="@+id/ad_call_to_action"
60+
style="@style/Widget.Material3.Button.TextButton"
61+
android:layout_width="wrap_content"
62+
android:layout_height="wrap_content"
63+
android:layout_marginStart="8dp" />
6864
</LinearLayout>
6965
</com.google.android.material.card.MaterialCardView>
70-
7166
</com.google.android.gms.ads.nativead.NativeAdView>

0 commit comments

Comments
 (0)