Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

Create a screen in Jetpack composable in which there is top bar which has two tabs…

  1. 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.

  2. Assistant

    Here is an example of how you can create a SavedScreen composable 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 TabRow in the topBar slot of the Scaffold to create the tabs.
      • selectedTabIndex keeps track of the currently selected tab.
      • tabs.forEachIndexed is used to generate each tab with the appropriate text and selection state.
    • Content Switching:

      • Inside the Scaffold content, we use a when expression on selectedTabIndex to switch between PhotosContent and VideosContent composables.
      • Each content composable accepts a Modifier to handle padding from the Scaffold.
    • 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.gradle file.
    • Customize the PhotosContent and VideosContent composables to display the actual content you want in each tab.

    Dependencies:

    Ensure you have the following dependencies in your build.gradle file:

    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.