Search code examples
javaandroidandroid-studiodelay

how to show images after a short time in android


i want to show images with delay after a button click

       final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            // Do something after 5s = 5000ms

        }
    }, 26000);

i want to use this code for this actualy,i want to show this images(on a motion layout)enter image description here

after 26s of clicking this button enter image description here


Solution

  • You already have your Handler and postDelayed method. Create the ImageView you want to show, default visibility as GONE or INVISIBLE and inside the postDelayed method, set it to VISIBLE.

    Maybe I didn't understand your question correctly, but it seems that you already answered your own question.

    Let me share some code. In your XML file, you'll have an ImageView and set the visibility property.

    <ImageView
         android:id="@+id/yourImage"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:visibility="gone" />
    

    In your Activity, you'll first find and create a reference to the ImageView and then change its visibility property after the delay.

    ImageView yourImage = findViewById(R.id.yourImage);
    
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            yourImage.setVisibility(View.VISIBLE);
        }
    }, 26000);
    

    By default, your image will not show because it's set to GONE in the XML. The handler will execute the code inside the run method after 26s, then your image visibility property will be set to VISIBLE.