1 year ago

#10781

test-img

krishnaacharyaa

Scroll to selected chip in horizontalScrollView

I have HorizontalScrollView , Which has a Horizontal Linear layout with 7 chips. In which some chips are out of screen.

Selected chipID is stored in DataStore (branchTypeID)

So now the next time this view(BottomSheetDialog) is created. I want to have auto scrolled to the position of the selected chip

But ,next time the chip Group View is created, it starts from the beginning . The selected chip is off the screen.Then manual scrolling is required. Which i want to avoid.

xml is as follows

<HorizontalScrollView
            android:id="@+id/branches_scrollView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/branches_linearLayout"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <com.google.android.material.chip.ChipGroup
                    android:id="@+id/branchType_chipGroup"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:checkedChip="@+id/cs_chip"
                    app:selectionRequired="true"
                    app:singleLine="false"
                    app:singleSelection="true">

                    <com.google.android.material.chip.Chip
                        android:id="@+id/cs_chip"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <com.google.android.material.chip.Chip
                        android:id="@+id/ec_chip"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <com.google.android.material.chip.Chip
                        android:id="@+id/ee_chip"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"/>

                    <com.google.android.material.chip.Chip
                        android:id="@+id/mech_chip"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"/>

                    <com.google.android.material.chip.Chip
                        android:id="@+id/civil_chip"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <com.google.android.material.chip.Chip
                        android:id="@+id/ch_chip"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"/>

                    <com.google.android.material.chip.Chip
                        android:id="@+id/oth_chip"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"/>


                </com.google.android.material.chip.ChipGroup>
            </LinearLayout>
        </HorizontalScrollView>

In the program

    val chip = mView.branchType_chipGroup.findViewById<Chip>(branchTypeId)
    if (chip != null)
        mView.branches_scrollView.scrollTo(chip.left, chip.right)

The above code is not working

android

android-layout

android-linearlayout

horizontalscrollview

android-chips

0 Answers

Your Answer

Accepted video resources