Want to drop a View onto another View in android - java

Hey Friends help me to achieve the objective. I am newbie to the android development and the only thing I want to do is that I am trying to drag a button and drop it onto the other View(i.e RelativeLayout) the very first thing I have done the code for the drag.
rootView = inflater.inflate(R.layout.fragment_game_map,container,false);
button1 = (Button) rootView.findViewById(R.id.button1);
button1.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item((CharSequence)v.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData(v.getTag().toString(),mimeTypes, item);
View.DragShadowBuilder myShadow = new View.DragShadowBuilder(button1);
v.startDrag(dragData,myShadow,null,0);
return true;
}
});
button1.setOnDragListener(new View.OnDragListener() {
#Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
Log.d(TAG, "Action is DragEvent.ACTION_DRAG_STARTED");
// Do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.d(TAG, "Action is DragEvent.ACTION_DRAG_ENTERED");
int x_cord = (int) event.getX();
int y_cord = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.d(TAG, "Action is DragEvent.ACTION_DRAG_EXITED");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
layoutParams.leftMargin = x_cord;
layoutParams.topMargin = y_cord;
v.setLayoutParams(layoutParams);
break;
case DragEvent.ACTION_DRAG_LOCATION:
Log.d(TAG, "Action is DragEvent.ACTION_DRAG_LOCATION");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_ENDED:
Log.d(TAG, "Action is DragEvent.ACTION_DRAG_ENDED");
// Do nothing
break;
case DragEvent.ACTION_DROP:
Log.d(TAG, "ACTION_DROP event");
// Do nothing
break;
default:
break;
}
return true;
}
});
button1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(button1);
button1.startDrag(data, shadowBuilder, button1, 0);
button1.setVisibility(View.INVISIBLE);
return true;
}
else
{
return false;
}
}
});
return rootView;
}
but the second thing I don't have Idea to do.. that is I want to drop it onto the RelativeLayout but the RelativeLayout can only allow one button to be drop on it.

You missing the point of DragLiatenr. DragListenr listen to event of dragging, and handle them properly, so if your RelativeLayout not register to DragEvent he can't handle them.
Actually for what you asking you just need to listen for ACTION_DROP, and return true for all the others events.
Your could should like something like:
final RelativeLayout container; //You need to initialize your Relativelayout here
container.setOnDragListener(new View.OnDragListener() {
#Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DROP:
Log.d(TAG, "ACTION_DROP event");
((ViewGroup)v.getParent()).removeView(v); //you must first remove view from it former container
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
container.addView(v, params); // of course you need to modify params for your need
return true;
break;
default:
return true;
}
}
});
Also why you put onTouchListenr you never use?

Related

Rotating an image with onTouchListener without scaling it

I have got this simple code to rotate an image with onTouchListener, but it seems to be zooming in and out on the image as well. I am really new to using onTouchListener. Could someone tell me what is happening, or how to fix this. Anything helps thanks:)
public class MainActivity extends Activity implements OnTouchListener {
private ImageView dialer;
private float y=0;
public boolean onTouch(View v, MotionEvent event) {
double r=Math.atan2(event.getX()-dialer.getWidth()/2, dialer.getHeight()/2-event.getY());
int rotation=(int)Math.toDegrees(r);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
//x=event.getX();
y=event.getY();
updateRotation(rotation);
break;
case MotionEvent.ACTION_UP:
break;
}//switch
return true;
}//onTouch
private void updateRotation(double rot){
float newRot= new Float(rot);
Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.round_button_big);
Matrix matrix= new Matrix();
matrix.postRotate(newRot,bitmap.getWidth()/2,bitmap.getHeight()/2);
if(y>250){
Bitmap reDrawnBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
dialer.setImageBitmap(reDrawnBitmap);
}
else{
Bitmap reDrawnBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
dialer.setImageBitmap(reDrawnBitmap);
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialer = (ImageView) findViewById(R.id.image);
dialer.setOnTouchListener(this);
}//onCreate
}
Try to use this code on touch event:
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// reset the touched quadrants
for (int i = 0; i < quadrantTouched.length; i++) {
quadrantTouched[i] = false;
}
allowRotating = false;
startAngle = getAngle(event.getX(), event.getY());
break;
case MotionEvent.ACTION_MOVE:
double currentAngle = getAngle(event.getX(), event.getY());
rotateDialer((float) (startAngle - currentAngle));
startAngle = currentAngle;
break;
case MotionEvent.ACTION_UP:
allowRotating = true;
break;
}
// set the touched quadrant to true
quadrantTouched[getQuadrant(event.getX() - (dialerWidth / 2), dialerHeight - event.getY() - (dialerHeight / 2))] = true;
detector.onTouchEvent(event);
return true;
And here you can find an example :http://code.tutsplus.com/tutorials/android-sdk-creating-a-rotating-dialer--mobile-8868

