View setVisibility with GestureDetector in Android - java

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);

Related

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;
}
});

Onclick event on textview(that has TextIsSelectable="true") is ony called on second click

I have a onClickListener on a textview and the textview has the flag that it's selectable.
But the onclick event I specified is only called when the textview is clicked a second time.
After the second time it calles the onclick right, but if a other textview that also is selectable with a onclicklistener it also is only called the second time, then it works fine, but then the other one works only the second time again. I can't find the source of these weird events.
telefoonTXT.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {startTelIntent();}}
);
urlTXT.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {startWebIntent();}
});
I faced this issue as well. Whenever text view is touched firstly onTouch, then OnSelection and at last OnClick is called.
If i understand your problem clearly you want to select text in text view when user double taps or long presses like the usual text selection but when user simply clicks it once u want the onClick to function. I think the following might help you.
Add a gestureDetector to your text View.
GestureDetectorCompat mDetector;
mDetector = new GestureDetectorCompat(this, new GestureDetector.SimpleOnGestureListener());
mDetector.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// This is where u add your OnClick event
startTelIntent();
return false;
}
#Override
public boolean onDoubleTap(MotionEvent e) {
Log.d("dtttt", "double tap");
return false;
}
#Override
public boolean onDoubleTapEvent(MotionEvent e) {
return false;
}
});
telefoonTXT.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
mDetector.onTouchEvent(event);
return false;
}
});
In my case focus change listener works, but be aware for cases when you use it in androidTv that you move with D-pad
textView.setOnFocusChangeListener { v, hasFocus ->
if (hasFocus && textView.measuredHeight > textView.lineHeight * textView.lineCount) {
//your click event
}
}
I check if text can be scrolled then don't take the click event
The accepted answer is good. But I had one additional issue with it: When I have some selection text in textview, I want to tap on textview to only deselecting the text - without perfoming on click listener. And perform on click only with no text selection.
So below is my solution:
#SuppressLint("ClickableViewAccessibility")
public static void setOnClickGestureDetectorListener(TextView textView, Context context, View.OnClickListener onClickListener){
if (textView == null || context == null || onClickListener == null)){
return;
}
final GestureDetectorCompat detector = new GestureDetectorCompat(context, new GestureDetector.SimpleOnGestureListener());
detector.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
onClickListener.onClick(textView);
return false;
}
#Override
public boolean onDoubleTap(MotionEvent e) {
return false;
}
#Override
public boolean onDoubleTapEvent(MotionEvent e) {
return false;
}
});
textView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if ( v instanceof TextView ) {
TextView textView = (TextView)v;
if (textView.getSelectionStart() >= 0 && textView.getSelectionEnd() > textView.getSelectionStart()){
return false;
}
detector.onTouchEvent(event);
}
return false;
}
});
}
And calling example is below:
setOnClickGestureDetectorListener(textView, getContext(), new View.OnClickListener() {
#Override
public void onClick(View v) {
// This is where u add your OnClick event
}
});
set this ...
TextisSelectable = "false"
i think it will be work correctly

Using OnLongTouch to move image to string

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;
}
});

How to set up an onTouchListener that changes an ImageView image resource?

I want to setup up an onTouchListener that changes an ImageViews image resource on ACTION_UP and ACTION_DOWN. Here is my code:
// Setup a basic upDownListener that registers UP and DOWN actions and changes the
// view image resource accordingly
View.OnTouchListener upDownListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
// Button was pressed, change button background
v.setImageResource(R.drawable.button_white_pressure);
return true;
} else if(event.getAction() == MotionEvent.ACTION_UP) {
// Button was released, reset button background
v.setImageResource(R.drawable.button_white);
return true;
}
return false;
}
};
This code does not compile because View does not have the method setImageResource, only ImageView. But how do I tell the listener that an ImageView is being used?
Cast the incoming View to an ImageView.
Sample code:
#Override
public boolean onTouch(View v, MotionEvent event) {
ImageView iv = (ImageView) v;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
iv.setImageResource(R.drawable.button_white_pressure);
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
iv.setImageResource(R.drawable.button_white);
return true;
}
return false;
}
View.OnTouchListener upDownListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
ImageView v = (ImageView) v;
if(event.getAction() == MotionEvent.ACTION_DOWN) {
// Button was pressed, change button background
v.setImageResource(R.drawable.button_white_pressure);
return true;
} else if(event.getAction() == MotionEvent.ACTION_UP) {
// Button was released, reset button background
v.setImageResource(R.drawable.button_white);
return true;
}
return false;
}
};

On fling to cause a simple animation

I am not sure how to make a simple onFling simply start an animation. It doesnt matter which direction the swipes go any contact and slide across the screen should cause the animation to start. The other thing I am wondering is how to get it to let the animation run through and when the animation finishes I want it to display a picture. To give you an idea of what I am doing picture a twister spin board. The animation is the spinner spinning and after it finishes spinning it stops and points in a direction. How can I get a similar effect. (The animation is a spinning animation and the pictures are all pointing in differnt directions)
Would appreciate any help anyone can give. Thanks.
one possibility: use a gesturedetector to detect the fling.
psuedocode:
public class WhatEver extends Activity implements OnGestureListener {
private GestureDetector gestures;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gestures = new GestureDetector(mContext, this);
View yourViewYouWantToHaveThemFling=(View) findViewById(blah..);
yourViewYouWantToHaveThemFling.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent event) {
if (gestures.onTouchEvent(event)) {
return true;
}
return false;
}
});
}
//all your normal stuff
#Override
public boolean onDown(MotionEvent e) {
return true; //must return true to continue
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
//do something
return false;
}
#Override
public void onLongPress(MotionEvent e) {
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
return false;
}
#Override
public void onShowPress(MotionEvent e) {
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
}
as for the animation, set a listener on your animation,
yourAnimation.setAnimationListener(new DisplayPicture());
then
private final class DisplayPicture implements Animation.AnimationListener {
.
.
public void onAnimationEnd(Animation animation) {
//code to display your picture here
}
.
.
}

Categories

Resources