I have a ScrollView (and a LinearLayout within it) set as main content. When the user scrolls the view further down, and then if I replace it's child (LinearLayout) with another LinearLayout, the view remains in the scrolled positioned.
How do I reset the ScrollView back to coordinate 0?
Why not just use scrollTo(0, 0);?
Link: http://developer.android.com/reference/android/view/View.html#scrollTo(int%2C%20int)
If u want to clear the scrollview u can use
removeAllViews();
Its all in the documentation... http://developer.android.com/reference/android/widget/ScrollView.html search for fullScroll(int)
When scrollView.scrollTo(0, 0) & scrollView.fullScroll(View.FOCUS_UP) doesn't work, you can accomplish the intended with:
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.scrollTo(0, 0);
}
});
yourScrollView.scrollTo(0,0) will do the job just fine.
Just use
ScrollView hscrollViewMain = (ScrollView)findViewById(R.id.scrollViewMain);
hscrollViewMain.scrollTo(0, 0);
Related
I have a textview which only shows 3 lines and then you have to scroll to the other ones.
I set the textview in my xml file as scrollable and activated its scrolling method with textview.setMovementMethod(new ScrollingMovementMethod()).
The scrolling works perfectly fine.
The problem I face is, that I need to know when the user has scrolled to the bottom of the textview in the case that the textview has more than 3 lins. Is there a way to check when the user has scrolled to bottom of the textview?
I played a little bit around with the OnTouchListener, OnDragListener and OnHoverListener but none of them really worked.
If you need more details, just let me know.
Thanks for your help!
You can try this :
scrollView.getViewTreeObserver()
.addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
if (scrollView.getChildAt(0).getBottom()
<= (scrollView.getHeight() + scrollView.getScrollY())) {
//scroll view is at bottom
} else {
//scroll view is not at bottom
}
}
});
Best
Is there anyway I can make a Scrollview start on bottom? I tried to:
post(new Runnable() {
#Override
public void run() {
fullScroll(ScrollView.FOCUS_DOWN);
}
});
And also setting the android:focusableInTouchMode="true" on the lowest element in the layout.
But I can see it scrolling to the bottom sometimes. The idea is to make it imperceptible to the user.
Thanks a lot.
try with scrollView.fullScroll(View.FOCUS_DOWN)
Try this
scrollView.scrollTo(0, scrollView.getBottom());
Since I was adding Views dynamically, I also had to allow Android to complete the layout on the content view inside the ScrollView. I just added a post at the end of the View's queue, that will run after it has completed its layout. Otherwise, Android was trying to use the old size of the content view, and wasn't scrolling all the way down.
mMessagesLayout.addView(messageLayout);
mMessagesLayout.requestLayout();
mMessagesLayout.post(new Runnable() {
#Override
public void run() {
mScrollView.scrollTo(0, mScrollView.getBottom());
}
});
I set my TextView to be scrollable:
textView.setMovementMethod(new ScrollingMovementMethod());
But, when I update my TextView's texts, the scrolling position keeps at the last text position, resulting in, sometimes, the text getting invisible and then, I need to move the scroll to see the new text (I update with: textView.setText(newText)).
I tried those codes, but no changes were noted:
textView.invalidate();
textView.requestLayout();
textView.scrollBy(0, 0)
textView.scrollTo(0, 0);
P.S.: the textView is a child of a RelativeLayout.
Why don't you just use a ScrollView in xml will be much easier open the ScrollView open a layout set it how you want it to be horizontal or vertical put your TextViews in and then close first the layout and then ScrollView and Voila done..
What I did to solve my problem:
textView.post(new Runnable() {
#Override
public void run() {
textHeight = textViews.getLineHeight() * textViews.getLineCount();
if (textHeight > relativeLayout.getHeight())
textView.setMovementMethod(new ScrollingMovementMethod());
else
textView.setMovementMethod(null);
}
});
My app opens up in View A. In View B, I have made a custom popover who's view contains:
LinearLayout
ScrollView
LinearLayout1
LinearLayout2
.
.
.
LinearLayoutN
What I wish to do is From View A, Move Into View B having set the Vertical Scroll position to a specific LinearLayout from the ScrollView within the Popup.
I have the scrollview being assigned in code to a variable and
variable.scrollTo
is NOT working.
I have also tried to put
sView.post(new Runnable() {
#Override
public void run() {
sView.scrollTo(0, myNum);
}
});
within the routine I run to setup View B, Tried it Outside in the OnCreate routine. Nothing seems to be working to scroll the ScrollView.
The
sView.post(new Runnable() {
#Override
public void run() {
sView.scrollTo(0, scrollYPos);
}
});
was EXTREMELY useful, however its placement was even more important. Do to the scrollview being inside of a popover view, the code above actually had to be placed AFTER the code that truly brought that popover onto screen.
I have a webview in a scrollview, when the Activity loads, it forces my scrollview to the bottom (where the webview is) once the webview finishes "loadData".
How do I keep this from happening?
I've tried this, but it jumps the screen up and down, which I don't want:
ScrollView scroll = (ScrollView)findViewById(R.id.detailsScroll);
scroll.post(new Runnable()
{
#Override
public void run()
{
ScrollView scroll = (ScrollView)findViewById(R.id.detailsScroll);
scroll.fullScroll(ScrollView.FOCUS_UP);
}
});
The answer of fhucho is OK for stoping the webview from scrolling to bottom. But you will lose all accessibility with trackball, etc. So I found an improvement :
public class MyScrollView extends ScrollView {
#Override
public void requestChildFocus(View child, View focused) {
if (focused instanceof WebView )
return;
super.requestChildFocus(child, focused);
}
}
Hope it helps !
Create a custom ScrollView with this method:
#Override
public void requestChildFocus(View child, View focused) {
if (child instanceof WebView) return;
}
For focus related issues, I found this worked better, adding these to your WebView in XML:
android:focusable="false"
android:focusableInTouchMode="false"
Although for the actual question asked, just adding another view that gets the focus by default, instead of the WebView, should have resolved that issue. I think others like me might find this when trying to deal with other general issues with a WebView inside a ScrollView taking the focus. Adding the above to the XML does not stop hyperlinks in the WebView from working.
add this to your main layout
android:descendantFocusability="blocksDescendants"