2

I have the following Layout:

<ScrollView>
    <RelativeLayout>
        <RelativeLayout>
            <View
                android:id="@+id/vline"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/md_white_1000"/>
            <ImageButton>...</ImageButton>
        </RelativeLayout>
    </RelativeLayout>
</ScrollView>

The different Layouts have there own content and height on different screens of course.

Now I want, by pressing the ImageButton, to smoothly scroll my ScrollView up so that the Views upper left corner is in the upper left corner of the specific screen.

I tried:

ImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollView.smoothScrollTo(0, vline.getTop());
            }
        });

and all the other "getY()" methods, but no one bring the result I want to have. It just scrolls all of my content fully high.

I'm just getting started with Android so thank you for your help.

kuemme01
  • 472
  • 5
  • 19
  • Why do you have to relative layouts? One of them is useless and should be removed. And the other one can be replaced with a simple linear layout. Van you attach an image where you illustrate the view transition? I did not get what you want to achieve. – Thomas R. Sep 24 '15 at 08:36
  • The two RelativeLayouts has there own content, I delete here for more clarity. I need them – kuemme01 Sep 24 '15 at 08:45
  • I found a solution, with getLocationInWindow( ). Now I just have to find a way to get the height of the Statusbar an subtract it from. – kuemme01 Sep 24 '15 at 09:58

1 Answers1

0

Here is the Solution:

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int statusbarHeight = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight()
                        - YOURVIEW.getHeight();
                int[] position = new int[2];
                vline.getLocationInWindow(position);
                scrollView.smoothScrollBy(0, (position[1]-statusbarHeight));
            }
        });
kuemme01
  • 472
  • 5
  • 19