最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - How to keep BottomBar above the keyboard in Jetpack Compose? - Stack Overflow

programmeradmin10浏览0评论

I'm trying to create a UI that consists of a text field and a bottom bar. When the user taps on the text field, I want the screen to resize so that the keyboard does not cover any content. Additionally, the bottom bar should move up and stay above the keyboard instead of being hidden.

Here are some key points:

The UI contains a text input field and a bottom navigation bar.

When the keyboard appears, the screen should adjust dynamically.

The bottom bar should be repositioned above the keyboard instead of overlapping with it.

The UI should work smoothly on different devices and screen sizes.

I've attached two illustrative images showing the expected behavior. How can I achieve this effect in Android?

I'm trying to create a UI that consists of a text field and a bottom bar. When the user taps on the text field, I want the screen to resize so that the keyboard does not cover any content. Additionally, the bottom bar should move up and stay above the keyboard instead of being hidden.

Here are some key points:

The UI contains a text input field and a bottom navigation bar.

When the keyboard appears, the screen should adjust dynamically.

The bottom bar should be repositioned above the keyboard instead of overlapping with it.

The UI should work smoothly on different devices and screen sizes.

I've attached two illustrative images showing the expected behavior. How can I achieve this effect in Android?

Share Improve this question asked Mar 17 at 20:54 gadmtbgadmtb 556 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 3
  • You can achieve that by using modifier modifier = Modifier.imePadding() apply this modifier to your BottomAppBar
Use modifier = Modifier.navigationBarsPadding().imePadding() in Scaffold modifier
fun BottomBar(){

    var message by remember { mutableStateOf(value = "") }
    var isKeyboardOpen by remember { mutableStateOf(false) }
    Scaffold(modifier = Modifier
        .fillMaxSize()
        .navigationBarsPadding()
        .imePadding()
        .animateContentSize(),

        bottomBar = { BottomAppBar(modifier = Modifier
            .fillMaxWidth()
            .height(animateDpAsState(if(isKeyboardOpen) 180.dp else 150.dp).value)

            ,
            containerColor = Color.Black,
            contentColor = Color.Black
            ){
           Column(modifier = Modifier.fillMaxWidth()
             ){
               TextField(
                   message,
                   onValueChange = { newMessage -> message = newMessage
                                   isKeyboardOpen = newMessage.isNotEmpty()},
                   modifier = Modifier.fillMaxWidth().height(50.dp),
                   placeholder = { Text("Message ChatGPT") },

                   colors = TextFieldDefaults.colors(
                       cursorColor = Color.White,
                       focusedPlaceholderColor = Color.White,
                       focusedContainerColor = Color.Black,
                       unfocusedContainerColor = Color.Black,
                       unfocusedPlaceholderColor = Color.White,
                       focusedIndicatorColor = Color.Transparent,
                       unfocusedIndicatorColor = Color.Transparent

                   ))
               Spacer(modifier = Modifier.height(10.dp))
               Row (modifier = Modifier
                   .fillMaxWidth()
                  ,
                   verticalAlignment = Alignment.CenterVertically,
                   horizontalArrangement = Arrangement.SpaceBetween,


                   ){
                   Card(modifier = Modifier
                       .size(40.dp)
                       .clip(shape = RoundedCornerShape(60)),
                       colors = CardDefaults.cardColors(
                           containerColor = Color.Black,
                           contentColor = Color.White
                       )
                       ) {
                       Box(modifier = Modifier.fillMaxSize(),
                           contentAlignment = Alignment.Center){
                           IconButton(onClick = {},
                               modifier = Modifier.padding(5.dp),
                               colors = IconButtonDefaults.iconButtonColors(
                                   contentColor = Color.White
                               )) {
                               Icon(Icons.Default.Add, contentDescription = "Add Icon",)
                           }
                       }
                   }
                   Card(modifier = Modifier
                       .width(95.dp)
                       .height(30.dp)
                       .clip(shape = RoundedCornerShape(80)),

                       colors = CardDefaults.cardColors(
                           containerColor = Color.Black,
                           contentColor = Color.White
                       )

                   ) {
                       Box(modifier = Modifier.fillMaxSize(),
                           contentAlignment = Alignment.Center){
                          Row (modifier = Modifier.fillMaxSize(),
                              verticalAlignment = Alignment.CenterVertically
                              ){
                              IconButton(onClick = {},
                                  colors = IconButtonDefaults.iconButtonColors(
                                      contentColor = Color.White
                                  )) {
                                  Icon(Icons.Default.Favorite, contentDescription = "Add Icon",)

                              }
                              Text("Search")
                          }
                       }
                   }

                   Card(modifier = Modifier
                       .width(95.dp)
                       .height(30.dp)
                       .clip(shape = RoundedCornerShape(60,)),
                       colors = CardDefaults.cardColors(
                           containerColor = Color.Black,
                           contentColor = Color.White
                       ),
                       elevation = CardDefaults.outlinedCardElevation(
                           focusedElevation = 4.dp,
                           defaultElevation = 4.dp,
                           pressedElevation = 4.dp,
                           )


                   ) {
                       Box(modifier = Modifier.fillMaxSize(),
                           contentAlignment = Alignment.Center){
                          Row (modifier = Modifier.fillMaxSize(),
                              verticalAlignment = Alignment.CenterVertically){ IconButton(onClick = {},
                              colors = IconButtonDefaults.iconButtonColors(
                                  contentColor = Color.White
                              )) {
                              Icon(Icons.Default.Send, contentDescription = "Add Icon",)

                          }
                              Text("Reason") }
                       }
                   }
                   Card(modifier = Modifier
                       .size(40.dp)
                       .clip(shape = RoundedCornerShape(60)),
                       colors = CardDefaults.cardColors(
                           containerColor = Color.Black,
                           contentColor = Color.White
                       )
                   ) {
                       Box(modifier = Modifier.fillMaxSize(),
                           contentAlignment = Alignment.Center){
                           IconButton(onClick = {},
                               colors = IconButtonDefaults.iconButtonColors(
                                   contentColor = Color.White
                               )) {
                               Icon(Icons.Default.Notifications, contentDescription = "Add Icon",)
                           }
                       }
                   }
                   Card(modifier = Modifier
                       .size(40.dp)
                       .clip(shape = RoundedCornerShape(60)),
                       colors = CardDefaults.cardColors(
                           containerColor = Color.Black,
                           contentColor = Color.White
                       )
                   ) {
                       Box(modifier = Modifier.fillMaxSize(),
                           contentAlignment = Alignment.Center){
                           IconButton(onClick = {},
                               colors = IconButtonDefaults.iconButtonColors(
                                   contentColor = Color.White
                               )) {
                               Icon(Icons.Default.Add, contentDescription"AddIcon",)
                           }
                       }
                   }

               }


           }

        } }
        )
    {

    }
}
发布评论

评论列表(0)

  1. 暂无评论