This is an example of how to create a draggable icon (finger-draggable) in Android by using the onClick and OnTouchListener methods.
There are two important commands in this project:
- The onClick listener that is triggered when the user types its name and clicks on Send.
- The OnTouchListener that modifies the position of the icon based on the dragging movement of the user.
The onClick method is implemented as
send = (Button) findViewById(R.id.btnSend);
send.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View view){
sendName();
}
});
The code of the OnTouchListener is the following
private OnTouchListener imgListener = new OnTouchListener() {
private float x, y; //The x, y position of the image.
private float mx, my; // The distance from original position to finger dragging.
// For the draggable options, we can use ACTION_DOWN and ACTION_MOVE
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN: //ACTION_DOWN.
x= v.getX()-event.getRawX();
y = v.getY()-event.getRawY();
break;
case MotionEvent.ACTION_MOVE: //ACTION_MOVE.
v.animate().x(event.getRawX()+x).y(event.getRawY()+y).setDuration(0).start();
break;
}
return true;
}
};
and it is implemented as
dino.setImageResource(R.drawable.dino);
dino.setOnTouchListener(imgListener);
This function uses the MotionEvent.ACTION_DOWN and MotionEvent.ACTION_MOVE events to detect a finger tap and drag movements.
About this project:
- This is an Android Studio project, so in order to open it just open it in Android Studio.
- If you use Eclipse for Android development, just create a project in Eclipse and copy the classes in the src folder.
Normally in both Android Studio and Eclipse you can create a device emulator with Android, but most of the times is extremelly slow to load. Bluestacks is a better option to perform emulations. In order to enable Bluestacks as an emulator in Android Studio, perform the following:
- Close both Bluestacks and Android Studio if open.
- Open Bluestacks, wait for it to update if needed and let it load until it reaches the home screen where all the apps are installed.
- Open Android Studio. Bluestacks should now appear as an option when running the project.
Sometimes after performing the steps above, Bluestacks still does not appear in the list of emulators. To solve this, do the following:
- Close Android Studio.
- Open a cmd terminal and navigate to the adb.exe location. Most of the times is in
C:\Users\<Local user>\AppData\Local\Android\sdk\platform-tools
- Run the following command
adb connect localhost:5555
The previous command should return a daemon started sucessfully message. In the case you get an error like Unable to connect to localhost: No connection could be made because the target machine actively refused it just run the following command
adb connect 127.0.0.1
NOTE: For these commands to work, Bluestacks needs to be open.
If after trying the steps above Bluestacks still does not show up as a valid emulator, please read this and this sites where they explain some other alternative solutions.
A run example can be seen below.