Code Monkey home page Code Monkey logo

android_draggable's Introduction

Android_Draggable

This is an example of how to create a draggable icon (finger-draggable) in Android by using the onClick and OnTouchListener methods.

demo

Commands

There are two important commands in this project:

  1. The onClick listener that is triggered when the user types its name and clicks on Send.
  2. The OnTouchListener that modifies the position of the icon based on the dragging movement of the user.

onClick

The onClick method is implemented as

send = (Button) findViewById(R.id.btnSend);
send.setOnClickListener(new Button.OnClickListener(){
	@Override
	public void onClick(View view){
		sendName();
	}
});

OnTouchListener

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.

IDE

About this project:

  1. This is an Android Studio project, so in order to open it just open it in Android Studio.
  2. If you use Eclipse for Android development, just create a project in Eclipse and copy the classes in the src folder.

Emulator

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:

  1. Close both Bluestacks and Android Studio if open.
  2. 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.
  3. Open Android Studio. Bluestacks should now appear as an option when running the project.

How to force Android Studio to use Bluestacks

Sometimes after performing the steps above, Bluestacks still does not appear in the list of emulators. To solve this, do the following:

  1. Close Android Studio.
  2. 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
  1. 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.

adb

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.

Execution

A run example can be seen below.

demo

android_draggable's People

Contributors

pontazaricardo avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.