Created
October 19, 2023 19:49
-
-
Save slaviboy/329b5b18d43d10e56fce3dc7e37f0e59 to your computer and use it in GitHub Desktop.
Scroll to TextField, when it gets focused
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.slaviboy.test | |
import android.os.Bundle | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.compose.foundation.ScrollState | |
import androidx.compose.foundation.layout.Box | |
import androidx.compose.foundation.layout.Column | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.foundation.layout.fillMaxWidth | |
import androidx.compose.foundation.layout.height | |
import androidx.compose.foundation.layout.imePadding | |
import androidx.compose.foundation.layout.padding | |
import androidx.compose.foundation.rememberScrollState | |
import androidx.compose.foundation.verticalScroll | |
import androidx.compose.material3.ExperimentalMaterial3Api | |
import androidx.compose.material3.Text | |
import androidx.compose.material3.TextField | |
import androidx.compose.runtime.getValue | |
import androidx.compose.runtime.mutableStateOf | |
import androidx.compose.runtime.remember | |
import androidx.compose.runtime.rememberCoroutineScope | |
import androidx.compose.runtime.setValue | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.composed | |
import androidx.compose.ui.focus.onFocusEvent | |
import androidx.compose.ui.layout.onGloballyPositioned | |
import androidx.compose.ui.layout.positionInRoot | |
import androidx.compose.ui.unit.dp | |
import kotlinx.coroutines.launch | |
class MainActivity : ComponentActivity() { | |
@OptIn(ExperimentalMaterial3Api::class) | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
val scrollState = rememberScrollState() | |
Column( | |
modifier = Modifier | |
.fillMaxSize() | |
.verticalScroll(scrollState) | |
.imePadding() | |
) { | |
for (i in 0 until 100) { | |
var textFieldValue by remember { | |
mutableStateOf("") | |
} | |
TextField( | |
value = textFieldValue, | |
onValueChange = { | |
textFieldValue = it | |
}, | |
label = { | |
Text(text = "$i") | |
}, | |
modifier = Modifier | |
.fillMaxWidth() | |
.padding(10.dp) | |
.bringIntoView(scrollState) | |
) | |
Box( | |
modifier = Modifier | |
.fillMaxWidth() | |
.height(50.dp) | |
) | |
} | |
} | |
} | |
} | |
} | |
fun Modifier.bringIntoView( | |
scrollState: ScrollState | |
): Modifier = composed { | |
var scrollToPosition by remember { | |
mutableStateOf(0f) | |
} | |
val coroutineScope = rememberCoroutineScope() | |
this | |
.onGloballyPositioned { coordinates -> | |
scrollToPosition = scrollState.value + coordinates.positionInRoot().y | |
} | |
.onFocusEvent { | |
if (it.isFocused) { | |
coroutineScope.launch { | |
scrollState.animateScrollTo(scrollToPosition.toInt()) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment