Skip to content Skip to sidebar Skip to footer

How To Display A Textview For Few Seconds And Then Make It Invisible?

this website is the best, its help me a lot ...I am a beginner in creating android applecation . this is my first time I ask a question here.. my question is how to display a textv

Solution 1:

Using AsyncTask:

new AsyncTask<Void, Void, Void>()
{

  protectedVoid doInBackground(Void... params)
  {
    Thread.sleep(5000);                   // sleep 5 seconds
  }

  protectedvoid onPostExecute (Void result)
  {
    // fade out view nicely
    AlphaAnimation alphaAnim = new AlphaAnimation(1.0f,0.0f);
    alphaAnim.setDuration(400);
    alphaAnim.setAnimationListener(new AnimationListener()
    {
     publicvoid onAnimationEnd(Animation animation)
     {
       // make invisible when animation completes, you could also remove the view from the layout
       myTextView.setVisibility(View.INVISIBLE);
     }
    });

    myTextView.startAnimation(alphaAnim);


  }
}.execute();

Or, better yet, just use animation:

EDITED (thanks @pkk for suggestion):

// fade out view nicely after 5 secondsAlphaAnimationalphaAnim=newAlphaAnimation(1.0f,0.0f);
alphaAnim.setStartOffset(5000);                        // start in 5 seconds
alphaAnim.setDuration(400);
alphaAnim.setAnimationListener(newAnimationListener()
{
 publicvoidonAnimationEnd(Animation animation)
 {
   // make invisible when animation completes, you could also remove the view from the layout
   myTextView.setVisibility(View.INVISIBLE);
 }
});

myTextView.setAnimation(alphaAnim);

Solution 2:

One way would be to use a CountDownTimer if you want the timer to show

publicvoidonCreate(...){
...
timer = newMyCountDown(5000, 1000);
}

privateclassMyCountDownextendsCountDownTimer
{
    long duration, interval;
    publicMyCountDown(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        // TODO Auto-generated constructor stub
        start();
    }

    @OverridepublicvoidonFinish() {
        textView1.setVisibility(View.INVISIBLE);    
    }

    @OverridepublicvoidonTick(long duration) { 
             // could set text for a timer here     
    }   
}

You also could use a TimerTask.

Here is a SO answer with a good example of TimerTask

There are also various other ways. You can search through the Docs or SO to decide which best suits your needs

Edit with TimerTask example

Timer t = newTimer(false);
  t.schedule(newTimerTask() {
  @Overridepublicvoidrun() {
       runOnUiThread(newRunnable() {
            publicvoidrun() {
                txt.setVisibility(View.INVISIBLE);
            }
        });
    }
}, 5000);

Solution 3:

finalTextViewtextView=newTextView(this);
        // add textView on some Layout
        textView.setText("Text or smth. from resource");        
        CountDownTimertimer=newCountDownTimer(5000, 1000) {

                        @OverridepublicvoidonTick(long millisUntilFinished) {
                        }

                        @OverridepublicvoidonFinish() {
                            textView .setVisibility(View.INVISIBLE); //(or GONE)
                            }
                    }.start();

Solution 4:

You can use the Handler with runnable to do this. Like i am doing for according my need.

I have a Activity1 from where i am going to Activity2 to update profile after updating profile i have to show a "Update Successfully" text in the Activity1 so i am creating a flag "PERSONAL_DETAIL_UPDATE" in the AppConstants and setting it "true" when profile update success in Activity2 and calling the Activity1 from there to show updated profile to user.

Now in the Activity2 using this code to i am showing "Update Successfully" text for 3 seconds and after 3 seconds making it gone. I am making the visibility of TextView gone by default in xml for this code you can modify according to your need.

enter code here



 if (AppConstants.PERSONAL_DETAIL_UPDATE) {
        personalDetailUpdateSuccessTV.setVisibility(View.VISIBLE);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                personalDetailUpdateSuccessTV.setVisibility(View.GONE);
            }
        }, 3000);
    }

Solution 5:

You can use a Handler and a Runnable to do this.

publicclassMainActivityextendsActivity {
privateHandler h;
TextView txt;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txt=(TextView)findViewById(R.id.txt);
    Runnable r=newRunnable(){

        @Overridepublicvoidrun() {
            // TODO Auto-generated method stubLog.e("bf", "fd");
            h.postDelayed(this, 500);
            if (txt.isShown()){
                txt.setVisibility(View.INVISIBLE);

            }
            else{
                txt.setVisibility(View.VISIBLE);
            }
        }

    };
    h=newHandler();
    h.post(r);
}
 }

Post a Comment for "How To Display A Textview For Few Seconds And Then Make It Invisible?"