Closed MasoudFallahpour closed 4 years ago
This works as intended. You don't want to center inside a ScrollView.
You want to change the starting scroll position. This is very unnatural because if you think of scrollable content you wouldn't want to hide the beginning.
Regardless what you want could be something like this:
override fun onResume() {
super.onResume()
scrollView.post {
val position = (toggleGroup.width - scrollView.width) / 2f;
scrollView.scrollTo(position.toInt(), 0);
}
}
I had the same need and saw the issue mentioned by OP but ended up with this,
<HorizontalScrollView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center">
<com.google.android.material.button.MaterialButtonToggleGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:singleSelection="true">
<com.google.android.material.button.MaterialButton
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
...
</com.google.android.material.button.MaterialButtonToggleGroup>
</HorizontalScrollView>
It won't change starting scroll position which wasn't needed on my case, it just puts content at center if the content short enough but takes all the space and adds horizontal scroller if the content is lengthy. Hopefully the approach won't have a not known side effect but doesn't hit the issue mentioned by OP at least I can say.
Description: I want to display a single selection list of buttons using
MaterialButtonToggleGroup
. Because of the number of buttons that I want to display, some buttons go beyond the edge of the screen so I nested theMaterialButtonToggleGroup
inside aHorizontalScrollView
. When I setandroid:layout_gravity="center"
onMaterialButtonToggleGroup
some buttons could not be reached with scrolling. If I removeandroid:layout_gravity="center"
then everything works fine.Here are some screenshots. In the first screenshot button "A" is out of reach and button "B" is partially displayed. In the second one I scrolled to the right and there is a huge gap after the last button.
Expected behavior: All buttons should be reachable no matter what
android:layout_gravity
is used.Source code: The following layout demonstrates the issue.
Android API version: 29
Material Library version: 1.3.0-alpha01
Device: Any device