How to get where is my finger, over of which layout? - java

I have a block, I drag it somewhere then this cell transform to yellow then I exitted this cell, It transforms again gray but, When I drop it somewhere Everywhere is magenta, How can I paint only that block When I drop it, You can check pictures to understand my problem
Best regards
public boolean onDrag(View v,DragEvent event)
{
int action=event.getAction();
switch (action)
{
case DragEvent.ACTION_DRAG_STARTED:
Log.i("Script",num+"-ACTION_DRAG_STARTED");
if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN))
{
return (true);
}
return (false);
case DragEvent.ACTION_DRAG_ENTERED:
Log.i("Script",num+"-ACTION_DRAG_ENTERED");
v.setBackgroundColor(Color.YELLOW);
break;
case DragEvent.ACTION_DRAG_LOCATION:
break;
case DragEvent.ACTION_DRAG_EXITED:
v.setBackgroundColor(Color.GRAY);
break;
case DragEvent.ACTION_DROP:
View view=(View) event.getLocalState();
ViewGroup owner=(ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container=(LinearLayout) v;
container.addView(view);
view.setVisibility(View.INVISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
v.setBackgroundColor(Color.MAGENTA);
break;
}
return true;
}

In DragEvent.ACTION_DRAG_ENDED event you should find view.getX() and view.getY(). This will return x and y co-ordinate in pixels then according to your requirement you should calculate nearest view which occupy this blocks and change its color to MAGENTA.

Related

MotionEvents triggering no matter what in Android

I have this code for my MotionEvents in Android:
#Override
public boolean onTouchEvent(MotionEvent event){
int action = event.getActionMasked();
switch (action){
case MotionEvent.ACTION_UP:
mGameview.moveUp(30);
break;
case MotionEvent.ACTION_DOWN:
mGameview.moveDown(30);
break;
}
return true;
}
However, instead of these things only triggering when I drag my finger up/down, they both happen simultaneously no matter what I do in the app, for example when I tap, drag left and right.
Try to get action by:
int action = event.getAction() & MotionEvent.ACTION_MASK;

Scroll view does not scroll on first touch

I have the following code, all I want is to detect touch on a edit text and scroll the scroll view:
View.OnTouchListener buttonFocus = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("TOuch");
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println("TOuch down");
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
break;
case MotionEvent.ACTION_UP:
System.out.println("TOuch up");
((ScrollView) findViewById(R.id.m_scroll)).fullScroll(View.FOCUS_DOWN);
v.performClick();
break;
default:
break;
}
return false;
}
};
While as the keypad pops up at the start of activity the scroll does not occur, it only scroll when I touch the view for the second time. Am I doing something wrong here? Will implementing a thread help?
Returning true in that last line of yours might solve your problem.
Returning false means that the onTouch has been used completely and won't entertain any further touch events

Positioning in drag and drop layout

i have a few problem here regarding the drag and drop. I already create the code for drag and drop, its working finely. But here's the problem, as u can see in the images below each jar can fits only 4 sweets, but when i drag sweets from the second jar to the first one, it can still be drop into it. How can i make each jar can only be fixed with four sweets?
This is the coding that I've done for the drag and drop. Should I use the if else statement somewhere in this coding to solve my problem?
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
View view = (View) event.getLocalState();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// Do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
v.setBackgroundDrawable(normalShape);
break;
case DragEvent.ACTION_DRAG_EXITED:
v.setBackgroundDrawable(normalShape);
break;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) v;
container.addView(view);
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
//v.setBackgroundDrawable(normalShape);
if (dropEventNotHandled(event))
{
view.setVisibility(View.VISIBLE);
}
break;
default:
break;
}
return true;
}
private boolean dropEventNotHandled(DragEvent event) {
// TODO Auto-generated method stub
return !event.getResult();
}
}
}
inside the case DragEvent.ACTION_DRAG_ENDED block you should be checking to see if theres room in the jar and if not then return false so that the drop action fails.

Finger touch/press/drag

I know that there are listeners for finger touch etc.. but I wonder how I can decet a finger drag up/down/left/right.
Is there any kind of listener for that?or it's some math involved.
Use this
#Override
public boolean onTouchEvent(MotionEvent e) {
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
Try taking a look at the Android Developer Resources sometime.

How to handle touch or press down events in Android?

Alright so i have 4 image views..
How can i control what happens when they are pressed down and up
So say like:
one of the images is press down a textview gets changed to 1 but when they let go of that one the text will change back to 0?
You need to use an OnTouchListener and have it listen for TouchEvents on your Views.
For Example:
MyTouchListener l = new MyTouchListener();
view.setOnTouchListener(l);
Here is an example of what MyTouchListener could look like:
class MyOnTouchListener implements OnTouchListener {
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// touch down code
break;
case MotionEvent.ACTION_MOVE:
// touch move code
break;
case MotionEvent.ACTION_UP:
// touch up code
break;
}
return true;
}
}

Categories