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>
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
.
afterDescendants
:- The ViewGroup will get focus only if none of its descendants want it.beforeDescendants
:- The ViewGroup will get focus before any of its descendants.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
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;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With