Using OnLongTouch to move image to string - java

I'm trying to use this code to set an "ontouchlistener". It says there are no errors in the code but when I try to run it I get a force quit... what is going wrong? any ideas?
final Handler handler = new Handler();
Runnable mLongPressed = new Runnable() {
public void run() {
}
};
#Override
public boolean onTouchEvent(MotionEvent event, View v){
if(event.getAction() == MotionEvent.ACTION_DOWN)
handler.postDelayed(mLongPressed, 1000);
if((event.getAction() == MotionEvent.ACTION_MOVE)||(event.getAction() ==
MotionEvent.ACTION_UP))
handler.removeCallbacks(mLongPressed);
return false;
}

In OnTouchListener the event function is:
public abstract boolean onTouch (View v, MotionEvent event)
which is called when a touch event is dispatched to a view. But you are using this one?:
public boolean onTouchEvent(MotionEvent event, View v)
To implement the listener, we can easily do this:
myImgView.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
// put your code here
return false;
}
});

Related

How to check for hold click Android studio/java

I want to make in my app when I hold click it does something and when I release the button it does something else. A variable will change to true when I hold and false when I release. How do I do that
(Android Studio)
Thanks
You have to use TouchListener and MotionEvent class. The MotionEvent class has Fields ACTION_UP, ACTION_DOWN, and ACTION_MOVE. You can use the getAction() method of the MotionEvent class.
Something like this
findViewById(R.id.btn).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// button pressed
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// button relased.
}
return false;
}
});
You can use the onTouchListener for your use case:
boolean isClicking = false;
myView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// the view is pressed
isClicking = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// the view is released.
isClicking = true;
}
return false;
}
});

Hiding Toolbar with Swipe Actions

I am trying to use an animation for that hides and shows a toolbar by detecting swiping gestures.It works but the issue is that when the toolbar is there(visible) it will repeat if you swipe up again and again and again. How can I prevent this from happening? I've try using variable to count each step then reset at the ending but that does not work either. Helping me past this would save a lot more time I've been stuck here for 2 weeks now.
CoreActivity.java
//Swipe Events WebSwipe
WebSwipe = new Swipe(350, 700);
WebSwipe.setListener(new SwipeListener() {
#Override
public void onSwipingLeft(final MotionEvent event) {
}
#Override
public void onSwipedLeft(final MotionEvent event) {
}
#Override
public void onSwipingRight(final MotionEvent event) {
}
#Override
public void onSwipedRight(final MotionEvent event) {
}
#Override
public void onSwipingUp(final MotionEvent event) {
}
#Override
public void onSwipedUp(final MotionEvent event) {
Animation ToolbarGone = AnimationUtils.loadAnimation(CoreActivity.this, R.anim.m_toolbar_gone);
m_Toolbar.startAnimation(ToolbarGone);
}
#Override
public void onSwipingDown(final MotionEvent event) {
}
#Override
public void onSwipedDown(final MotionEvent event) {
Animation ToolbarVisible = AnimationUtils.loadAnimation(CoreActivity.this, R.anim.m_toolbar_visible);
m_Toolbar.startAnimation(ToolbarVisible);
}
});
}
Use visibility of view
if it is visible don't start animation.
Use animation listeners, so that you can set your view visibility to GONE
Try this code::
Add boolean to manage swipe gesture.
private boolean isSwipeUp=falese;
//Swipe Events WebSwipe
WebSwipe = new Swipe(350, 700);
WebSwipe.setListener(new SwipeListener() {
#Override
public void onSwipingLeft(final MotionEvent event) {
}
#Override
public void onSwipedLeft(final MotionEvent event) {
}
#Override
public void onSwipingRight(final MotionEvent event) {
}
#Override
public void onSwipedRight(final MotionEvent event) {
}
#Override
public void onSwipingUp(final MotionEvent event) {
}
#Override
public void onSwipedUp(final MotionEvent event) {
if(!isSwipeUp)
{
Animation ToolbarGone = AnimationUtils.loadAnimation(CoreActivity.this, R.anim.m_toolbar_gone);
m_Toolbar.startAnimation(ToolbarGone);
isSwipeUp = true;
}
}
#Override
public void onSwipingDown(final MotionEvent event) {
}
#Override
public void onSwipedDown(final MotionEvent event) {
Animation ToolbarVisible = AnimationUtils.loadAnimation(CoreActivity.this, R.anim.m_toolbar_visible);
m_Toolbar.startAnimation(ToolbarVisible);
isSwipeUp = false;
}
});
}

onKeyListener not firing an event in Android

I have an Activity with one Fragment. This fragment has one button, and I want something to happen when I push the "enter" button on my hardware keyboard (I am testing it with adb keyevent).
I've read a lot of solutions here on stackoverflow, but none of them worked.
This is my fragment code:
btnPhoto = (Button) rootView.findViewById(R.id.taskBtnPhoto);
photoView = (ImageView) rootView.findViewById(R.id.taskPhotoView);
btnPhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
takePhoto();
}
});
btnPhoto.setFocusable(true);
btnPhoto.setFocusableInTouchMode(true);
btnPhoto.requestFocus();
btnPhoto.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (KeyEvent.KEYCODE_VOLUME_UP == keyCode) {
doFoo();
return true;
}
return false;
}
});
This code is placed in the onCreateView part of my fragment. Using debug, the event is never fired when I launch KeyEvent from ADB or use the hardware keys of my phone (volume keys).
public static class MyFragment extends Fragment {
#Override
public void onResume() {
super.onResume();
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
//YOUR CODE
return false;
}
});
}
}

JAVA / Android - Ontouchstart instead of onClick

We have an onClick function on our relativeLayout but the method triggers when we release our finger from the screen (onTouchEnd). We want our function to trigger on the moment my finger touches the screen.
android:clickable="true"
android:onClick="click"
public void click(View v){
releaseItem();
}
Use View.setOnToucListener(listener)
yourRelativeLayout.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
//Your stuff here
return true;
}
return false;
}
});

View setVisibility with GestureDetector in Android

I have a GestureListener with a view. I want to change view visibility based on the GestureListener results. On double tap show the view, on single tap hide it. And I want to show the vie when I'll hold my finger on the display (snapchat like feature).
mDetector = new GestureDetectorCompat(this, this);
mDetector.setOnDoubleTapListener(this);
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
mDetector.onTouchEvent(event);
return true;
}
});
Here's my gesture listeners.. On double tap it'll show my view but it keeps the view visible
#Override
public boolean onSingleTapUp(MotionEvent event) {
Log.d(DEBUG_TAG, "onSingleTapUp: " + event.toString());
text.setVisibility(View.INVISIBLE);
return true;
}
#Override
public boolean onDoubleTap(MotionEvent event) {
Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString());
text.setVisibility(View.VISIBLE);
return true;
}
I had a similar problem and I added
ImageView imgView11 = (ImageView)findViewById(R.id.imagek11d);
just before imgView11.setVisibility(View.INVISIBLE);
and before imgView11.setVisibility(View.VISIBLE);

Categories

Resources