0

A few days ago I bumped on a problem where a part of my view is overlaped by keyboard.

Let's say we have 3 different dialogs (could be any content), which looks like this:

enter image description here

When I want to write in anything, last dialog is covered by keyboard:

enter image description here

And there's no way to see what user wrote. Here's my code:

@Composable
fun BuildWordsView(navController: NavController, sharedViewModel: SharedViewModel) {

        Column(
            modifier = Modifier
                .fillMaxWidth()
                .background(PrimaryLight)
                .fillMaxSize()

        ) {
            BuildWordsScreenContents()
        }

}

@Composable
fun BuildWordsScreenContents() {

    Column(
        Modifier
            .fillMaxSize()
            .padding(all = 16.dp)

    ) {

        val inputBoxModifier = Modifier
            .clip(RoundedCornerShape(10.dp))
            .background(Primary)
            .weight(12f)
            .wrapContentHeight()

        InputBlock("Dialog1", inputBoxModifier)
        Spacer(Modifier.weight(1f))
        InputBlock("Dialog2", inputBoxModifier)
        Spacer(Modifier.weight(1f))
        InputBlock("Dialog3", inputBoxModifier)
    }
}



@Composable
fun InputBlock(dialogText: String, inputBlockModifier: Modifier) {
    Column(modifier = inputBlockModifier) {
        Text(
            dialogText,
            fontSize = 30.sp,
            textAlign = TextAlign.Center,
            modifier = Modifier
                .fillMaxWidth()
                .wrapContentSize(Alignment.Center)
        )
        var text by remember { mutableStateOf("") }

        TextField(
            value = text,
            modifier = Modifier
                .fillMaxWidth()
                .wrapContentSize(Alignment.Center),
            onValueChange = { text = it },
            label = { Text("Label") }
        )
    }
}

This question seems to be similar to mine but answers modificate the content of view which I want to avoid:

Software keyboard overlaps content of jetpack compose view

By now I figured out how to solve this problem and I share my approach as an answer

Drogheda
  • 150
  • 3
  • 12
  • Does this answer your question? [Software keyboard overlaps content of jetpack compose view](https://stackoverflow.com/questions/64050392/software-keyboard-overlaps-content-of-jetpack-compose-view) – Pylyp Dukhov Jan 06 '22 at 00:18
  • You shouldn't create an other question just because current answers doesn't fit you. You should left your own answer there instead. – Pylyp Dukhov Jan 06 '22 at 00:19
  • @PhilipDukhov it does not answer my question and I mentioned it. Please read carefully my problem. The main difference is that I do not want to create scrollable layout but leave it as it is (however I use this as a trick). Moreover I do not show any lists in my view. That are completly different problems – Drogheda Jan 06 '22 at 01:15
  • It doesn't make really difference and your answer has nothing which wasn't mentioned in [this answer](https://stackoverflow.com/a/68802725/3585796) or in accompanist documentation. Also if you had to use scrollable, then how is it different that you didn't have it in the first place? Anyone who's looking for the answer will find it by the original title and will have full information to solve it. – Pylyp Dukhov Jan 06 '22 at 02:26

1 Answers1

6

My approach to deal with this problem is using Insets for Jetpack Compose:

https://google.github.io/accompanist/insets/

In order to start dealing with problem you need to add depency to gradle (current version is 0.22.0-rc).

dependencies { 
    implementation "com.google.accompanist:accompanist-insets:0.22.0-rc"
}

Then you need to wrap your content in your activity with ProvideWindowInsets

setContent {
    ProvideWindowInsets {
        YourTheme {
            //YOUR CONTENT HERE
        }
    }
}

Additionaly you need to add following line in your activity onCreate() function:

WindowCompat.setDecorFitsSystemWindows(window, false)

Update: Despite this function is recommended, to my experience it may make this approach not work. If you face any problem, you may need to delete this line.

Now your project is set up to use Insets

In the next steps I'm gonna use code I provided in question

First of all you need to wrap your main Column with

ProvideWindowInsets(windowInsetsAnimationsEnabled = true)

Then let's modificate a modifier a bit by adding:

.statusBarsPadding()
.navigationBarsWithImePadding()
.verticalScroll(rememberScrollState())

As you can see the trick in my approach is to use verticalScroll(). Final code of main column should look like this:

@Composable
fun BuildWordsView(navController: NavController, sharedViewModel: SharedViewModel) {

    ProvideWindowInsets(windowInsetsAnimationsEnabled = true) {

        Column(
            modifier = Modifier
                .fillMaxWidth()
                .background(PrimaryLight)
                .statusBarsPadding()
                .navigationBarsWithImePadding()
                .verticalScroll(rememberScrollState())
                .fillMaxSize()

        ) {
            BuildWordsScreenContents()
        }
    }
}

Now let's modificate the modifier of Column in fun BuildWordsScreenContents()

The main modification is that we provide a height of our screen by:

.height(LocalConfiguration.current.screenHeightDp.dp)

This means that height of our Column would fit our screen perfectly. So when keyboard is not opened the Column will not be scrollable

There is the full code:

@Composable
fun BuildWordsView(navController: NavController, sharedViewModel: SharedViewModel) {

    ProvideWindowInsets(windowInsetsAnimationsEnabled = true) {

        Column(
            modifier = Modifier
                .fillMaxWidth()
                .background(PrimaryLight)
                .statusBarsPadding()
                .navigationBarsWithImePadding()
                .verticalScroll(rememberScrollState())
                .fillMaxSize()

        ) {
            BuildWordsScreenContents()
        }
    }
}

@Composable
fun BuildWordsScreenContents() {

    Column(
        Modifier
            .height(LocalConfiguration.current.screenHeightDp.dp)
            .padding(all = 16.dp)

    ) {

        val inputBoxModifier = Modifier
            .clip(RoundedCornerShape(10.dp))
            .background(Primary)
            .weight(12f)
            .wrapContentHeight()

        InputBlock("Dialog1", inputBoxModifier)
        Spacer(Modifier.weight(1f))
        InputBlock("Dialog2", inputBoxModifier)
        Spacer(Modifier.weight(1f))
        InputBlock("Dialog3", inputBoxModifier)
    }
}





@Composable
fun InputBlock(dialogText: String, inputBlockModifier: Modifier) {
    Column(modifier = inputBlockModifier) {
        Text(
            dialogText,
            fontSize = 30.sp,
            textAlign = TextAlign.Center,
            modifier = Modifier
                .fillMaxWidth()
                .wrapContentSize(Alignment.Center)
        )
        var text by remember { mutableStateOf("") }

        TextField(
            value = text,
            modifier = Modifier
                .fillMaxWidth()
                .wrapContentSize(Alignment.Center),
            onValueChange = { text = it },
            label = { Text("Label") }
        )
    }
}

The final code allows us to scroll down the view:

enter image description here

Important Note For APIs 30-

For APIs lower then 30 you need to modificate the AndroidManifest.xml file

In <activity you need to add android:windowSoftInputMode="adjustResize" in order to make it work. It do not resize your components but it is obligatory to make this approach work

Manifest should look like this:

<activity
    android:name=".MainActivity"
    android:windowSoftInputMode="adjustResize"

Feel free to give me any tips how can I improve my question. AFAIK this problem is as old as android and I wanted to create a quick tutorial how to manage that. Happy coding!

Drogheda
  • 150
  • 3
  • 12