Skip to content

Commit d63b73d

Browse files
Nicholas Ventimigliacopybara-github
authored andcommitted
Added jetpack compose navigation.
Renamed MobileAdsApplication to GoogleMobileAdsApplication. PiperOrigin-RevId: 668056997
1 parent fcff776 commit d63b73d

7 files changed

Lines changed: 128 additions & 57 deletions

File tree

kotlin/advanced/JetpackComposeDemo/app/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ dependencies {
4444
implementation("androidx.compose.ui:ui-graphics")
4545
implementation("androidx.compose.material3:material3")
4646
implementation("androidx.compose.foundation:foundation")
47+
implementation("androidx.navigation:navigation-compose:2.7.7")
4748
implementation("com.google.android.gms:play-services-ads:23.3.0")
4849
implementation("com.google.android.ump:user-messaging-platform:3.0.0")
4950
implementation(project(":compose-util"))
51+
implementation("androidx.navigation:navigation-runtime-ktx:2.7.7")
5052
debugImplementation("androidx.compose.ui:ui-tooling")
5153
}

kotlin/advanced/JetpackComposeDemo/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xmlns:tools="http://schemas.android.com/tools">
44

55
<application
6-
android:name="com.google.android.gms.example.jetpackcomposedemo.MobileAdsApplication"
6+
android:name="com.google.android.gms.example.jetpackcomposedemo.GoogleMobileAdsApplication"
77
android:allowBackup="true"
88
android:icon="@mipmap/ic_launcher"
99
android:label="@string/app_name"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.android.gms.example.jetpackcomposedemo
18+
19+
import android.app.Application
20+
21+
class GoogleMobileAdsApplication : Application() {
22+
companion object {
23+
const val TAG = "GoogleMobileAdsSample"
24+
}
25+
}

kotlin/advanced/JetpackComposeDemo/app/src/main/java/com/google/android/gms/example/jetpackcomposedemo/MainActivity.kt

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,6 @@ import android.os.Bundle
2020
import androidx.activity.ComponentActivity
2121
import androidx.activity.compose.setContent
2222
import androidx.activity.enableEdgeToEdge
23-
import androidx.compose.foundation.layout.Column
24-
import androidx.compose.foundation.layout.Spacer
25-
import androidx.compose.foundation.layout.WindowInsets
26-
import androidx.compose.foundation.layout.WindowInsetsSides
27-
import androidx.compose.foundation.layout.fillMaxHeight
28-
import androidx.compose.foundation.layout.only
29-
import androidx.compose.foundation.layout.padding
30-
import androidx.compose.foundation.layout.systemBars
31-
import androidx.compose.foundation.layout.windowInsetsBottomHeight
32-
import androidx.compose.foundation.rememberScrollState
33-
import androidx.compose.foundation.verticalScroll
34-
import androidx.compose.material3.ExperimentalMaterial3Api
35-
import androidx.compose.material3.MaterialTheme
36-
import androidx.compose.material3.Scaffold
37-
import androidx.compose.material3.Surface
38-
import androidx.compose.material3.Text
39-
import androidx.compose.material3.TopAppBar
40-
import androidx.compose.runtime.Composable
41-
import androidx.compose.ui.Modifier
42-
import androidx.compose.ui.tooling.preview.Preview
43-
import com.example.jetpackcomposedemo.R
44-
import com.google.android.gms.example.jetpackcomposedemo.ui.theme.JetpackComposeDemoTheme
4523

