Check if MotionEvent.ACTION_UP is out of the imageview - java

The user will touch a image and it is important that if he left his finger up in that image or not
i tried with writing a onTouchListner() and after that using swich case but i don't know how to continue
image.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
}
return true;
}
});
thank's in advance

I found my answer through this link but i change it to this:
private Rect rect; // Variable rect to hold the bounds of the view
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) {
// User moved outside bounds
}
break;
case MotionEvent.ACTION_DOWN:
rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
break;
}
return false;
}

Don't forgot MotionEvent.ACTION_CANCEL

Related

Display text in logcat when finger moves past a button [duplicate]

I'm new to android and I want to know how to identify the hover view when moving finger on different views. As a example if my activity has 5 LinearLayouts A,B,C,D,E and if I moving from C to B then to E I want to identify that. Please if someone knows how to implement that can you give me an example.
Code that I have tried so far..
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
switch (v.getId()) {
case R.id.linearLayoutA:
Log.d(getClass().getSimpleName(), "entered A");
break;
case R.id.linearLayoutB:
Log.d(getClass().getSimpleName(), "entered B");
break;
case R.id.linearLayoutC:
Log.d(getClass().getSimpleName(), "entered c");
break;
case R.id.linearLayoutD:
Log.d(getClass().getSimpleName(), "entered d");
break;
case R.id.linearLayoutE:
Log.d(getClass().getSimpleName(), "entered e");
break;
}
}
return true;
}
Thank you.
Implement View.OnTouchListener in your activity
get rect area of item touch then if touch points are inside the that rectangle area then do something...
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
rect1 = new Rect(linearLayout1.getLeft(), linearLayout1.getTop(),
linearLayout1.getRight(), linearLayout1.getBottom());
rect2= new Rect(linearLayout2.getLeft(), linearLayout2.getTop(),
linearLayout2.getRight(), linearLayout2.getBottom());
}
/////////////////////////////
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getActionMasked() == (MotionEvent.ACTION_DOWN|MotionEvent.ACTION_MOVE)) {
if (rect1.contains((int) event.getX(), (int) event.getY())) {
//do something when touch or moving on linearlayout1 area
} else if (rect2.contains((int) event.getX(), (int) event.getY())) {
//do something when touch or moving on linearlayout2 area
}
}
return true;
}

Can you access a public static object of another activity from within inner class?

Let's say in ActivityA.java, we have:
public static Car mCar = new Car();
and then in ActivityB.java, we have:
horn.setOnTouchListener( myOnTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mCar.Honk();
break;
}
return true;
when it comes to "ActivityA.mCar.Honk();", will I get an annoying red underline telling me I cannot access this or that?

How to handle touch events and gesture events on screen

i want to set on touch listener and use on touch right
in On Create Method i set On Touch Listener for Main Layout
mainActivityLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
XML File:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mainActivityLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorBackground"
android:orientation="vertical"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
then override onTouchEvent, but not working
#Override
public boolean onTouchEvent(MotionEvent event) {
Toast.makeText(getBaseContext(), "On Touch", Toast.LENGTH_SHORT).show();
if (event.getAction() == *TouchRight*)
Toast.makeText(getBaseContext(), "Touch Right", Toast.LENGTH_SHORT).show();
return super.onTouchEvent(event);
}
how i can handle gesture events ?
You can use below code sample
float initialX, initialY;
#Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getActionMasked();
switch (action) {
case MotionEvent.ACTION_DOWN:
initialX = event.getX();
initialY = event.getY();
Log.d(TAG, "Action was DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.d(TAG, "Action was MOVE");
break;
case MotionEvent.ACTION_UP:
float finalX = event.getX();
float finalY = event.getY();
Log.d(TAG, "Action was UP");
if (initialX < finalX) {
Log.d(TAG, "Left to Right swipe performed");
}
if (initialX > finalX) {
Log.d(TAG, "Right to Left swipe performed");
}
if (initialY < finalY) {
Log.d(TAG, "Up to Down swipe performed");
}
if (initialY > finalY) {
Log.d(TAG, "Down to Up swipe performed");
}
break;
case MotionEvent.ACTION_CANCEL:
Log.d(TAG,"Action was CANCEL");
break;
case MotionEvent.ACTION_OUTSIDE:
Log.d(TAG, "Movement occurred outside bounds of current screen element");
break;
}
return super.onTouchEvent(event);
}
Hope that answer the question
EDIT
For your parent layout to work add below code to your parent layout like Relative layout
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"

How do you change the onTouchListener on the same animated image view?

Just like how those conveyors in some apps that you can swipe to the right to bring out (show it) and then swipe to the left to send back in, hiding it again. I tried using setOnTouchListener again on the same view with a delayed message after it's brought out, but the app receives an error and closes when you try to bring it out (show). If I am to remove the method that is supposed to make it go back in (hidden), it would be brought out just fine, but how do I make it happen that it could be sent back in and hidden through swiping to the left after it's been brought out by swiping to the right?
Here's the code I used in the activity:
ImageView conveyorWheel;
private float x1, x2;
static final int MIN_DISTANCE = 150;
RelativeLayout parentLayout;
android.os.Handler conveyorHandler;
conveyorWheel = (ImageView) findViewById(R.id.conveyerWheel);
parentLayout = (RelativeLayout) findViewById(R.id.parentLayout);
protected void onCreate(Bundle savedInstanceState) {
...
parentLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
break;
case MotionEvent.ACTION_MOVE:
x2 = event.getX();
float deltaX = x2 - x1;
if (Math.abs(deltaX) > MIN_DISTANCE & x1 < x2) {
conveyorWheel.animate()
.x(-750)
.setDuration(150)
.start();
conveyorHandler.postDelayed(new Runnable() {
#Override
public void run() {
conveyorRevert();
}
}, 151);
break;
}
default:
return false;
}
return true;
}
});
}
private void conveyorRevert (){
parentLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
break;
case MotionEvent.ACTION_MOVE:
x2 = event.getX();
float deltaX = x2 - x1;
if (Math.abs(deltaX) > MIN_DISTANCE){
conveyorWheel.animate()
.x(-950)
.setDuration(150)
.start();
break;
}
default: return false;
}
return true;
}
});
}
The Android Monitor displayed the error:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.Handler.postDelayed(java.lang.Runnable, long)' on a null object reference
But I have no idea how to fix this

Check when the first item of gridview is fully shown

I want to do something when first item of grid view is shown, and currently I'm doing this to achieve this.
if(((GridView)v).getFirstVisiblePosition() == 0)
{
...
...
}
But it runs when 1st item is slightly visible, but I want to run it when it is fully shown. Is it possible to achieve this by some mean. This code is inside touch event of gridview.
gridView.setOnTouchListener(new OnTouchListener() {
private float mInitialY;
#Override
public boolean onTouch(View v, MotionEvent event) {
//
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
mInitialY = event.getY();
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_MOVE:
final float y = event.getY();
final float yDiff = y - mInitialY;
if (yDiff > 0.0) {
//SCROLL DOWN
if(((GridView)v).getFirstVisiblePosition() == 0)
{
}
break;
} else if (yDiff < 0.0) {
// SCROLL UP
break;
}
break;
}
// Handle ListView touch events.
v.onTouchEvent(event);
return true;
}
});

Categories