Noaman Akram
Noaman Akram

Reputation: 3830

How to hide imageview after some intervals android

I am working on an android app in which i want to hide my image view after some interval. i am using this code but it is not hiding. can anybody tell me how i can hide it ???

showtrue1.setBackgroundResource(R.drawable.ticktrue);
showtrue1.setVisibility(View.VISIBLE);

try {
    Thread.sleep(1000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

showtrue1.setVisibility(View.GONE);

Upvotes: 0

Views: 1292

Answers (3)

jyomin
jyomin

Reputation: 1967

You can use async Task also to solve your problem . In its backgound function make a thread sleep for particular second and in the post method make trhe visibility gone for your image viiew.

Call the execute method in your oncreate new MyAsyncTask().execute();

and make an inner class as defined below:

private class MyAsyncTask extends AsyncTask<Void, Void, Void>{
          @Override
          protected void onPreExecute(){
                // show your progress dialog
              showtrue1.setBackgroundResource(R.drawable.ticktrue);
              showtrue1.setVisibility(View.VISIBLE);
          }
          @Override
          protected Void doInBackground(Void... voids){

              try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            return null;
          }

          @Override
          protected void onPostExecute(Void params)
          {
              showtrue1.setVisibility(View.GONE);

       }

    }

Upvotes: 1

Jitender Dev
Jitender Dev

Reputation: 6925

Create a separate thread that sleeps for 1 seconds then call runOnUiThread to hide the view.

Thread thread = new Thread() {
    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // Do some stuff
               showtrue1.setVisibility(View.GONE);
            }
        });
    }
};

Upvotes: 0

Paresh Mayani
Paresh Mayani

Reputation: 128428

Try CountDownTimer:

new CountDownTimer(1000, 100) {

     public void onTick(long millisUntilFinished) {
        // implement whatever you want for every tick
     }

     public void onFinish() {
         showtrue1.setVisibility(View.GONE);
     }
  }.start();

Upvotes: 4

Related Questions