In my app, I have a ScrollView with a LinearLayout whose visibility is set on GONE.
I need to make it visible and then have my ScrollView scroll to the bottom of the LinearLayout.
For this I'm using this code:
mLinearLayout.setVisibility(View.VISIBLE);
mScrollView.smoothScrollTo(0, mLinearLayout.getBottom());
This however, does not work. When the ScrollView is asked to scroll the LinearLayout still returns 0 on getBottom().
So when this is called for the first time, the LinearLayout is visible, but the scrollview has not scrolled.
When it is called for a second time, it does scroll down to the right position.
How can I fix this?
You need to put your smmothScrollTo method inside a new thread like this:
mScrollView.post(new Runnable() {
public void run() {
mScrollView.smoothScrollTo(0,mLinearLayout.getBottom());
}
});
Related
I want to make my Scrollview always scrollable even though it has no element in it.
I tried to extend a LinearLayout out of the screen, but it does not work.
csv=(ScrollView) findViewById(R.id.scrView);
csv.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
int scrollY= csv.getScrollY();
txtview.setText(String.valueOf(scrollY));
}
I want it because I want to see the image position which is the only one element in the ScrollView.
By this, I can move the image and see its y position.
It can be thought as vertical ViewPager.
I have a LinearLayout (with horizontal orientation) inside a HorizontalScrollView. When the user clicks on an element of the LinearLayout, I want it to always scroll so that the selected element is flush with the left side of the ScrollView.
Right now, the following code in my OnClickListener usually accomplishes what I want:
ReadView readView = (ReadView) view;
horizontalScrollView.smoothScrollTo(readView.getLeft(), 0);
Except in cases where the element is near the end of the LinearLayout, which makes sense. In an attempt to solve this, I add padding to the right side of the LinearLayout:
ReadView readView = (ReadView) view;
horizontalLayout.setPadding(0,0, 1000, 0);
horizontalScrollView.smoothScrollTo(readView.getLeft(), 0);
Except...that doesn't work. I've verified that the padding is being added correctly, but the ScrollView isn't scrolling based on the padding.
I want to be able to remove the padding afterward, so that when the user scrolls without clicking, they won't see the extra padding, so I'd prefer to be able to do this in the OnClickListener.
I found a solution: use a ViewTreeObserver.
ViewTreeObserver vto = horizontalLayout.getViewTreeObserver();
vto.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
#Override
public void onDraw() {
vto.removeOnDrawListener(this);
horizontalScrollView.smoothScrollTo(readView.getLeft(), 0);
/* ... */
}
});
horizontalLayout.setPadding(0,0, 1000, 0);
I have two rows of imageViews in a ScrollView. The imageViews all have onClickListener and when i want to scroll in this area it doesn't work. So i guess the Click listeners intercept the scrolling of the ScrollView. What's the best way to change this behaviour ?
My View hierarchy is like this:
<ScrollView>
<RelativeLayout>
<FrameLayout>
</FrameLayout>
<RelativeLayout>
</ScrollView>
in the FrameLayout i put a Fragment which has a LinearLayout in which i inflate other LinearLayouts like this:
productHolder.productLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
}
});
Maybe you could use an OnTouchListener instead of an OnClickListener.
In the OnTouchListener you can check if the action of the motion event was a click and consume the event (return true). Otherwise you can leave the event for other listeners (return false) to consume.
Note, that you might need to add some additional implementation for figuring out which image view was clicked, however without seeing your layout, it's hard to give you a hint how to do it.
I have application with ListView. This ListView has a Progressbar.When I am clicking on this Progressbar, then it is showing a hided Layout(This layout contain textview with lyrics). So as I am clicking on the last Progressbar then it is showing the hided Layout, but I need to scroll manually. I want that scrolling must be automatically.
Please help me.
Try something like this:
private void scroll2bottom() {
yourListView.post(new Runnable() {
#Override
public void run() {
yourListView.setSelection(yourListAdapter.getCount() - 1);
}
});
}
You might find the smoothScrollByOffset() or smoothScrollBy() methods of ListView most useful. In particular smoothScrollBy() takes both a distance to scroll and a duration over which the scroll animation should take place. With this method you can control precisely how far to scroll the list.
in your layout of your listview just use the transcriptMode
a listview has builtin auto scroll
android:transcriptMode="normal" witll scroll when its at the bottom but not when you have scrolled away
and the setting alwaysScroll will always scroll to the bottom
and then you can set the android:stackFromBottom="true" to make it grow from the bottom
Try in this way:
lv.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
lv.setStackFromBottom(true);
To scroll a listView programmatically you can use its smoothScrollByOffset(int offset) or smoothScrollToPosition(int position) methods, you can find here the documentation about the ListView widget and how to use those methods.
I have a ScrollView which I'm adding a number of custom Views to. After adding the custom Views, I would like to be able to Scroll to a particular one. But, immediately after adding the custom views, the ScrollView hasn't been resized, so I can not scroll. Any idea how I can force this resize?
for(int i = 0, i < numberOfViews, i++)
{
scrollView.addView(new MyView(...));
}
//tried this, doesn't seem to be helpful
scrollView.requestLayout();
//at this point scrollView dimensions are 0,0,0,0
scrollView.scrollTo(5, 200);
I'm not completely sure what you're trying to do, but I wanted to scroll all the way down, and couldn't just do that: I had to do it like this:
((ScrollView) findViewById(R.id.yourId)).post(new Runnable() {
public void run() {
((ScrollView) findViewById(R.id.yourId)).fullScroll(View.FOCUS_DOWN);
}
});
It will update later, so your view is made :)
An elegant solution is to plan the scroll and do it on the next onLayout(). Example code here:
https://stackoverflow.com/a/10209457/1310343