4624
class MainActivity : ComponentActivity() {
4725

@@ -50,36 +28,6 @@ class MainActivity : ComponentActivity() {
5028
enableEdgeToEdge()
5129
super.onCreate(savedInstanceState)
5230

53-
setContent {
54-
JetpackComposeDemoTheme {
55-
Surface(modifier = Modifier.fillMaxHeight(), color = MaterialTheme.colorScheme.background) {
56-
MainScreen()
57-
}
58-
}
59-
}
31+
setContent { MainScreen() }
6032
}
61-
62-
@Composable
63-
@Preview
64-
fun MainScreenPreview() {
65-
JetpackComposeDemoTheme {
66-
Surface(modifier = Modifier.fillMaxHeight(), color = MaterialTheme.colorScheme.background) {
67-
MainScreen()
68-
}
69-
}
70-
}
71-
72-
@OptIn(ExperimentalMaterial3Api::class)
73-
@Composable
74-
fun MainScreen() =
75-
Scaffold(
76-
topBar = { TopAppBar(title = { Text(resources.getString(R.string.main_title)) }) },
77-
contentWindowInsets =
78-
WindowInsets.systemBars.only(WindowInsetsSides.Top + WindowInsetsSides.Horizontal),
79-
) { innerPadding ->
80-
Column(Modifier.padding(innerPadding).verticalScroll(rememberScrollState())) {
81-
Text(resources.getString(R.string.main_title))
82-
Spacer(Modifier.windowInsetsBottomHeight(WindowInsets.systemBars))
83-
}
84-
}
8533
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.google.android.gms.example.jetpackcomposedemo
2+
3+
import androidx.compose.foundation.layout.Column
4+
import androidx.compose.foundation.layout.Spacer
5+
import androidx.compose.foundation.layout.WindowInsets
6+
import androidx.compose.foundation.layout.WindowInsetsSides
7+
import androidx.compose.foundation.layout.fillMaxSize
8+
import androidx.compose.foundation.layout.only
9+
import androidx.compose.foundation.layout.padding
10+
import androidx.compose.foundation.layout.systemBars
11+
import androidx.compose.foundation.layout.windowInsetsBottomHeight
12+
import androidx.compose.material.icons.Icons
13+
import androidx.compose.material.icons.automirrored.filled.ArrowBack
14+
import androidx.compose.material.icons.filled.MoreVert
15+
import androidx.compose.material3.DropdownMenu
16+
import androidx.compose.material3.ExperimentalMaterial3Api
17+
import androidx.compose.material3.Icon
18+
import androidx.compose.material3.IconButton
19+
import androidx.compose.material3.MaterialTheme
20+
import androidx.compose.material3.Scaffold
21+
import androidx.compose.material3.Surface
22+
import androidx.compose.material3.Text
23+
import androidx.compose.material3.TopAppBar
24+
import androidx.compose.runtime.Composable
25+
import androidx.compose.runtime.getValue
26+
import androidx.compose.runtime.mutableStateOf
27+
import androidx.compose.runtime.remember
28+
import androidx.compose.runtime.setValue
29+
import androidx.compose.ui.Modifier
30+
import androidx.compose.ui.platform.LocalContext
31+
import androidx.compose.ui.tooling.preview.Preview
32+
import androidx.navigation.NavHostController
33+
import androidx.navigation.compose.NavHost
34+
import androidx.navigation.compose.composable
35+
import androidx.navigation.compose.rememberNavController
36+
import com.example.jetpackcomposedemo.R
37+
import com.google.android.gms.example.jetpackcomposedemo.ui.theme.JetpackComposeDemoTheme
38+
39+
@Composable
40+
fun MainScreen() {
41+
val navController = rememberNavController()
42+
Scaffold(
43+
topBar = { MainTopBar(navController = navController) },
44+
contentWindowInsets =
45+
WindowInsets.systemBars.only(WindowInsetsSides.Top + WindowInsetsSides.Horizontal),
46+
) { innerPadding ->
47+
Column(Modifier.padding(innerPadding)) {
48+
NavHost(navController = navController, startDestination = NavDestinations.Home.name) {
49+
composable(NavDestinations.Home.name) {}
50+
}
51+
Spacer(Modifier.windowInsetsBottomHeight(WindowInsets.systemBars))
52+
}
53+
}
54+
}
55+
56+
@OptIn(ExperimentalMaterial3Api::class)
57+
@Composable
58+
private fun MainTopBar(navController: NavHostController) {
59+
val context = LocalContext.current
60+
var menuExpanded by remember { mutableStateOf(false) }
61+
TopAppBar(
62+
title = { Text(context.getString(R.string.main_title)) },
63+
navigationIcon = {
64+
if (navController.currentBackStackEntry != null) {
65+
IconButton(onClick = { navController.popBackStack() }) {
66+
Icon(
67+
imageVector = Icons.AutoMirrored.Filled.ArrowBack,
68+
contentDescription = Icons.AutoMirrored.Filled.ArrowBack.name,
69+
)
70+
}
71+
}
72+
},
73+
actions = {
74+
IconButton(onClick = { menuExpanded = true }) {
75+
Icon(imageVector = Icons.Filled.MoreVert, contentDescription = Icons.Filled.MoreVert.name)
76+
}
77+
DropdownMenu(expanded = menuExpanded, onDismissRequest = { menuExpanded = false }) {}
78+
},
79+
)
80+
}
81+
82+
@Preview
83+
@Composable
84+
private fun MainScreenPreview() {
85+
JetpackComposeDemoTheme {
86+
Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
87+
MainScreen()
88+
}
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.google.android.gms.example.jetpackcomposedemo
2+
3+
enum class NavDestinations {
4+
Home
5+
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<resources>
2-
<string name="app_name">Jetpack Compose Demo</string>
3-
<string name="main_title">Google Mobile Ads Sample</string>
4-
<string name="main_status_uninitialized">Google Mobile Ads SDK is not initialized.</string>
2+
<string name="app_name">Google Mobile Ads Jetpack Compose Demo</string>
3+
<string name="version_format">Google Mobile Ads SDK Version: %s </string>
4+
<string name="main_title">Google Mobile Ads</string>
5+
<string name="nav_home">Home</string>
56
</resources>

0 commit comments

Comments
 (0)