How to slide image with finger touch in android?

I am developing an android application in which I want to slide images with finger touch. I have implemented an onClickListener with which I can slide images but I don’t know how to implement finger touch functionality.

Please suggest me any method how to slide images with finger touch. Any Suggestion or any tutorial or method will be helpful.

  • Graphical Layout tab does not appear for some layout files into Eclipse
  • ExpandableListView inside ExpandableListView
  • Google Maps CameraUpdateFactory not initialized
  • Encrypt and Decrypt with AES ECB mode 'BadPaddingException' in some cases
  • Google Play Services update
  • Firebase messaging NoSuchMethodError.zzUr exception
  • Related posts:

    How to send event from Service to Activity with Otto event bus?
    Mixing Android Views and GLSurfaceView
    how to correct this error: java.lang.OutOfMemoryError
    How do I install the same Android App .apk twice on the same device?
    java.util.ConcurrentModificationException when doing Parse.initialize(this
    Change TimePicker text color
  • The correct way to draw text in OpenGL ES 2
  • How to Add an event in Google Calendar from Activity?
  • Custom seekbar android
  • Which type of url should use in android for sending request to server?
  • CustomTabs shows ERR_UNKNOWN_URL_SCHEME after 302 Redirect
  • Why defining Flowable I could receive updates of the DB
  • 5 Solutions collect form web for “How to slide image with finger touch in android?”

    You can use onTouchListner method instead of onClickListner. Below onTouchListners example is given..

    public class abc extends Activity implements OnTouchListener 
    {
         ImageView img;
         protected void onCreate(Bundle savedInstanceState) 
         {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.one);
    
                    img = (ImageView) findViewById(R.id.imageView1);
                    img.setOnTouchListener(this);
         }
    
            public boolean onTouch(View v, MotionEvent event) 
        {
            switch (event.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                {       
                      // Here u can write code which is executed after the user touch on the screen 
                         break; 
                }
                case MotionEvent.ACTION_UP:
                {             
                       // Here u can write code which is executed after the user release the touch on the screen    
                     break;
                }
                case MotionEvent.ACTION_MOVE:
                {  
                   // Here u can write code which is executed when user move the finger on the screen   
                    break;
                }
            }
            return true;
        }
    

    What you are looking for is an ViewFlipper. This will help you to get the look what you are expecting.

    This mgiht be helpful

    check this too

    Or try this,

    In your xml just add only this,

    <ViewFlipper android:id="@+id/flipper" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    </ViewFlipper>
    

    need not enclose with any ImageViews.

    Now do this in your coding.

    Considering that you have stored your Images in a array like this,

    int gallery_grid_Images[]={R.drawable.gallery_image_1,R.drawable.gallery_image_2,R.drawable.gallery_image_3,
            R.drawable.gallery_image_4,R.drawable.gallery_image_5,R.drawable.gallery_image_6,R.drawable.gallery_image_7,
            R.drawable.gallery_image_8,R.drawable.gallery_image_9,R.drawable.gallery_image_10
            };
    

    Now in your onCreate(),

    viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
     for(int i=0;i<gallery_grid_Images.length;i++)
            {
            //  This will create dynamic image view and add them to ViewFlipper
                setFlipperImage(gallery_grid_Images[i]);
            }
    

    And now add this method to your activity,

    private void setFlipperImage(int res) {
        Log.i("Set Filpper Called", res+"");
        ImageView image = new ImageView(getApplicationContext());
        image.setBackgroundResource(res);
        viewFlipper.addView(image);
    }
    

    That’s it. And now using your viewFlipper.showNext(); and viewFlipper.showPrevious();
    methods you can show your dynamic images.

    I would really suggest you using RealViewSwitcher class by Marc Reichelt, get it from here It provides a slight view swticher, like android launcher does.

    Views can catch touch events — take a look at OnTouchEvent which lets you catch MotionEvents like touch down and touch up. You could use a handler to loop, moving the image/view to the currently touched place after a touch down event until a touch up event.

    You can use the Gallery widget

    Here the link to the API

    There is also a tutorial. For me it was very helpful 🙂

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.