Skip to content Skip to sidebar Skip to footer

Android - Wait For Touch Event

In my Android app, I'd like to display several images on the screen in sequence, waiting for a touch event (a single tap) to go to the next one. I saw here that one way to do this

Solution 1:

By default, event listeners in Android are for waiting - you don't have to provide any delay.

Simply set the onTouchEvent(...) listener on the ImageView and show the first bitmap. When the ImageView is touched, show the next bitmap and so on. All you have to do is keep a count of how many touches there have been in order to know which image to show (image 1, 2, 3, 4 etc).

Example...

publicclassLoadImageextendsActivity {

    intimageNumber=1;

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_load_image);

        //get an image and create a bitmap from itImageViewimageView= (ImageView) findViewById(R.id.imageView);
        imageView.setImageBitmap(bitmap);

   }

    @OverridepublicbooleanonTouchEvent(MotionEvent evt) {
        if (evt.getAction() == MotionEvent.ACTION_DOWN) {
            imageNumber++;
            switch (imageNumber) {
                case2:
                    // show image 2break;
                case3:
                    // show image 3break;
                ...
            }
            returntrue;
        }
        returnfalse;
    }
}

Post a Comment for "Android - Wait For Touch Event"