What I'm trying to achieve is automatically scrolling to the bottom of a scrollview when a new view is added via:
mContainerView.addView(newView);
This is the code I have currently:
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String input = et.getText().toString();
if (input != null && input.length() > 0) {
findViewById(android.R.id.empty).setVisibility(View.GONE);
addItem(input);
et.setText(null);
ScrollToBottom();
}
}
});
private void ScrollToBottom() {
mScrollView.postDelayed(new Runnable() {
@Override
public void run() {
mScrollView.smoothScrollTo(0, mScrollView.getBottom());
}
}, 300);
}
private void addItem(String name) {
final ViewGroup newView = (ViewGroup) LayoutInflater.from(this).inflate(
R.layout.list_item_example, mContainerView, false);
((TextView) newView.findViewById(android.R.id.text1)).setText(name);
newView.findViewById(R.id.delete_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mContainerView.removeView(newView);
if (mContainerView.getChildCount() == 0) {
findViewById(android.R.id.empty).setVisibility(View.VISIBLE);
}
}
});
mContainerView.addView(newView);
}
The reason I'm using postDelayed is because without the delay, the new view is not drawn quick enough and I end up scrolling to the bottom minus the height of the new entry. I know this to be the case because if I drop the delay time to, for example, 100ms, the scroll will end up finishing about a third of the way down the new entry. After trial and error with different values 300ms was the number that got me the desired result.
Is there a better way of achieving this? - At present my method works but it does not seem the cleanest option.
I'm still relatively new to Android/Java programming so I;m sure I might have missed something somewhere...
I should mention I have also tried fullScroll(View.FOCUS_DOWN) also, without any change in behaviour. I swapped it out for the smoothScroll option because I preferred the smoother animation