0

I have 3 HorizontalScrollview, for each of them i have customView contain editText that i add dynamically depend from web service response. The problem is, when i want to change value in editText and keyboard appear, the HorizontalScrollView scroll automatically to the end, so i can't properly input value to my edit text. My Question is how to prevent autoscroll in my HorizontalScrollView so i can input value on my editText? and the HorizontalScrollView only scroll when user scroll it.

I've search and my problem similar with this thread but the sugestion didn't work. here is workaround that i already try :

  1. Add android:windowSoftInputMode="stateHidden|adjustNothing" or android:windowSoftInputMode="stateHidden|adjustResize" already try adjustPan and other combination but it's impact nothing.
  2. Add android:descendantFocusability="beforeDescendants" android:focusable="true" android:focusableInTouchMode="true" in LinearLayout inside my HorizontalScrollView and it's also impact Nothing.

  3. Add android:descendantFocusability="blocksDescendants the HorizontalScrollView didn't scroll automatically but i can't change value on my edit text, it similar when i add android:focusable = "false" in my editText

  4. Add code bellow but impact nothing.

    scrollLayout.descendantFocusability = ViewGroup.FOCUS_BEFORE_DESCENDANTS scrollLayout.isFocusable = true scrollLayout.isFocusableInTouchMode = true scrollLayout.setOnTouchListener { v, event -> v.requestFocusFromTouch() false}

Here is My Code

private fun setUpList(rootLayout: LinearLayout, scrollLayout: HorizontalScrollView, items: List<ProductItem>) {
    //config horizontal scroll view
    scrollLayout.descendantFocusability = ViewGroup.FOCUS_BEFORE_DESCENDANTS
    scrollLayout.isFocusable = true
    scrollLayout.isFocusableInTouchMode = true
    scrollLayout.setOnTouchListener { v, event ->
        v.requestFocusFromTouch()
        false
    }
    // add customView to linear layout
    rootLayout.removeAllViews()
    for (item in items) {
        val view = EditableThumbnailProduct(context)
        view.setProductInfo(item)
        view.setOnProductChangeListener(onChangeListener())
        rootLayout.setBackgroundColor(Color.TRANSPARENT)
        rootLayout.addView(view)
    }
}

Here is my XML

<HorizontalScrollView
            android:id="@+id/scrollMed"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none">

            <LinearLayout
                android:id="@+id/contentMed"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="@android:color/transparent"
                android:descendantFocusability="beforeDescendants"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:orientation="horizontal" />
        </HorizontalScrollView>

Here is my custom view

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@android:color/transparent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/img_thumbnail"
        android:layout_width="150dp"
        android:layout_height="120dp"
        android:background="@color/gray_border"
        android:scaleType="fitXY" />

    <TextView
        android:id="@+id/txt_product_name"
        style="@style/DefaultTextTitle"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_margin="5dp"
        android:ellipsize="end"
        android:gravity="center"
        android:maxLines="2"
        android:text="@string/product_name_label" />

    <TextView
        android:id="@+id/txt_product_stock"
        style="@style/DefaultTextSubTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:gravity="center"
        android:text="@string/available_stock_label" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/gray_border" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:orientation="vertical">

        <Button
            android:id="@+id/btn_add_to_cart"
            style="@style/WhiteTitle"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_margin="10dp"
            android:background="@drawable/round_button_orange"
            android:gravity="center"
            android:text="@string/buy_button_label"
            android:visibility="invisible" />

        <RelativeLayout
            android:id="@+id/layout_plus_minus"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_margin="10dp"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/btn_minus"
                style="@style/WhiteTitle"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_alignParentLeft="true"
                android:background="@drawable/round_button_blue"
                android:paddingLeft="5dp"
                android:paddingTop="11dp"
                android:paddingRight="5dp"
                android:paddingBottom="11dp"
                android:scaleType="fitXY"
                app:srcCompat="@drawable/ic_minus" />

            <ImageView
                android:id="@+id/btn_delete"
                style="@style/WhiteTitle"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_alignParentLeft="true"
                android:background="@drawable/round_button_blue"
                android:padding="3dp"
                android:scaleType="fitXY"
                android:visibility="invisible"
                app:srcCompat="@drawable/ic_delete" />

            <EditText
                android:id="@+id/txt_cart_amount"
                style="@style/LightTextTitle"
                android:layout_width="50dp"
                android:layout_height="match_parent"
                android:layout_marginLeft="40dp"
                android:layout_marginRight="10dp"
                android:backgroundTint="@color/blue_text_color"
                android:inputType="number"
                android:maxLength="3"
                android:paddingBottom="5dp" />

            <ImageView
                android:id="@+id/btn_plus"
                style="@style/WhiteTitle"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_alignParentRight="true"
                android:background="@drawable/round_button_blue"
                android:gravity="center"
                android:padding="5dp"
                android:scaleType="fitXY"
                android:textSize="16sp"
                app:srcCompat="@drawable/ic_plus" />

        </RelativeLayout>
    </RelativeLayout>
</LinearLayout>

All sugestion will be appreciate. Thanks in advance.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
WardaLyn
  • 164
  • 1
  • 1
  • 10
  • Hi, try the answer from link. https://stackoverflow.com/questions/34298356/android-auto-scroll-when-clicked-on-edittext – jessica Oct 23 '18 at 07:07
  • hy @jessica, i want to prevent autoscroll, already edit my thread so other can have better understanding. thx – WardaLyn Oct 23 '18 at 07:19

0 Answers0