Can I scroll ScrollView programmatically to a specific position?

How to get last scroll view position, scrollview

  • I am using TableLayout. which have 100 of items to make it scrollable I am using Tablelayout inside ScrollView. But I have to detect whether the user have scrolled to the last row. If the user have scrolled to the last view then user will be shown a Toast message. But How to know that the user has scrolled to the last row of the tablelayout. I have referred the code from http://huuah.com/using-tablelayout-on-android/w. http://huuah.com/using-tablelayout-on-android/

  • Answer:

    If new scrolled y position + scroll view height >= tableview height that means you have reached the end of list. To achieve this you have to write your custiom scrollview. Step-1 You have to create custom scroll view extending ScrollView package com.sunil; import android.content.Context; import android.util.AttributeSet; import android.widget.ScrollView; public class LDObservableScrollView extends ScrollView { private LDObservableScrollViewListener scrollViewListener = null; public LDObservableScrollView(Context context) { super(context); } public LDObservableScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public LDObservableScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public void setScrollViewListener(LDObservableScrollViewListener scrollViewListener) { this.scrollViewListener = scrollViewListener; } @Override protected void onScrollChanged(int x, int y, int oldx, int oldy) { super.onScrollChanged(x, y, oldx, oldy); if(scrollViewListener != null) { scrollViewListener.onScrollChanged(this, x, y, oldx, oldy); } } } Step-2 Create a Listener to detect scrollchanged package com.sunil; import LDObservableScrollView; public interface LDObservableScrollViewListener { void onScrollChanged(LDObservableScrollView scrollView, int x, int y, int oldx, int oldy); } Step-3 in layout xml instead of ScrollView use the custom scroll view <com.abc.LDObservableScrollView android:layout_width="fill_parent" android:layout_marginTop="1dip" android:id="@+id/OLF_ScrollView" android:layout_height="fill_parent" android:background="@drawable/small_list_back"> <TableLayout android:layout_width="fill_parent" android:id="@+id/OLF_tableLayout_TableLayout" android:layout_height="fill_parent"> </TableLayout> </com.abc.LDObservableScrollView> Step-4 In your activity class or where you want to detect the scroll event use the follwoing code public class DetectHere implements LDObservableScrollViewListener{ ... LDObservableScrollView scrollView = (LDObservableScrollView)view.findViewById(R.id.OLF_ScrollView); scrollView.setScrollViewListener(this); ..... @Override public void onScrollChanged(LDObservableScrollView scrollView, int x, int y, int oldx, int oldy) { // TODO Auto-generated method stub if((scrollView.getHeight()+y) >= tableLayout.getHeight()){ Log.e("Custom Listener ", "END OF LIST OCCURRED ::"); } }

bapi at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.