How to Scroll a Scrollview Programmatically in Android

Can I scroll a ScrollView programmatically in Android?

ScrollView sv = (ScrollView)findViewById(R.id.scrl);
sv.scrollTo(0, sv.getBottom());

or

sv.scrollTo(5, 10);

Android, How to Scroll Programmatically a scrollview When I Clicked on edittext

Thank you guys for all answers, but I solved my problem by myself,

First I would like to say you have to use "OnFocusListener" and then you have to use Handler like this;

Now, When I click my edittext; keyboard opened and scrollview scrolled to top at the same time :)

et_space.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){
final Handler handler;
handler = new Handler();

final Runnable r = new Runnable() {
public void run() {
scrollView.smoothScrollTo(0, 500);

}
};
handler.postDelayed(r, 200);
}
}
});

How to Programmatically Scroll a ScrollView to Bottom

This doesn't actually answer your question.
But it's an alternative which pretty much does the same thing.

Instead of Scrolling to the bottom of the screen, change the focus to a view which is located at the bottom of the screen.

That is, Replace:

scroll.scrollTo(0, scroll.getBottom());

with:

Footer.requestFocus();

Make sure you specify that the view, say 'Footer' is focusable.

android:focusable="true"
android:focusableInTouchMode="true"

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

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

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.

Why cannot scroll ScrollView Programmatically?

try this code:

 final ScrollView scrollView = (ScrollView) view.findViewById(R.id.scrollbar_textchat);
scrollView.post(new Runnable() {
public void run() {
scrollView.scrollTo(0, scrollView.getBottom());
}
});

use ListView: to scroll to bottom poistion

  listView.post(new Runnable(){
public void run() {
listView.setSelection(listView.getCount() - 1);
}});

How to create Scrollview programmatically?

you may create it as below:

ScrollView scroll = new ScrollView(context);
scroll.setBackgroundColor(android.R.color.transparent);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
scroll.addView(yourTableView);

Scroll horizontally in programmatically created ScrollView

The only way you can do this is by creating a horizontalscrollview to be the child of the scrollview. If you make the minor modification to your create_relativeLayout() to create a horizontalscroll view then it will work correctly. I have seen this happen in other examples.

public  HorizontalScrollView create_relativeLayout(){

HorizontalScrollView horizontalScrollView = new HorizontalScrollView(getContext());

relativeLayout = new RelativeLayout(getContext());
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
relativeLayout.setLayoutParams(relativeParams);
create_Player_Table_Layout();
horizontalScrollView.addView(relativeLayout);
return horizontalScrollView;
}


Related Topics



Leave a reply



Submit