scrollview move section to top - android

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());
} });

Related

Why does smoothScrollTo() not scroll to the bottom?

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.

Scrollview making view invisble

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);
}
});

Auto Scroll a Android ViewGroup top to bottom and bottom to top

Here I'm trying to scroll a GridView automatically top to bottom and as it comes to last item scroll it back to top from bottom using smoothScrollToPosition but its very fast. The code is :
homeGridView.postDelayed(new Runnable() {
public void run() {
// ObjectAnimator.ofInt(homeGridView, "scrollY", homeGridView.getCount()-1).setDuration(1000).start();
homeGridView.smoothScrollToPosition(homeGridView.getCount() - 1);
homeGridView.postDelayed(new Runnable() {
public void run() {
// ObjectAnimator.ofInt(homeGridView, "scrollY", 0).setDuration(1000).start();
homeGridView.smoothScrollToPosition(0);
}
}, 1000);
}
}, 1000);
Now I just want to know that how can I decrease speed of this scroll or something better this.

Android - Scrollview start at the bottom

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());
}
});

scrollTo always works, smoothScrollTo only sometimes

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.

Categories

Resources