Siddhpura Amit
Siddhpura Amit

Reputation: 15078

ImageView.Visible and ImageView.Gone is not working android

I have created code in that when I click on suppose A Image, then B Image should show for 100 Milliseconds and then goes off

I have done this by Java Code

public void changeRightDrum() {
    System.out.println("RIGHT");
    imageViewB.setVisibility(View.VISIBLE);
    try {
        Thread.sleep(100);
    } catch (Exception e) {
        e.printStackTrace();
    }
    imageViewB.setVisibility(View.GONE);
    System.out.println("RIGHT DONE");
}

But it is not working B image is not displaying

Can anybody help me how to achieve that

Upvotes: 1

Views: 690

Answers (4)

Siddhpura Amit
Siddhpura Amit

Reputation: 15078

I have achieved by using this

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

        @Override
        protected void onPreExecute() {
            imgPressedLeftTabla.setVisibility(View.VISIBLE);
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(Void result) {
            imgPressedLeftTabla.setVisibility(View.GONE);
            super.onPostExecute(result);
        }

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(50);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

    }.execute();

Upvotes: 1

Hirak Chhatbar
Hirak Chhatbar

Reputation: 3181

Implementing what @laalto said :

public void changeRightDrum() {
System.out.println("RIGHT");
imageViewB.setVisibility(View.VISIBLE);
Handler handler = new Handler();
       handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                   imageViewB.setVisibility(View.GONE);
                   System.out.println("RIGHT DONE"); 
                  }
           },200);
}

Upvotes: 1

Hemanth
Hemanth

Reputation: 2737

Use postDelayed like this.

public void changeRightDrum() {
    System.out.println("RIGHT");
    imageViewB.setVisibility(View.VISIBLE);
    imageViewB.postDelayed(new Runnable() {
      @Override
      public void run() {
        imageViewB.setVisibility(View.GONE);
        System.out.println("RIGHT DONE");
      }
    }, 100);
}

Upvotes: 2

laalto
laalto

Reputation: 152817

You're blocking the UI thread with sleep() and any UI updates cannot really be performed.

Instead of sleeping, use a Handler with postDelayed() to schedule a Runnable to run after a delay.

Upvotes: 8

Related Questions