How to find where to place item in linearlayout with drag and drop?

I have a linearlayout filled with programmatically created views. I have implemented drag and drop code (shown below) so that if I touch down on the right side of any of the views, they start in drag mode. The drag mode itself works fine but the problem I am having is finding out where to place the view on release so that it falls in the expected place. Thanks for the help in advance!
Here is the onTouchMethod of the view(s):
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (Math.round(event.getX()) >= 720) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(data, shadowBuilder, v, 0);
v.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
default:
return false;
}
}
});
and here is the onDragMethod that is also placed on the views:
view.setOnDragListener(new View.OnDragListener() {
#Override
public boolean onDrag(View v, DragEvent event) {
View view = (View) event.getLocalState();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DROP:
LinearLayout owner = (LinearLayout) view.getParent();
owner.removeView(view);
owner.addView(view, getNewViewPosition(fillView.indexOfChild(v), Math.round(v.getY()), Math.round(event.getY())));
view.setVisibility(View.VISIBLE);
break;
}
return true;
}
});
finally here is the getNewViewPosition method:
public int getNewViewPosition(int currentPosition, int stationaryY, int floatingY) {
Toast.makeText(this, stationaryY + " and then " + floatingY, Toast.LENGTH_SHORT).show();
if (floatingY - stationaryY <= 15) {
return currentPosition - 1;
} else {
return currentPosition + 1;
}
}
I figured it out, since I knew that my views had a height of 52 I was able to formulate this code:
public int getNewViewPosition(int currentPosition, int stationaryY, int floatingY) {
if (floatingY - stationaryY >= 13) {
return currentPosition - 1;
} else if (floatingY - stationaryY <= 39) {
return currentPosition + 1;
} else
return currentPosition;
}

Button with onTouch and onClick

I have a button for which I am using onTouch so that I can move it around. But the button is now failing to load the contents in onClick. How to make it happen ?
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//some code which dosent work
}
#Override public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = params.x;
initialY = params.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_UP:
Voice v1 = new Voice();
v1.putVoice();
return true;
case MotionEvent.ACTION_MOVE:
params.x = initialX + (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(chatHead, params);
return true;
}
return false;
}
I finally found out that if we differentiate with ACITON_MOVE position we can get a click event.
Here is the snippet which did the trick -
case MotionEvent.ACTION_UP:
if(Math.abs(event.getRawX()-initialTouchX )<=2){
floatingFaceBubble.performClick();
return false;
}

how to programmatically move a mapView (osmdroid)

