Ari M
Ari M

Reputation: 1426

Android: How to make a TextView visible for a defined time at runtime

I have a Layout with a TextView, and I want to make the text or the View itself to be presented for a defined time at run time. How can I do that?

I tried with Animation: I've placed the TextView inside an Animation tag in the main.xml, but when i use:

animation = AnimationUtils.loadAnimation(this, R.id.msg_anim);

and later:

animation.startNow();

I get an exception.

So How to make the text or the TextView visible for a second?

Thank You.

Upvotes: 0

Views: 1229

Answers (3)

you786
you786

Reputation: 3550

You might be getting the exception because you are trying to start the animation in onCreate().

As per the documentation, the recommended place to start animations is in onResume(). If that's not the problem, post some more information about your Exception (like the stack trace)

Upvotes: 0

DeeV
DeeV

Reputation: 36035

You can use handlers to handle timed UI elements during runtime.

TextView myTV;
Handler uiHandler = new Handler();
Runnable makeTextGone = new Runable(){
   @Override
   public void run(){
      myTv.setVisibility(View.GONE);
   }
};

@Override
public void onCreate(Bundle icicle){
   .... code ....
   myTv = (TextView) findViewById(R.id.myTextView);
   uiHandler.postDelayed(makeTextGone, 1000);
   ... code ...
}

The call to uiHandler.postDelayed(makeTextGone, 1000); will call the runnable only once after one second. Put it immediately after you make the text visible and it should disappear after a second.

Upvotes: 2

Codeman
Codeman

Reputation: 12375

You need to make sure that your activity is instantiated before you call any animations or resources.

Make sure you are calling this AFTER super.onCreate();

Hope this helped.

Upvotes: 0

Related Questions