How to Scroll Scrollview to Bottom Programmatically in Android

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"

How to programmatically scroll to bottom (Android, kotlin)?

Try using scroll.fullScroll

scroll.post { 
scroll.fullScroll(View.FOCUS_DOWN)
}

Android: ScrollView force to bottom

scroll.fullScroll(View.FOCUS_DOWN) also should work.

Put this in a scroll.Post(Runnable run)

Kotlin Code

scrollView.post {
scrollView.fullScroll(View.FOCUS_DOWN)
}

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);

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());
}
});
}

Scroll to bottom of layout

A simple way is to create an Action-based runnable, passing the Action as a parameter to the runnable .
You can refer to the following code:

        scrollView = FindViewById<ScrollView>(Resource.Id.scrollView1);

var runnable = new MyRunnable(async () =>
{
// Do whatever you need to do, including capturing of local vars, app/activity context, etc.
await Task.Delay(500);
scrollView.FullScroll(FocusSearchDirection.Down);
});
runnable.Run();

Code of class MyRunnable

public class MyRunnable : Java.Lang.Object, Java.Lang.IRunnable
{
readonly WeakReference<Action> actionRef;

public MyRunnable(Action action)
{
actionRef = new WeakReference<Action>(action);
}

public void Run()
{
actionRef.TryGetTarget(out Action action);
action?.Invoke();
}
}

ScrollView sliding from bottom to top, programmatically

Well, at the moment I've decided to use this other approach, but if anyone have a better suggestion, come on. I've deleted the ScrollView layout and I've only used a classical RelativeLayout with all the text I need to cycle.

In the OnCreate:

display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int display_h = display.getHeight();
pos_y = display_h;

In the onWindowsFocusChanged:

@Override
public void onWindowFocusChanged(boolean hasFocus)
{
if (hasFocus)
{

RelativeLayout r_main = (RelativeLayout)findViewById(R.id.RelativeTables);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)r_main.getLayoutParams();
params.leftMargin = 0;
params.topMargin = pos_y;
r_main.setLayoutParams(params);
mScrollHandler.removeCallbacks(mUpdateScroll);
mScrollHandler.postDelayed(mUpdateScroll, 0);

}
else
{

mScrollHandler.removeCallbacks(mUpdateScroll);

}

}

And finally in the runnable:

private Runnable mUpdateScroll = new Runnable() {
public void run() {
pos_y = pos_y - 1;
RelativeLayout r_main = (RelativeLayout)findViewById(R.id.RelativeTables);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)r_main.getLayoutParams();
params.leftMargin = 0;
params.topMargin = pos_y;
r_main.setLayoutParams(params);
mScrollHandler.postDelayed(mUpdateScroll, 100);

// the running cycle:
if (pos_y<-display.getHeight()) {
pos_y = display.getHeight();
}

}
};

It works.



Related Topics



Leave a reply



Submit