Skip to content

Commit a3480c0

Browse files
Merge pull request #144 from MihaiCristianCondrea/codex/create-new-native-ads-for-lessons
Add lesson code native ad layout with rounded styling
2 parents 0020083 + 1cc98fd commit a3480c0

File tree

9 files changed

+156
-34
lines changed

9 files changed

+156
-34
lines changed

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
@@ -77,7 +77,9 @@ private static void populateNativeAdView(@NonNull NativeAd nativeAd, @NonNull Na
7777
ImageView iconView = adView.findViewById(R.id.ad_app_icon);
7878
TextView attributionView = adView.findViewById(R.id.ad_attribution);
7979

80-
adView.setMediaView(mediaView);
80+
if (mediaView != null) {
81+
adView.setMediaView(mediaView);
82+
}
8183
adView.setHeadlineView(headlineView);
8284
adView.setBodyView(bodyView);
8385
adView.setCallToActionView(callToActionView);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
android:layout_width="match_parent"
5+
android:layout_height="wrap_content">
6+
7+
<com.google.android.material.card.MaterialCardView
8+
android:id="@+id/ad_card"
9+
style="@style/Widget.Material3.CardView.Outlined"
10+
android:layout_width="match_parent"
11+
android:layout_height="wrap_content"
12+
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.CardViewBottomRounded">
13+
14+
<LinearLayout
15+
android:layout_width="match_parent"
16+
android:layout_height="wrap_content"
17+
android:orientation="vertical"
18+
android:paddingVertical="4dp"
19+
android:paddingHorizontal="16dp">
20+
21+
<include layout="@layout/ad_attribution" />
22+
23+
<LinearLayout
24+
android:layout_width="match_parent"
25+
android:layout_height="wrap_content"
26+
android:layout_marginTop="8dp"
27+
android:gravity="center_vertical"
28+
android:orientation="horizontal">
29+
30+
<ImageView
31+
android:id="@+id/ad_app_icon"
32+
android:layout_width="40dp"
33+
android:layout_height="40dp"
34+
android:layout_marginEnd="16dp" />
35+
36+
<LinearLayout
37+
android:layout_width="0dp"
38+
android:layout_height="wrap_content"
39+
android:layout_weight="1"
40+
android:orientation="vertical">
41+
42+
<TextView
43+
android:id="@+id/ad_headline"
44+
android:layout_width="wrap_content"
45+
android:layout_height="wrap_content"
46+
android:ellipsize="end"
47+
android:maxLines="1"
48+
android:textAppearance="@style/TextAppearance.Material3.TitleMedium" />
49+
50+
<TextView
51+
android:id="@+id/ad_body"
52+
android:layout_width="wrap_content"
53+
android:layout_height="wrap_content"
54+
android:ellipsize="end"
55+
android:maxLines="2"
56+
android:textAppearance="@style/TextAppearance.Material3.BodySmall" />
57+
</LinearLayout>
58+
59+
<com.google.android.material.button.MaterialButton
60+
android:id="@+id/ad_call_to_action"
61+
style="@style/Widget.Material3.Button.TextButton"
62+
android:layout_width="wrap_content"
63+
android:layout_height="wrap_content" />
64+
</LinearLayout>
65+
</LinearLayout>
66+
</com.google.android.material.card.MaterialCardView>
67+
</com.google.android.gms.ads.nativead.NativeAdView>

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
android:layout_height="match_parent">
66

77
<com.google.android.material.card.MaterialCardView
8-
style="@style/Widget.Material3.CardView.Elevated"
8+
style="@style/Widget.Material3.CardView.Outlined"
99
android:layout_width="match_parent"
1010
android:layout_height="0dp"
11-
android:layout_margin="24dp"
12-
app:cardCornerRadius="24dp"
11+
android:layout_marginStart="24dp"
12+
android:layout_marginEnd="24dp"
13+
android:layout_marginBottom="2dp"
14+
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.CardViewTopRounded"
1315
app:layout_constraintBottom_toTopOf="@id/ad_view"
1416
app:layout_constraintTop_toTopOf="parent">
1517

@@ -266,6 +268,12 @@
266268
android:id="@+id/ad_view"
267269
android:layout_width="match_parent"
268270
android:layout_height="wrap_content"
269-
android:layout_margin="24dp"
270-
app:layout_constraintBottom_toBottomOf="parent" />
271+
android:layout_marginStart="24dp"
272+
android:layout_marginTop="0dp"
273+
android:layout_marginEnd="24dp"
274+
android:layout_marginBottom="24dp"
275+
app:nativeAdLayout="@layout/ad_lesson_code"
276+
app:layout_constraintBottom_toBottomOf="parent"
277+
app:layout_constraintEnd_toEndOf="parent"
278+
app:layout_constraintStart_toStartOf="parent" />
271279
</androidx.constraintlayout.widget.ConstraintLayout>

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
android:layout_height="match_parent">
66

77
<com.google.android.material.card.MaterialCardView
8-
style="@style/Widget.Material3.CardView.Elevated"
8+
style="@style/Widget.Material3.CardView.Outlined"
99
android:layout_width="match_parent"
1010
android:layout_height="0dp"
11-
android:layout_margin="24dp"
12-
app:cardCornerRadius="24dp"
11+
android:layout_marginStart="24dp"
12+
13+
android:layout_marginEnd="24dp"
14+
android:layout_marginBottom="2dp"
15+
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.CardViewTopRounded"
1316
app:layout_constraintBottom_toTopOf="@id/ad_view"
1417
app:layout_constraintTop_toTopOf="parent">
1518

@@ -73,6 +76,12 @@
7376
android:id="@+id/ad_view"
7477
android:layout_width="match_parent"
7578
android:layout_height="wrap_content"
76-
android:layout_margin="24dp"
77-
app:layout_constraintBottom_toBottomOf="parent" />
79+
android:layout_marginStart="24dp"
80+
android:layout_marginTop="0dp"
81+
android:layout_marginEnd="24dp"
82+
android:layout_marginBottom="24dp"
83+
app:nativeAdLayout="@layout/ad_lesson_code"
84+
app:layout_constraintBottom_toBottomOf="parent"
85+
app:layout_constraintEnd_toEndOf="parent"
86+
app:layout_constraintStart_toStartOf="parent" />
7887
</androidx.constraintlayout.widget.ConstraintLayout>

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
android:layout_height="match_parent">
66

77
<com.google.android.material.card.MaterialCardView
8-
style="@style/Widget.Material3.CardView.Elevated"
8+
style="@style/Widget.Material3.CardView.Outlined"
99
android:layout_width="match_parent"
1010
android:layout_height="0dp"
11-
android:layout_margin="24dp"
12-
app:cardCornerRadius="24dp"
11+
android:layout_marginStart="24dp"
12+
android:layout_marginEnd="24dp"
13+
android:layout_marginBottom="2dp"
14+
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.CardViewTopRounded"
1315
app:layout_constraintBottom_toTopOf="@id/ad_view"
1416
app:layout_constraintTop_toTopOf="parent">
1517

@@ -30,7 +32,11 @@
3032
android:id="@+id/ad_view"
3133
android:layout_width="match_parent"
3234
android:layout_height="wrap_content"
33-
android:layout_margin="24dp"
35+
android:layout_marginStart="24dp"
36+
android:layout_marginTop="0dp"
37+
android:layout_marginEnd="24dp"
38+
android:layout_marginBottom="24dp"
39+
app:nativeAdLayout="@layout/ad_lesson_code"
3440
app:layout_constraintBottom_toBottomOf="parent"
3541
app:layout_constraintEnd_toEndOf="parent"
3642
app:layout_constraintStart_toStartOf="parent" />

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
android:layout_height="match_parent">
66

77
<com.google.android.material.card.MaterialCardView
8-
style="@style/Widget.Material3.CardView.Elevated"
8+
style="@style/Widget.Material3.CardView.Outlined"
99
android:layout_width="match_parent"
1010
android:layout_height="0dp"
11-
android:layout_margin="24dp"
12-
app:cardCornerRadius="24dp"
11+
android:layout_marginStart="24dp"
12+
android:layout_marginEnd="24dp"
13+
android:layout_marginBottom="2dp"
14+
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.CardViewTopRounded"
1315
app:layout_constraintBottom_toTopOf="@id/ad_view"
1416
app:layout_constraintTop_toTopOf="parent">
1517

@@ -30,7 +32,11 @@
3032
android:id="@+id/ad_view"
3133
android:layout_width="match_parent"
3234
android:layout_height="wrap_content"
33-
android:layout_margin="24dp"
35+
android:layout_marginStart="24dp"
36+
android:layout_marginTop="0dp"
37+
android:layout_marginEnd="24dp"
38+
android:layout_marginBottom="24dp"
39+
app:nativeAdLayout="@layout/ad_lesson_code"
3440
app:layout_constraintBottom_toBottomOf="parent"
3541
app:layout_constraintEnd_toEndOf="parent"
3642
app:layout_constraintStart_toStartOf="parent" />

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
android:layout_height="match_parent">
66

77
<com.google.android.material.card.MaterialCardView
8-
style="@style/Widget.Material3.CardView.Elevated"
8+
style="@style/Widget.Material3.CardView.Outlined"
99
android:layout_width="match_parent"
1010
android:layout_height="0dp"
11-
android:layout_margin="24dp"
12-
app:cardCornerRadius="24dp"
11+
android:layout_marginStart="24dp"
12+
android:layout_marginEnd="24dp"
13+
android:layout_marginBottom="2dp"
14+
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.CardViewTopRounded"
1315
app:layout_constraintBottom_toTopOf="@id/ad_view"
1416
app:layout_constraintTop_toTopOf="parent">
1517

@@ -56,6 +58,12 @@
5658
android:id="@+id/ad_view"
5759
android:layout_width="match_parent"
5860
android:layout_height="wrap_content"
59-
android:layout_margin="24dp"
60-
app:layout_constraintBottom_toBottomOf="parent" />
61+
android:layout_marginStart="24dp"
62+
android:layout_marginTop="0dp"
63+
android:layout_marginEnd="24dp"
64+
android:layout_marginBottom="24dp"
65+
app:nativeAdLayout="@layout/ad_lesson_code"
66+
app:layout_constraintBottom_toBottomOf="parent"
67+
app:layout_constraintEnd_toEndOf="parent"
68+
app:layout_constraintStart_toStartOf="parent" />
6169
</androidx.constraintlayout.widget.ConstraintLayout>

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
android:layout_height="match_parent">
66

77
<com.google.android.material.card.MaterialCardView
8-
style="@style/Widget.Material3.CardView.Elevated"
8+
style="@style/Widget.Material3.CardView.Outlined"
99
android:layout_width="match_parent"
1010
android:layout_height="0dp"
11-
android:layout_margin="24dp"
12-
app:cardCornerRadius="24dp"
11+
android:layout_marginStart="24dp"
12+
android:layout_marginEnd="24dp"
13+
android:layout_marginBottom="2dp"
14+
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.CardViewTopRounded"
1315
app:layout_constraintBottom_toTopOf="@id/ad_view"
1416
app:layout_constraintTop_toTopOf="parent">
1517

@@ -27,6 +29,12 @@
2729
android:id="@+id/ad_view"
2830
android:layout_width="match_parent"
2931
android:layout_height="wrap_content"
30-
android:layout_margin="24dp"
31-
app:layout_constraintBottom_toBottomOf="parent" />
32+
android:layout_marginStart="24dp"
33+
android:layout_marginTop="0dp"
34+
android:layout_marginEnd="24dp"
35+
android:layout_marginBottom="24dp"
36+
app:nativeAdLayout="@layout/ad_lesson_code"
37+
app:layout_constraintBottom_toBottomOf="parent"
38+
app:layout_constraintEnd_toEndOf="parent"
39+
app:layout_constraintStart_toStartOf="parent" />
3240
</androidx.constraintlayout.widget.ConstraintLayout>

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
android:layout_height="match_parent">
66

77
<com.google.android.material.card.MaterialCardView
8-
style="@style/Widget.Material3.CardView.Elevated"
8+
style="@style/Widget.Material3.CardView.Outlined"
99
android:layout_width="match_parent"
1010
android:layout_height="0dp"
11-
android:layout_margin="24dp"
12-
app:cardCornerRadius="24dp"
11+
android:layout_marginStart="24dp"
12+
android:layout_marginEnd="24dp"
13+
android:layout_marginBottom="2dp"
14+
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.CardViewTopRounded"
1315
app:layout_constraintBottom_toTopOf="@id/ad_view"
1416
app:layout_constraintTop_toTopOf="parent">
1517

@@ -52,6 +54,12 @@
5254
android:id="@+id/ad_view"
5355
android:layout_width="match_parent"
5456
android:layout_height="wrap_content"
55-
android:layout_margin="24dp"
56-
app:layout_constraintBottom_toBottomOf="parent" />
57+
android:layout_marginStart="24dp"
58+
android:layout_marginTop="0dp"
59+
android:layout_marginEnd="24dp"
60+
android:layout_marginBottom="24dp"
61+
app:nativeAdLayout="@layout/ad_lesson_code"
62+
app:layout_constraintBottom_toBottomOf="parent"
63+
app:layout_constraintEnd_toEndOf="parent"
64+
app:layout_constraintStart_toStartOf="parent" />
5765
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)