8

I had an issue that my ScrollView in fragment auto-scroll to bottom. So to fix this issue I have added following line in RelativeLayout as suggested in this answer:

android:descendantFocusability="blocksDescendants"

But after adding this line, textIsSelectable is stopped working for. How can I add both lines?

Here is the XML:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="15dp"
        android:textSize="23sp"
        android:text="The Beach Beneath the Street"
        android:fontFamily="sans-serif-medium"
        android:textColor="@color/black"
        android:textIsSelectable="true"/>

    <TextView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:padding="15dp"
        android:fontFamily="sans-serif-condensed"
        android:lineHeight="25dp"
        android:lineSpacingMultiplier="1"
        android:lineSpacingExtra="6dp"
        android:textSize="16sp"
        android:text="Never add a RecyclerView or ListView to a scroll view"
        android:textIsSelectable="true" />

</RelativeLayout>
Onik
  • 19,396
  • 14
  • 68
  • 91
Faisal Shaikh
  • 3,900
  • 5
  • 40
  • 77

3 Answers3

4

When you use android:descendantFocusability="blocksDescendants" in a ViewGroup it will block all its child view's from receiving focus

For more information please Check out the Docs

Must be one of the following constant values in android:descendantFocusability.

  1. afterDescendants:- The ViewGroup will get focus only if none of its descendants want it.
  2. beforeDescendants:- The ViewGroup will get focus before any of its descendants.
  3. blocksDescendants:- The ViewGroup will block its descendants from receiving focus.

Now your question

But after adding this line, textIsSelectable is stopped working for. How can I add both lines?

AFAIK you can't use both togather

You need to find an alternative solution for your issue of auto-scroll to bottom in a fragment

Here are some links that might help you for your issue of auto-scroll to bottom in a fragment

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • I don't want auto-scroll. My fragment automatically scrolled to bottom once it's loaded hence to fix this issue, I have added following line `android:descendantFocusability="blocksDescendants"`. I have added the for the same in my question. – Faisal Shaikh Jan 24 '19 at 14:02
2

First, make sure that your Relative Layout which contains your TextView has descendantFocusability set to "Before Descendants," focusable set to "true" and focusableInTouchMode set to "true". Next add an onTouchListener to your ScrollView that removes focus from your TextViews whenever it is touched, like so:

ScrollView scroll = (ScrollView)findViewById(R.id.myscrollView);
scroll.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (myTextView1.hasFocus()) {
            myTextView1.clearFocus();
        }
        if (myTextView2.hasFocus()) {
            myTextView2.clearFocus();
        }
        return false;
    }
});
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Vanshaj Daga
  • 2,145
  • 11
  • 18
2

One possible approach is to use the method fullScroll. According to the documentation

public boolean fullScroll (int direction)

Handles scrolling in response to a "home/end" shortcut press. This method will scroll the view to the top or bottom and give the focus to the topmost/bottommost component in the new visible area. If no component is a good candidate for focus, this scrollview reclaims the focus.

int direction: the scroll direction: View.FOCUS_UP to go the top of the view or View.FOCUS_DOWN to go the bottom

Therefore, you should remove

android:descendantFocusability="blocksDescendants"

and add the following lines

XML:

android:id="@+id/myScrollView"

JAVA:

myScrollView = (ScrollView)findViewById(R.id.myScrollView);
myScrollView.fullScroll(View.FOCUS_UP);
Community
  • 1
  • 1
S. Czop
  • 602
  • 1
  • 6
  • 17