I have a LinearLayout with a mapView, zoomIn button, zoomOut button.
the app receives touchevents and it is supposed to manage the LinearLayout children with the given information (X coordinate, Y coordinate , type of touch event)
I am able to zoomIn and ZoomOut with the following code, but I am not able to move the map:
#Override
public boolean onTouch(View arg0, MotionEvent event) {
int action = event.getAction();
switch(action)
{
case MotionEvent.ACTION_DOWN:
savedTouchedX = event.getX();
savedTouchedY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
doPanning(event, mapView);
break;
case MotionEvent.ACTION_UP:
doPanning(event, mapView);
savedTouchedX = -1;
savedTouchedY = -1;
break;
default:
break;
}
return true;
}
});
//move mapView
private boolean doPanning(MotionEvent e, MapView mapView)
{
if(savedTouchedX >= 0 && savedTouchedY >= 0)
{
IGeoPoint mapCenter = mapView.getMapCenter();
GeoPoint panToCenter = new GeoPoint((int)(mapCenter.getLatitudeE6() + (e.getY() - savedTouchedY) * 1E5),(int)(mapCenter.getLongitudeE6() - (e.getX() - savedTouchedX) * 1E5));
mapView.getController().setCenter(panToCenter);
}
savedTouchedX = e.getX();
savedTouchedY = e.getY();
return true;
}
....
public void lookForButton(String msg){
String[] strArray = msg.split(" ");
final MotionEvent m;
int x=Integer.valueOf(strArray[1]);
int y=Integer.valueOf(strArray[2]);
int type=Integer.valueOf(strArray[3]);
switch (type){
case 2:
m = MotionEvent.obtain(226707,226707,0/*ACTION_DOWN*/,x,y,0);
runOnUiThread(new Runnable() {
public void run() {
memecontentView.dispatchTouchEvent(m);
}
});
break;
case 3:
m = MotionEvent.obtain(226707,226707,1/*ACTION_UP*/,x,y,0);
runOnUiThread(new Runnable() {
public void run() {
memecontentView.dispatchTouchEvent(m);
}
});
break;
case 5:
m = MotionEvent.obtain(226707,226707,2/*ACTION_MOVE*/,x,y,0);
runOnUiThread(new Runnable() {
public void run() {
memecontentView.dispatchTouchEvent(m);
}
});
break;
}
}
public void ZoomInButton(View v){
mapView.getController().zoomIn();
}
public void ZoomOutButton(View v){
mapView.getController().zoomOut();
}
How can I programmatically move mapview with the given information (X coordinate, Y coordinate , type of touch event)?
You can call mapView.scrollTo(x, y).
Another possibility is mapView.getController().animateTo() (with animation) or mapView.getController().setCenter() (without animation).
It takes an IGeoPoint as a parameter which you can instantiate with your coordinates:
new GeoPoint(latitude, longitude)

Android OnTouchListener interferes OnClickListener

I have got an Activity with a function, where I can create a new ImageView.
I would like to move the ImageView, so implemented a new OnTouchListener. That is working great, but I would also like to add a LongClickListener and here is my problem:
My LongClickListener starts one time when I am moving the ImageView.
What can I do to fix this?
public ImageView neuesDefaultBild(int x, int y, int id){
ImageView iv=new ImageView(this);
iv.setImageResource(R.drawable.usericon);
iv.setId(id);
iv.setX(x);
iv.setY(y);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.width=180;
params.height=130;
iv.setLayoutParams(params);
iv.setLongClickable(true);
iv.setFocusable(true);
iv.setFocusableInTouchMode(true);
iv.setEnabled(true);
iv.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
boolean defaultResult = v.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
//Get the coords from the Event
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
v.setX(x_cord-90);
v.setY(y_cord-130);
return true;
default:
return defaultResult;
}
return false;
}
});
iv.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
Toast.makeText(getApplicationContext(), "Long!!", Toast.LENGTH_SHORT).show();
return false;
}
});
return iv;
}
OnclickListener can be set using OnTouchListener itself
Just set a flag
private int boolean onClick;
switch (event.getAction())
{
case MotionEvent. ACTION_DOWN:
{
onClick = true;
break ;
}
case MotionEvent. ACTION_MOVE:
{
onClick = false;
break ;
}
case MotionEvent. ACTION_UP:
{
if(onClick)
{
//Call your own click listener
}
break;
}
}

Categories

Resources