Skip to content

Commit dee82d4

Browse files
malandr2copybara-github
authored andcommitted
Update native ad sample app bar layout to support Android >=15
PiperOrigin-RevId: 822120751
1 parent 516f36e commit dee82d4

File tree

14 files changed

+240
-129
lines changed

14 files changed

+240
-129
lines changed

java/admanager/NativeAdsExample/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ android {
2222
dependencies {
2323
implementation 'androidx.appcompat:appcompat:1.2.0'
2424
implementation 'com.google.android.gms:play-services-ads:24.7.0'
25+
implementation 'com.google.android.material:material:1.13.0'
2526
implementation 'com.google.android.ump:user-messaging-platform:3.2.0'
2627
}

java/admanager/NativeAdsExample/app/src/main/java/com/google/example/gms/nativeadsexample/MainActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public class MainActivity extends AppCompatActivity {
8080
protected void onCreate(Bundle savedInstanceState) {
8181
super.onCreate(savedInstanceState);
8282
setContentView(R.layout.activity_main);
83+
setSupportActionBar(findViewById(R.id.toolBar));
8384

8485
refresh = findViewById(R.id.btn_refresh);
8586
requestNativeAds = findViewById(R.id.cb_nativeads);
Lines changed: 74 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,89 @@
1-
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
1+
<androidx.constraintlayout.widget.ConstraintLayout
2+
xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
24
xmlns:tools="http://schemas.android.com/tools"
35
android:layout_width="match_parent"
46
android:layout_height="match_parent"
5-
tools:context="com.google.example.gms.nativeadsexample.MainActivity">
7+
tools:context=".MainActivity">
68

7-
<LinearLayout
9+
<com.google.android.material.appbar.AppBarLayout
10+
android:id="@+id/app_bar_layout"
811
android:layout_width="match_parent"
912
android:layout_height="wrap_content"
10-
android:orientation="vertical">
13+
android:fitsSystemWindows="true"
14+
android:background="@color/colorPrimaryDark"
15+
app:layout_constraintTop_toTopOf="parent">
1116

12-
<FrameLayout
13-
android:id="@+id/fl_adplaceholder"
17+
<com.google.android.material.appbar.MaterialToolbar
18+
android:id="@+id/toolBar"
1419
android:layout_width="match_parent"
15-
android:layout_height="wrap_content"
16-
android:layout_marginTop="16dp" />
20+
android:layout_height="?attr/actionBarSize"
21+
app:titleTextColor="@android:color/white"
22+
app:title="@string/app_name" />
1723

18-
<CheckBox
19-
android:id="@+id/cb_nativeads"
20-
android:layout_width="wrap_content"
21-
android:layout_height="wrap_content"
22-
android:layout_marginTop="16dp"
23-
android:checked="true"
24-
android:text="@string/request_unified" />
24+
</com.google.android.material.appbar.AppBarLayout>
2525

26-
<CheckBox
27-
android:id="@+id/cb_customtemplate"
28-
android:layout_width="wrap_content"
29-
android:layout_height="wrap_content"
30-
android:checked="true"
31-
android:text="@string/request_template" />
3226

33-
<CheckBox
34-
android:id="@+id/cb_start_muted"
35-
android:layout_width="wrap_content"
36-
android:layout_height="wrap_content"
37-
android:checked="true"
38-
android:text="@string/start_muted" />
27+
<ScrollView
28+
android:id="@+id/root_view"
29+
android:layout_width="match_parent"
30+
android:layout_height="0dp"
31+
app:layout_constraintTop_toBottomOf="@id/app_bar_layout"
32+
app:layout_constraintBottom_toBottomOf="parent">
3933

40-
<Button
41-
android:id="@+id/btn_refresh"
42-
android:layout_width="wrap_content"
43-
android:layout_height="wrap_content"
44-
android:layout_gravity="center_horizontal"
45-
android:layout_marginBottom="16dp"
46-
android:layout_marginTop="16dp"
47-
android:text="@string/refresh_ad"
48-
android:visibility="gone" />
49-
50-
<TextView
51-
android:id="@+id/tv_video_status"
34+
<LinearLayout
5235
android:layout_width="match_parent"
5336
android:layout_height="wrap_content"
54-
android:layout_marginLeft="@dimen/activity_horizontal_margin"
55-
android:layout_marginRight="@dimen/activity_horizontal_margin"
56-
android:text="@string/video_status"
57-
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
37+
android:orientation="vertical">
38+
39+
<FrameLayout
40+
android:id="@+id/fl_adplaceholder"
41+
android:layout_width="match_parent"
42+
android:layout_height="wrap_content"
43+
android:layout_marginTop="16dp" />
44+
45+
<CheckBox
46+
android:id="@+id/cb_nativeads"
47+
android:layout_width="wrap_content"
48+
android:layout_height="wrap_content"
49+
android:layout_marginTop="16dp"
50+
android:checked="true"
51+
android:text="@string/request_unified" />
52+
53+
<CheckBox
54+
android:id="@+id/cb_customtemplate"
55+
android:layout_width="wrap_content"
56+
android:layout_height="wrap_content"
57+
android:checked="true"
58+
android:text="@string/request_template" />
59+
60+
<CheckBox
61+
android:id="@+id/cb_start_muted"
62+
android:layout_width="wrap_content"
63+
android:layout_height="wrap_content"
64+
android:checked="true"
65+
android:text="@string/start_muted" />
66+
67+
<Button
68+
android:id="@+id/btn_refresh"
69+
android:layout_width="wrap_content"
70+
android:layout_height="wrap_content"
71+
android:layout_gravity="center_horizontal"
72+
android:layout_marginBottom="16dp"
73+
android:layout_marginTop="16dp"
74+
android:text="@string/refresh_ad"
75+
android:visibility="gone" />
76+
77+
<TextView
78+
android:id="@+id/tv_video_status"
79+
android:layout_width="match_parent"
80+
android:layout_height="wrap_content"
81+
android:layout_marginLeft="@dimen/activity_horizontal_margin"
82+
android:layout_marginRight="@dimen/activity_horizontal_margin"
83+
android:text="@string/video_status"
84+
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
85+
86+
</LinearLayout>
87+
</ScrollView>
5888

59-
</LinearLayout>
60-
</ScrollView>
89+
</androidx.constraintlayout.widget.ConstraintLayout>
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+
<resources>
3+
<color name="colorPrimary">#3F51B5</color>
4+
<color name="colorPrimaryDark">#303F9F</color>
5+
<color name="colorAccent">#FF4081</color>
6+
</resources>

java/admanager/NativeAdsExample/app/src/main/res/values/styles.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<resources>
22

33
<!-- Base application theme. -->
4-
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
4+
<style name="AppTheme" parent="Theme.Material3.DayNight.NoActionBar">
55
<!-- Customize your theme here. -->
6+
<item name="colorPrimary">@color/colorPrimary</item>
7+
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
8+
<item name="colorAccent">@color/colorAccent</item>
69
</style>
710

811
<style name="AppTheme.AdAttribution">

java/admob/NativeAdvancedExample/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ android {
2222
dependencies {
2323
implementation 'androidx.appcompat:appcompat:1.2.0'
2424
implementation 'com.google.android.gms:play-services-ads:24.7.0'
25+
implementation 'com.google.android.material:material:1.13.0'
2526
implementation 'com.google.android.ump:user-messaging-platform:3.2.0'
2627
}

java/admob/NativeAdvancedExample/app/src/main/java/com/google/example/gms/nativeadvancedexample/MainActivity.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,11 @@ public class MainActivity extends AppCompatActivity {
7373
protected void onCreate(Bundle savedInstanceState) {
7474
super.onCreate(savedInstanceState);
7575
setContentView(R.layout.activity_main);
76+
setSupportActionBar(findViewById(R.id.toolBar));
7677

77-
refresh = findViewById(R.id.btn_refresh);
78-
startVideoAdsMuted = findViewById(R.id.cb_start_muted);
79-
videoStatus = findViewById(R.id.tv_video_status_text);
78+
refresh = findViewById(R.id.refresh_button);
79+
startVideoAdsMuted = findViewById(R.id.start_muted_checkbox);
80+
videoStatus = findViewById(R.id.video_status_text);
8081

8182
// Log the Mobile Ads SDK version.
8283
Log.d(TAG, "Google Mobile Ads SDK Version: " + MobileAds.getVersion());
@@ -304,7 +305,7 @@ public void onNativeAdLoaded(NativeAd nativeAd) {
304305
MainActivity.this.nativeAd.destroy();
305306
}
306307
MainActivity.this.nativeAd = nativeAd;
307-
FrameLayout frameLayout = findViewById(R.id.fl_adplaceholder);
308+
FrameLayout frameLayout = findViewById(R.id.ad_frame);
308309
NativeAdView adView =
309310
(NativeAdView)
310311
getLayoutInflater().inflate(R.layout.ad_unified, frameLayout, false);
Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,73 @@
1-
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
1+
<androidx.constraintlayout.widget.ConstraintLayout
2+
xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
24
xmlns:tools="http://schemas.android.com/tools"
35
android:layout_width="match_parent"
46
android:layout_height="match_parent"
5-
tools:context="com.google.example.gms.nativeadvancedexample.MainActivity">
7+
tools:context=".MainActivity">
68

7-
<LinearLayout
9+
<com.google.android.material.appbar.AppBarLayout
10+
android:id="@+id/app_bar_layout"
811
android:layout_width="match_parent"
912
android:layout_height="wrap_content"
10-
android:orientation="vertical">
13+
android:fitsSystemWindows="true"
14+
android:background="@color/colorPrimaryDark"
15+
app:layout_constraintTop_toTopOf="parent">
1116

12-
<FrameLayout
13-
android:id="@+id/fl_adplaceholder"
17+
<com.google.android.material.appbar.MaterialToolbar
18+
android:id="@+id/toolBar"
1419
android:layout_width="match_parent"
15-
android:layout_height="wrap_content"
16-
android:layout_marginTop="16dp" />
20+
android:layout_height="?attr/actionBarSize"
21+
app:titleTextColor="@android:color/white"
22+
app:title="@string/app_name" />
1723

18-
<CheckBox
19-
android:id="@+id/cb_start_muted"
20-
android:layout_width="wrap_content"
21-
android:layout_height="wrap_content"
22-
android:checked="true"
23-
android:text="@string/start_muted" />
24+
</com.google.android.material.appbar.AppBarLayout>
2425

25-
<Button
26-
android:id="@+id/btn_refresh"
27-
android:layout_width="wrap_content"
28-
android:layout_height="wrap_content"
29-
android:layout_gravity="center_horizontal"
30-
android:layout_marginBottom="16dp"
31-
android:layout_marginTop="16dp"
32-
android:text="@string/refresh_ad"
33-
android:visibility="gone" />
34-
35-
<TextView
36-
android:text="@string/video_status"
26+
<ScrollView
27+
android:id="@+id/root_view"
28+
android:layout_width="match_parent"
29+
android:layout_height="0dp"
30+
app:layout_constraintTop_toBottomOf="@id/app_bar_layout"
31+
app:layout_constraintBottom_toBottomOf="parent">
32+
33+
<LinearLayout
3734
android:layout_width="match_parent"
3835
android:layout_height="wrap_content"
39-
android:layout_marginLeft="@dimen/activity_horizontal_margin"
40-
android:layout_marginRight="@dimen/activity_horizontal_margin"
41-
android:id="@+id/tv_video_status_text"
42-
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
36+
android:orientation="vertical">
37+
38+
<FrameLayout
39+
android:id="@+id/ad_frame"
40+
android:layout_width="match_parent"
41+
android:layout_height="wrap_content"
42+
android:layout_marginTop="16dp" />
43+
44+
<CheckBox
45+
android:id="@+id/start_muted_checkbox"
46+
android:layout_width="wrap_content"
47+
android:layout_height="wrap_content"
48+
android:checked="true"
49+
android:text="Start video ads muted" />
50+
51+
<Button
52+
android:id="@+id/refresh_button"
53+
android:layout_width="wrap_content"
54+
android:layout_height="wrap_content"
55+
android:layout_gravity="center_horizontal"
56+
android:layout_marginBottom="16dp"
57+
android:layout_marginTop="16dp"
58+
android:text="Refresh Ad"
59+
android:visibility="gone" />
60+
61+
<TextView
62+
android:id="@+id/video_status_text"
63+
android:layout_width="match_parent"
64+
android:layout_height="wrap_content"
65+
android:layout_marginLeft="16dp"
66+
android:layout_marginRight="16dp"
67+
android:text="Video status: "
68+
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
4369

70+
</LinearLayout>
71+
</ScrollView>
4472

45-
</LinearLayout>
46-
</ScrollView>
73+
</androidx.constraintlayout.widget.ConstraintLayout>
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+
<resources>
3+
<color name="colorPrimary">#3F51B5</color>
4+
<color name="colorPrimaryDark">#303F9F</color>
5+
<color name="colorAccent">#FF4081</color>
6+
</resources>

java/admob/NativeAdvancedExample/app/src/main/res/values/styles.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<resources>
22

33
<!-- Base application theme. -->
4-
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
4+
<style name="AppTheme" parent="Theme.Material3.DayNight.NoActionBar">
55
<!-- Customize your theme here. -->
6+
<item name="colorPrimary">@color/colorPrimary</item>
7+
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
8+
<item name="colorAccent">@color/colorAccent</item>
69
</style>
710

811
<style name="AppTheme.AdAttribution">

0 commit comments

Comments
 (0)