Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

textIsSelectable is not working with descendantFocusability=blocksDescendants

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>
like image 528
Faisal Shaikh Avatar asked Jan 08 '19 17:01

Faisal Shaikh


2 Answers

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

  • android:descendantFocusability

    Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.

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

  • How to scroll to top of long ScrollView layout?
  • Android: How to scroll ScrollView in top
  • Can I scroll a ScrollView programmatically in Android?
like image 158
AskNilesh Avatar answered Nov 10 '22 00:11

AskNilesh


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;
    }
});
like image 21
Vanshaj Daga Avatar answered Nov 10 '22 00:11

Vanshaj Daga