Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animation when changing textview

I currently use a major workaround and have two activities switching each time I change the text on a TextView. I am using this code:

Weeklytext.this.overridePendingTransition(                      R.anim.slide_in_left,                      R.anim.slide_out_right             ); 

Is it possible to do this in one Activity? It's kind of annoying having two Activities with the exact same content just so that I can use animations ;)

Thanks! Please ask if you don't understand my Question!

like image 378
Juan Henderson Avatar asked Sep 27 '11 18:09

Juan Henderson


People also ask

How do you animate TextView?

To start the animation we need to call the startAnimation() function on the UI element as shown in the snippet below: sampleTextView. startAnimation(animation); Here we perform the animation on a textview component by passing the type of Animation as the parameter.

What is transition animation in android?

Android's transition framework allows you to animate all kinds of motion in your UI by simply providing the starting layout and the ending layout.

What is TextSwitcher?

In Android, TextSwitcher is a specialized ViewSwitcher that contains only children of type TextView. TextSwitcher is available in Android from version Android 1.6+. A TextSwitcher is useful to animate a label(i.e. text) on screen. It is an element of transition widget which helps us to add transitions on the labels.

How do I change an animation layout?

All you need to do is set an attribute in the layout to tell the Android system to animate these layout changes, and system-default animations are carried out for you. Tip: If you want to supply custom layout animations, create a LayoutTransition object and supply it to the layout with the setLayoutTransition() method.


1 Answers

You can use a TextSwitcher to have animations when changing the text in a TextView.

A TextSwitcher is just a special kind of ViewSwitcher, and as such, it lets you provide two Views from which to animate between. When you call setText(), it updates the text of the next TextView and then animates that one into the screen, and the current one out. The old TextView is then designated as the 'next' TextView and the process repeats.

You can specify the Views using setFactory(...) or just simply add two TextViews to it with addView(...).

// get a TextSwitcher view; instantiate in code or resolve from a layout/XML TextSwitcher textSwitcher = new TextSwitcher(context);  // specify the in/out animations you wish to use textSwitcher.setInAnimation(context, R.anim.slide_in_left); textSwitcher.setOutAnimation(context, R.anim.slide_out_right);  // provide two TextViews for the TextSwitcher to use // you can apply styles to these Views before adding textSwitcher.addView(new TextView(context)); textSwitcher.addView(new TextView(context));  // you are now ready to use the TextSwitcher // it will animate between calls to setText textSwitcher.setText("hello"); ... textSwitcher.setText("goodbye"); 
like image 167
antonyt Avatar answered Oct 05 '22 13:10

antonyt