user3502626 user3502626 - 5 months ago 172
Android Question

How to get the scrollview total height = Scrollview.getScrollY() on Android

I have already look on some forums but the tricks do not work for me.

I have this code:

scrollview = (ScrollView)v.findViewById(R.id.scrollView);
scrollview.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {

@Override
public void onScrollChanged() {

Log.d(MainActivity.TAG, "scroll ch: "+scrollview.getScrollY()+"/"+scrollview.getChildAt(0).getHeight());
}
});


In my layout.xml

<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >

<LinearLayout
android:id="@+id/principal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@drawable/horiz"
android:orientation="vertical"
android:showDividers="end|middle" >
</LinearLayout>
</ScrollView>


When I scroll down to the last I get the getScrollY() smaller than getChildAt(0).getHeight().

How can I get both having the same total size ?

Answer

Maybe change

layout_height="wrap_content"

to

layout_height="match_parent"
Comments