o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
Create a screen in Jetpack composable in which there is top bar which has two tabs…
- User
Create a screen in Jetpack composable in which there is top bar which has two tabs Photos and vidoes. The name of this composable should be saved screen.
- Assistant
Here is an example of how you can create a
SavedScreencomposable in Jetpack Compose with a top bar containing two tabs: "Photos" and "Videos". When a tab is selected, the corresponding content is displayed.import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.material.* import androidx.compose.runtime.* import androidx.compose.ui.Modifier @Composable fun SavedScreen() { var selectedTabIndex by remember { mutableStateOf(0) } val tabs = listOf("Photos", "Videos") Scaffold( topBar = { TabRow( selectedTabIndex = selectedTabIndex, modifier = Modifier.fillMaxWidth(), ) { tabs.forEachIndexed { index, text -> Tab( selected = selectedTabIndex == index, onClick = { selectedTabIndex = index }, text = { Text(text) } ) } } } ) { innerPadding -> when (selectedTabIndex) { 0 -> PhotosContent(Modifier.padding(innerPadding)) 1 -> VideosContent(Modifier.padding(innerPadding)) } } } @Composable fun PhotosContent(modifier: Modifier = Modifier) { // Replace with your Photos screen content Text( text = "Photos Screen", modifier = modifier ) } @Composable fun VideosContent(modifier: Modifier = Modifier) { // Replace with your Videos screen content Text( text = "Videos Screen", modifier = modifier ) }Explanation:
-
TabRow and Tabs:
- We use a
TabRowin thetopBarslot of theScaffoldto create the tabs. selectedTabIndexkeeps track of the currently selected tab.tabs.forEachIndexedis used to generate each tab with the appropriate text and selection state.
- We use a
-
Content Switching:
- Inside the
Scaffoldcontent, we use awhenexpression onselectedTabIndexto switch betweenPhotosContentandVideosContentcomposables. - Each content composable accepts a
Modifierto handle padding from theScaffold.
- Inside the
-
PhotosContent and VideosContent:
- These composables are placeholders where you can implement the actual UI for your Photos and Videos screens.
Usage:
To display the
SavedScreen, you can call it from your main composable or navigation graph:@Composable fun MainScreen() { // Other UI components or navigation setup SavedScreen() }Note:
- Make sure to import the necessary Compose libraries in your
build.gradlefile. - Customize the
PhotosContentandVideosContentcomposables to display the actual content you want in each tab.
Dependencies:
Ensure you have the following dependencies in your
build.gradlefile:dependencies { implementation 'androidx.compose.ui:ui:<latest_version>' implementation 'androidx.compose.material:material:<latest_version>' implementation 'androidx.compose.ui:ui-tooling-preview:<latest_version>' // Other dependencies... }Replace
<latest_version>with the latest versions of the Compose libraries. -