Skip to main content
further enhance the extension to check whether it is already at the bottom before scrolling
Source Link

scroll.fullScroll(View.FOCUS_DOWN) will lead to the change of focus. That will bring some strange behavior when there are more than one focusable views, e.g two EditText. There is another way for this question.

 View lastChild = scrollLayout.getChildAt(scrollLayout.getChildCount() - 1); int bottom = lastChild.getBottom() + scrollLayout.getPaddingBottom(); int sy = scrollLayout.getScrollY(); int sh = scrollLayout.getHeight(); int delta = bottom - (sy + sh); scrollLayout.smoothScrollBy(0, delta); 

This works well.

Kotlin Extension

fun ScrollView.scrollToBottom() { val lastChild = getChildAt(childCount - 1) val bottom = lastChild.bottom + paddingBottom val delta = bottom - (scrollY+ height) smoothScrollBy(0, delta) } 

Or if you want to further enhance to check whether it is already at the bottom before scrolling, you can do something like this:

 fun ScrollView.scrollToBottom() { val lastChild = children.lastOrNull() ?: return val bottom = lastChild.bottom + paddingBottom val currentY = height + scrollY val alreadyAtBottom = bottom <= currentY if (!alreadyAtBottom) { val delta = bottom - currentY smoothScrollBy(0, delta) } else { // already at bottom, do nothing } } 

scroll.fullScroll(View.FOCUS_DOWN) will lead to the change of focus. That will bring some strange behavior when there are more than one focusable views, e.g two EditText. There is another way for this question.

 View lastChild = scrollLayout.getChildAt(scrollLayout.getChildCount() - 1); int bottom = lastChild.getBottom() + scrollLayout.getPaddingBottom(); int sy = scrollLayout.getScrollY(); int sh = scrollLayout.getHeight(); int delta = bottom - (sy + sh); scrollLayout.smoothScrollBy(0, delta); 

This works well.

Kotlin Extension

fun ScrollView.scrollToBottom() { val lastChild = getChildAt(childCount - 1) val bottom = lastChild.bottom + paddingBottom val delta = bottom - (scrollY+ height) smoothScrollBy(0, delta) } 

scroll.fullScroll(View.FOCUS_DOWN) will lead to the change of focus. That will bring some strange behavior when there are more than one focusable views, e.g two EditText. There is another way for this question.

 View lastChild = scrollLayout.getChildAt(scrollLayout.getChildCount() - 1); int bottom = lastChild.getBottom() + scrollLayout.getPaddingBottom(); int sy = scrollLayout.getScrollY(); int sh = scrollLayout.getHeight(); int delta = bottom - (sy + sh); scrollLayout.smoothScrollBy(0, delta); 

This works well.

Kotlin Extension

fun ScrollView.scrollToBottom() { val lastChild = getChildAt(childCount - 1) val bottom = lastChild.bottom + paddingBottom val delta = bottom - (scrollY+ height) smoothScrollBy(0, delta) } 

Or if you want to further enhance to check whether it is already at the bottom before scrolling, you can do something like this:

 fun ScrollView.scrollToBottom() { val lastChild = children.lastOrNull() ?: return val bottom = lastChild.bottom + paddingBottom val currentY = height + scrollY val alreadyAtBottom = bottom <= currentY if (!alreadyAtBottom) { val delta = bottom - currentY smoothScrollBy(0, delta) } else { // already at bottom, do nothing } } 

scroll.fullScroll(View.FOCUS_DOWN) will lead to the change of focus. That will bring some strange behavior when there are more than one focusable views, e.g two EditText. There is another way for this question.

 View lastChild = scrollLayout.getChildAt(scrollLayout.getChildCount() - 1); int bottom = lastChild.getBottom() + scrollLayout.getPaddingBottom(); int sy = scrollLayout.getScrollY(); int sh = scrollLayout.getHeight(); int delta = bottom - (sy + sh); scrollLayout.smoothScrollBy(0, delta); 

This works well.

Kotlin Extension

fun ScrollView.scrollToBottom() { val lastChild = getChildAt(childCount - 1) val bottom = lastChild.bottom + paddingBottom val delta = bottom - (scrollY+ height) smoothScrollBy(0, delta) } 

scroll.fullScroll(View.FOCUS_DOWN) will lead to the change of focus. That will bring some strange behavior when there are more than one focusable views, e.g two EditText. There is another way for this question.

 View lastChild = scrollLayout.getChildAt(scrollLayout.getChildCount() - 1); int bottom = lastChild.getBottom() + scrollLayout.getPaddingBottom(); int sy = scrollLayout.getScrollY(); int sh = scrollLayout.getHeight(); int delta = bottom - (sy + sh); scrollLayout.smoothScrollBy(0, delta); 

This works well.

scroll.fullScroll(View.FOCUS_DOWN) will lead to the change of focus. That will bring some strange behavior when there are more than one focusable views, e.g two EditText. There is another way for this question.

 View lastChild = scrollLayout.getChildAt(scrollLayout.getChildCount() - 1); int bottom = lastChild.getBottom() + scrollLayout.getPaddingBottom(); int sy = scrollLayout.getScrollY(); int sh = scrollLayout.getHeight(); int delta = bottom - (sy + sh); scrollLayout.smoothScrollBy(0, delta); 

This works well.

Kotlin Extension

fun ScrollView.scrollToBottom() { val lastChild = getChildAt(childCount - 1) val bottom = lastChild.bottom + paddingBottom val delta = bottom - (scrollY+ height) smoothScrollBy(0, delta) } 
Source Link
peacepassion
  • 2k
  • 2
  • 16
  • 19

scroll.fullScroll(View.FOCUS_DOWN) will lead to the change of focus. That will bring some strange behavior when there are more than one focusable views, e.g two EditText. There is another way for this question.

 View lastChild = scrollLayout.getChildAt(scrollLayout.getChildCount() - 1); int bottom = lastChild.getBottom() + scrollLayout.getPaddingBottom(); int sy = scrollLayout.getScrollY(); int sh = scrollLayout.getHeight(); int delta = bottom - (sy + sh); scrollLayout.smoothScrollBy(0, delta); 

This works well.