Where Does Android View.Scrollto(X, Y) Scroll To

Where does Android View.scrollTo(x, y) scroll to?

After extensive research and testing I've finally understood how scrollTo() works.

(0,0) are the coordinates to the top left corner of the View container. When scrolling to any (x,y) point, the top left corner of the View will be placed at the (x,y) coordinates.

If the View is showing an image, Bitmap, larger than the View itself, scrolling to (0,0) will place the View in the center of the image. This way, the top left corner of the image will be located at (-dX/2, -dY/2) and the bottom right corner at (mW - dX/2, mH - dY/2). dX represents the difference between the widths of the image and the View. And dY represents the difference between the heights of the image and the View.

In order to see the bottom right corner and not go passed it (lower or further to the right), this is the proper call: scrollTo(mW - ivW - dX/2, mH - ivH - dY/2);

The attached image shows the graphical representation of the View and Bitmap image positioning.

Graphical representation of the relationship between the image and the view.

View.scrollTo(int x, int y) don't work in Android

You cannot scroll an ImageView with the scrollTo function.

ScrollTo() is used to manipulate actual scroll bars that you usually find in a ScrollView or a RecyclerView. When you use ScrollTo the scroll bars are moved with respect to the X,Y that you provide to the function. You do not actually move any views you just move the focus of your screen in a particular place.

You will probably have more luck with something like this:

ImageView mView;

float lastScrollX;
float lastScrollY;

...

@Override
public void onClick(View v) {
lastScrollX = mView.getScrollX();
lastScrollY = mView.getScrollY();
lastScrollY += 50.0;
lastScrollX += 50.0;
mView.setX(lastScrollX)
mView.setY(lastScrollY)
mView.invalidate();
}

Programmatically scroll to the top of a NestedScrollView

I managed to do it (animated scrolling):

CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
if (behavior != null) {
behavior.onNestedFling(coordinatorLayout, appBarLayout, null, 0, 10000, true);
}

where 10000 is the velocity in y-direction.

Confused about the scrollTo(int x,int y) method in Android

The direction of the scroll is not related to the sign of the x parameter for the scrollTo method.

This is your x axis:

<------------------------------------------------------------->
| | | | | | | | | | | | | | |
-7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7

The scrollTo(int x, int y) method will scroll your View to a specific position.

Let's say the current x position of your View is 3. If you do this:

scrollTo(1, 0);

You make the View scroll to the right, because the current value of x (3) is greater than the expected value (1).

Now, if you do this:

scrollTo(5, 0);

You make the view scroll to the left because the current value of x (3) is lower than the expected value (5).

To make it simple, the direction of the scroll for the scrollTo method only depends on the difference between the current and the expected values:

  • If expectedValue < currentValue then scroll to right
  • If expectedValue > currentValue then scroll to left

However, the reasoning that you had is correct for the scrollBy(int x, int y) method.

EDIT

Here is a more detailed explanation.

Let's say, again, that your View's current x value is 3. It means that the left border of your screen (for example) matches with the 3 value on the x axis, it can be seen as below:

<------------------------------------------------------------->
| | | | | | | | | | | | | | |
-7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7
^
|
LEFT BORDER

Then you want to put your View to 7 on the x axis, it means that you want to put the value 7 to the left border of your screen:

<------------------------------------------------------------->
| | | | | | | | | | | | | | |
-7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7
^ ^
| |
LEFT BORDER EXPECTED

The LEFT BORDER can't move as it is the left border of your screen. So the only solution is to move the EXPECTED position to the left. It's the same when the expected value is lower than the current one.

It's pretty complicated to explain this easily, hope you'll get it.

Is there a way to programmatically scroll a scroll view to a specific edit text?

private final void focusOnView(){
yourScrollView.post(new Runnable() {
@Override
public void run() {
yourScrollView.scrollTo(0, yourEditText.getBottom());
}
});
}

Android ScrollView scrollTo() method not working with TextView?

First of all, if all your LinearLayouts have the same orientation, why you don't simply create it only once?
If I'm not wrong, ScrollView can only have 1 child (1 direct child). Maybe that's probably the reason of your problems.
However, if you still want those 3 LinearLayouts, you can create a FrameLayout as a parent to hold them and make that FrameLayout the child of the ScrollView

Android calculate scrollTo position in HorizontalScrollView

Try this:

int x, y;
x = myTextView.getLeft();
y = myTextView.getTop();

myHsv.scrollTo(x, y);

getLeft() and getTop() should return the coordinates relative to the parent view (that is, the HorizontalScrollView in this instance.

How to set the starting position of a ScrollView?

Yes, that is possible:

ScrollView.scrollTo(int x, int y);
ScrollView.smoothScrollTo(int x, int y);
ScrollView.smoothScrollBy(int x, int y);

can be used for that. The x and y parameters are the coordinates to scroll to on the horizontal and vertical axis.

Example in code:

    @Override   
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);

ScrollView sv = (ScrollView) findViewById(R.id.scrollView);
sv.scrollTo(0, 100);
}

In that example, once the Activity is started, your ScrollView will be scrolled down 100 pixels.

You can also try to delay the scrolling process:

final ScrollView sv = (ScrollView) findViewById(R.id.scrollView);

Handler h = new Handler();

h.postDelayed(new Runnable() {

@Override
public void run() {
sv.scrollTo(0, 100);
}
}, 250); // 250 ms delay


Related Topics



Leave a reply



Submit