I'm just wondering what this does.
I've tried using:
scrollView.smoothScrollTo(0, scrollView.bottom)
, but it doesn't even scroll all the way to the bottom...
Thanks
Try to use the code below. It should work for you.
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.smoothScrollTo(0, scrollView.bottom);
}
});
Kotlin:
scrollView.post { scrollView.smoothScrollTo(0, scrollView.bottom) }
Feel free to ask if something is unclear.
Related
I am new to Android Programming. I am using the scroll view for rendering a view.
My XML view as follows:
<ScrollView>
<LL1> <TV1> </LL1>
<LL2> <TV2> </LL2>
<LL3> <TV3> </LL3>
<LL4> <TV4> </LL4>
<LL5> <TV5> </LL5>
<LL6> <TV6> </LL6>
</ScrollView>
When I click on TV6 then LL6 layout should move to the top of the screen.
I tried following things but not working properly:
- TV6.getParent().requestChildFocus(TV6,TV6);
- scrollView.smoothScrollTo(0, 0);
Let me know how to achieve this.
Try this one, maybe this will work for you
scrollView.post(new Runnable() {
public void run() {
scrollView.fullScroll(scrollView.FOCUS_DOWN);
} });
or
scrollView.post(new Runnable() {
public void run() {
scrollView.scrollTo(0, scrollView.getBottom());
} });
I have multiple views in my layout inside a ScrollView and there is a validation on certain items, in case of any error I scroll to that view using the following:
view.post(new Runnable() {
#Override
public void run() {
view.scrollTo(0, view.getBottom());
view.getParent().requestChildFocus(view, view);
}
});
But the problem here is as soon as this code runs the scroll is achieved but the view passed to this makes itself invisible. After removing this everything is working fine.
I think this is some sort of bug with scrollview.
Your view is inside scrollView and you have added scrollTo() method in your view not in scrollView. Replace view.scrollTo(0,view.getBottom()) to scrollview.scrollTo(0, view.getBottom()).
Try below solution this might work for you.
view.post(new Runnable() {
#Override
public void run() {
scrollview.scrollTo(0, view.getBottom());
view.getParent().requestChildFocus(view, view);
}
});
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've subclassed HorizontalScrollView so that I can have some custom scrolling behavior, but have found that smoothScrollTo doesn't always fire.
I've had to work around this problem by using the following code:
smoothScrollTo(x, y);
scrollTo(x, y);
This makes sure the scrolling actually gets done even if smoothScrollTo doesn't work, since scrollTo works every time. Why is this happening? How can I get smoothScrollTo to work every time?
try this:
mScrollView.post(new Runnable() {
#Override
public void run() {
mScrollView.smoothScrollTo(x, y);
}
});
Following code will work:
final int scrollposition = Math.round(hr/24.0f * 1440f);
final ScrollView sv = (ScrollView)findViewById(R.id.graphScrollView);
//sv.smoothScrollTo(0, scrollposition);
sv.post(new Runnable() {
#Override
public void run() {
sv.smoothScrollTo(0, scrollposition);
}
});
Reason:
It will wait for the scrollview to be posted before running the underlying code.
Would it be possible to hide a view off the top edge of the screen, and only have it appear if the user scrolls upwards?
My first attempt used a scrollview, but it seems that scrollTo() doesn't work unless I used postDelayed (it doesn't even work with Post()). I tried adding it to the scrollview's view tree observer onPreDraw() event and it still doesn't work unless I delay it, so there is an ugly glitch when the activity is first launched.
The second issue is that if the onscreen keyboard is minimized, the view no longer needs to scroll so hiding things by using a scroll offset no longer works. I thought about manipulating the height in code, but this seems pretty hackish.
Is there a better way to do this than by using a scrollview? Alternatively, Does anyone have any tips on the best place to place the scrollTo (the end of onCreate does not work nor the other places I have tried) so I don't need to use postDelayed? That would at least eliminate one glitch.
Thanks!
This is the code I'm using right now, which is the least glitchy but I don't understand why it doesn't work until the third time onPreDraw() is called.
mScrollView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener()
{
#Override
public boolean onPreDraw()
{
final int fieldYStart = mFieldIWantAtTheTop.getTop();
if (mFieldIWantAtTheTopYStart != fieldYStart
|| mScrollView.getScrollY() < 10)
{
mFieldIWantAtTheTopYStart = fieldYStart;
mScrollView.post(new Runnable()
{
#Override
public void run()
{
Log.v("Testing", "scrolling!");
mScrollView.scrollTo(0, mFieldIWantAtTheTopYStart);
Log.v("Testing", "scroll is now=" + mScrollView.getScrollY());
}
});
}
return true;
}
});
I also tried using a custom scrollview as mentioned below, but this does not solve the issue of the graphical glitch:
#Override
public void onMeasure(int measureWidthSpec, int measureHeightSpec) {
super.onMeasure(measureWidthSpec, measureHeightSpec);
Log.v("Testing", "Scrolling");
post(
new Runnable()
{
public void run()
{
scrollTo(0, 100);
Log.v("Testing", "ScrollY = " + getScrollY());
}
});
}
This code works as does the onPreDraw() code above but there is still a glitch when the activity is launched because the activity is first drawn with the scroll at 0.
I haven't tried this, but you may want to create a custom ScrollView and override onMeasure:
ScrollView scroll = new ScrollView(this) {
#Override
public void onMeasure(int measureWidthSpec, int measureHeightSpec) {
super.onMeasure(measureWidthSpec, measureHeightSpec);
scrollTo(...);
}
};
It seems like this would be the earliest point that scrollTo would be valid.
Edit - I found this answer, which apparently worked for the asker. Is this the method you tried?