0

I am writing a chat app that has an activity that draws chat transcript(ListView stacked from bottom) along with a Text input area below the list view. User can type their chat message in the Text input area and then this message is appended to the listview.

To "reflect" this addition, I am adding the user entered string in my ListAdapter and then calling notifyDatasetChanged() method. Although this method does the job, there are no animations which makes the entire thing look less "natural".

I wish to slide the list up gradually as the row is being added and want to know how this can be achieved.

Ryan Fulghum
  • 25
  • 1
  • 4

2 Answers2

2

Define an xml animation this will have a "slide up" effect put in res/anim/

 <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:shareInterpolator="true" >

    <translate
        android:duration="200"
        android:fromXDelta="0"
        android:fromYDelta="20%"
        android:toXDelta="0"
        android:toYDelta="0" />

</set>

Load the animation res in your adapter

Animation slideUp = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);

For the last item in your adapter's getView() method do something like this where slide up is the animation resource you load

 v.setAnimation(slideUp);
   v.startAnimation(slideUp);
Brian
  • 4,328
  • 13
  • 58
  • 103
1

If you can re-arrange to use linearlayout that continually gets textviews added, then you can use android:animateLayoutChanges="true" (not sure about scrolling with linearlayout though), source: how to add animation to the linear layout?

Alternatively you could have a ScrollView on which you add TextViews to the end and call:

final ScrollView scrollview = ((ScrollView) findViewById(R.id.scrollview));
scrollview.post(new Runnable() {
    @Override
    public void run() {
        scrollview.fullScroll(ScrollView.FOCUS_DOWN);
     }
});

Source: How to scroll to bottom in a ScrollView on activity startup

Could be flavoured with some coloring and some delay using postdelayed instead of post. To tint the background of a view in my app I use this code:

@SuppressLint("NewApi") private void tintBackground() {
    ColorDrawable[] color = { new ColorDrawable(Color.RED),
            new ColorDrawable(Color.WHITE) };
    TransitionDrawable trans = new TransitionDrawable(color);
    int sdk = android.os.Build.VERSION.SDK_INT;
    if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
        mViewPager.setBackgroundDrawable(trans);
    } else {
        mViewPager.setBackground(trans);
    }
    trans.startTransition(ANIMATION_TINTBACKGROUND);
}

If you stick with the listview, you can add:

android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"

to your listview layout, source: Listview Scroll to the end of the list after updating the list

Have not tried this last one. Sounds promising but do not know if it adds the effect you are seeking.

Community
  • 1
  • 1
cYrixmorten
  • 7,110
  • 3
  • 25
  • 33
  • All the approaches look reasonable. I am going to try the last one (since it warrants minimal code change). Thanks much! – Ryan Fulghum Feb 24 '14 at 13:46