Search code examples
androidandroid-layouthorizontalscrollview

Scroll to particular view


I have home activity on which I have 3 buttons : Move To A , Move To B and Move To C.

On click of these buttons, I navigate to inner activity where I have 3 layouts within a HorizontalScrollView, say A, B and C which in turn consists of several views.

Now what I want is , inner activity should navigate to one of the 3 layouts based on clicked button from Home activity i.e.

If Move To A on home activity is pressed, inner activity should navigate to Layout A and so on...

Possible solution might be to use ScrollTo() but it isn't efficient with variable screen sizes.

Any help appreciated.

Edit

This doesn't work:

RelativeLayout LayoutA = (RelativeLayout) findViewById(R.id.LayoutA);
HorizontalScrollView main = (HorizontalScrollView) findViewById(R.id.scrollview);
main.scrollTo(PoCLayout.getLeft(), 0);

Even this doesn't work:

main.scrollTo(500, 0);

Solution

  • If main.scrollTo(500,0) does not work, try :

    main.post(new Runnable() {
        @Override
        public void run() {
            main.scrollTo(500, 0);
        }
    }
    

    Edit : the working solution in the listener of the button :

    main.post(new Runnable() {
        @Override
        public void run() {
            main.scrollTo(layoutX.getLeft(), 0);
        }
    }