How to scroll to a specific view and focus the view with scroll view in android

As many of the developer face this problem to scroll the view to specific view to focus the child view of scroll view. Let’s see an example, we have huge form where user need to fill to submit. But in the big form if user is missing any filling field or view then it is very difficult for user to finds the field which are missing to fill. Now we need to scroll the view to this specific view and focus this view for the user, then it becomes easy for the user to understand to finding the missing information to filled.

<ScrollView
            android:id="@+id/scrollView2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            card_view:layout_constraintBottom_toBottomOf="parent"
            card_view:layout_constraintEnd_toEndOf="parent"
            card_view:layout_constraintStart_toStartOf="parent">


  <LinearLayout
                android:id="@+id/detail_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#f1f6fb"
                android:orientation="vertical"
                tools:visibility="visible">


                        <com.google.android.material.textfield.TextInputLayout
                            android:id="@+id/number_inputlayout">

                            <com.google.android.material.textfield.TextInputEditText
                                android:id="@+id/number_edittext"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                />

                        </com.google.android.material.textfield.TextInputLayout>

                        ..............
                       .............
   </LinearLayout>
        </ScrollView>

Here is solution.

private fun scrollToView(scrollViewParent: ScrollView, view: View) {
       // Get deepChild Offset
       val childOffset = Point()
       getDeepChildOffset(scrollViewParent, view.parent, view, childOffset)
       // Scroll to child.
       scrollViewParent.smoothScrollTo(0, childOffset.y)
   }

   private fun getDeepChildOffset(
       mainParent: ViewGroup,
       parent: ViewParent,
       child: View,
       accumulatedOffset: Point
   ) {
       val parentGroup = parent as ViewGroup
       accumulatedOffset.x += child.left
       accumulatedOffset.y += child.top
       if (parentGroup == mainParent) {
           return
       }
       getDeepChildOffset(mainParent, parentGroup.parent, parentGroup, accumulatedOffset)
   }

That is. Now it becomes easy to user to identify the view which is missing to filled. Ref 

Happy Coding.

0 0 votes
Article Rating

Recent Posts

Hide your production API key or any sensitive data in Android

Hi everyone, In this article, we are going to learn how to hide the production… Read More

2 years ago

How to handle the localisation or multi language support in android with examples?

Hello everyone, Today in this article, we are going to learn about localisation to support… Read More

2 years ago

How to convert any callback to Coroutines and use them in Kotlin Android?

Hello everyone, In this article, we are going to learn something to handle the callback… Read More

2 years ago

Request Permission Launcher with Kotlin in Android

In this article, we are learning about the run time permissions for request permission launchers.… Read More

2 years ago

Implement the SMS User Consent API and SMS Retriever API in Android

Hello everyone. In my last tutorial, we learned about the Jetpack Compose introduction and about applying the… Read More

3 years ago

Jetpack Compose Coroutine flow with LiveData/ViewModel in Android

Hello everyone, In this article, we are going to learn about the Jetpack Compose with… Read More

3 years ago