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
Android Force Gpu Rendering How to Enable and Disable
How to Completely Rename an Android Application
Check If Json Object Contains Specific Value
Convert File: Uri to File in Android
How to Select a Video from the Gallery and Get It's Real Path
How to Restrict Entry of Numbers But Allow All the Special Characters and Alphabets in Edittext
Android Kotlin Call a Function in Other Activity
Converting Edittext to Int - Android
Android Fastboot Devices Not Returning Device
Method to Refresh Fragment Content When Data Changed ( Like Recall Oncreateview)
Changing Cardview Shadow Color
Illegalstateexception: Link Does Not Have a Navcontroller Set
How to Hide Action Bar for Fragment
Android:Strike Out Text With Bold or Thicker Line Than Default Strike_Thru_Text_Flag
Retrofit @Body With @Multipart Having Issue