Decrease horizontal scroll width for BasicTextField in compose - android

In Jetpack Compose I am using BasicTextField and I want to add my own horizontalScroll for it. When I apply it, it seems to have built in minimum width, so even if content fits, user is still able to scroll.
I don't have this issue if I remove horizontal scroll and use singleLine = true mode. Then BasicTextField becomes scrollable only when entered value doesn't fit.
Is there any way to add some sort of "wrap content" flag for horizontal scroll?
The implementation is pretty standart, I add horizontal scroll to it like this:
var scrollState by remember { mutableStateOf(ScrollState(0)) }
BasicTextField(
modifier = Modifier
.horizontalScroll(state = scrollState, reverseScrolling = true),
singleLine = true,
...

Related

How to set half expand ratio to bottom sheet in Compose?

In XML we have material3 bottom sheet. It allows us to set behavior of bottom sheet. It can be updated like:
bottomSheetBehavior.halfExpandedRatio = 0.6
bottomSheetBehavior.state = BottomSheetBehavior.STATE_HALF_EXPANDED
I'm migrating project to Compose. My app used this half expanded ratio for 3 positioned bottom sheet: collapsed, half expanded, expanded. Now i'm trying to create bottom sheet like that:
val sheetState = rememberBottomSheetState(
initialValue = BottomSheetValue.Collapsed
)
val scaffoldState = rememberBottomSheetScaffoldState(
bottomSheetState = sheetState
)
BottomSheetScaffold(
scaffoldState = scaffoldState,
sheetContent = {}
) {}
But it looks like we don't have those behavior attributes here. Can we get the same behavior as in XML with half expanded bottom sheet?
Yes, it's possible. But it's a little bit of work. You basically have to provide your own implementations of the classes BottomSheetScaffold and SwipeableState as well as any related enums, classes and Composable functions used by these two.
The main points to focus on:
provide a third value to BottomSheetValue for the half-expanded state
provide a function that animates the half-expansion (similar to functions expand() and collapse() in BottomSheetState) as well as a property that checks if the bottom sheet is half expanded (similar to val isExpanded: Boolean)
add a third swipeable anchor to BottomSheetScaffold with the half expansion value you want to achieve (check line fullHeight - peekHeightPx to Collapsed, here is where you want to add your custom anchor)
handle the half expanded state in the .semantics block of the Modifier in BottomSheetScaffold
You might need to tweak a few more things depending on your specific requirement. It can take a while to set up everything but once it's done, it works like a charm.

How to scroll Jetpack Compose screen that overflows its contents?

I have an Add Shopping List item Jetpack Compose screen which contains several TextField inputs and an image container at the bottom. The screen overflows at the bottom and is cut off. How can I scroll the screen?
Add Modifier.scrollable(..) to the container that you wish to make scrollable.
Code would be something like this:
val scrollState = rememberScrollState()
...
Box( // or whatever your parent composable is
modifier = Modifier
.scrollable(state = scrollState, orientation = Orientation.Vertical)
) {
...
}
Of course, there are other Modifier methods for making composables scrollable that might fit better for your case.

Jetpack Compose animation is laggy if runs inside xml ComposeView with wrap_content height

I have kind of persistent BottomSheet styled Composable added to Fragment's xml (of course, using ComposeView). I need it to take only the required height, so it's only attached to start, end and bottom of parent ConstraintLayout, like so:
<androidx.compose.ui.platform.ComposeView
android:id="#+id/dashboard"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
ComposeView's root is a Column that contains some type of expandable composable and buttons underneath it. I need ComposeView to shrink or grow when expandable content is collapsed/revealed respectively. I have expandable's content wrapped in AnimatedContent:
#Composable
fun DashboardExpandableContent(modifier: Modifier = Modifier, expanded: Boolean) {
AnimatedContent(
modifier = modifier,
targetState = expanded,
transitionSpec = {
fadeIn(animationSpec = tween(durationMillis = 250)) with
fadeOut(animationSpec = tween(durationMillis = 250)) using SizeTransform()
}
) { currentlyExpanded ->
if (currentlyExpanded) {
ExpandedContent()
} else {
CollapsedContent()
}
}
}
But everything shakes like crazy during animation when I set xml height to wrap_content. Looks like it's because Android tries to measure my ComposeView during animation.
A possible hack I found is to change height in xml to a static value (i.e, constraint it to parent top), and add a Spacer with weight 1.0f as the Column's first child. Then the animation works just fine.
But I need it to have a dynamic height because other views (like onboarding popups) in xml layout rely on that. How can I make it work?

Is there something like TextView’s center_vertical in Jetpack Compose?

I’d like to find some properties like android:gravity=“center_vertical” in Jetpack Compose without using Column, any ideas?
No, the only way is to use a container. And that is exactly how it should be used in Compose: the container does not add any problems, unnecessary load, etc. No reason why you should avoid them.
To me, using Column is not the most logical solution, because it is designed to vertically arrange several elements.
For a single item, Box is more clear to me:
Box(
contentAlignment = Alignment.Center,
modifier = Modifier.fillMaxSize()
) {
Text("Hello")
}
You can't place a text vertically center without using a container. But you can change the alignment of Text without using a container.
Text(textAlign: TextAlign.Center)
In case of height with wrapContent this will automatically place the text vertically centered.
Other values of TextAlign are Left, Right, Justify, Start and End

Compose Column content becomes unreachable if the size of the TextField increases

I have a Composable which has 2 TextFields and a Button in a Column, when the content of the TextFields increases, the Button at the bottom becomes unreachable.
Is there a way I can make the Column scrollable?
To create and automatically remember ScrollState with default parameters use rememberScrollState.
val scroll = rememberScrollState(0)
You can add the verticalScroll modifier to enable scrolling within the Column.
Column(
modifier = Modifier.verticalScroll(scroll)
) {
...
For more read and this

Categories

Resources