I have a View and I need to detect a single tap on a LinearLayout.
I don't care about movements, All I want to detect is a single tap.
Basically a touch detection just like the touch detection on buttons. How can I achieve this?
myView.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
// This gets called so many times on every movement
return true;
}
});
I'm pretty sure, this would work aryaxt:
myView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// myView tapped //
}
});
Related
I am trying to make some field with text (no button) what I click on to open datepicker dialog.
I am totally begginer and I am trying it on easy example.
firstbirth.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Toast.makeText(getApplicationContext(), "ahooj", Toast.LENGTH_LONG).show();
return false;
}
});
firstbirth is spinner, I tried it with Edittext and the result was same.
Can anybody help me?
Thanks in advance!
Use an OnClickListener instead.
setOnClickListener(View.OnClickListener l)
OnTouchListeners will trigger on multiple touch events (like touch down, touch up etc.) whereas the OnClickListener will only get fired once (onClick :) )
If you, for some reason, want to use an OnTochListener you can make sure that its only called once.
Try:
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_UP { // another option would be ACTION_DOWN for example
Toast.makeText(getApplicationContext(), "ahooj", Toast.LENGTH_LONG).show();
return true;
}
return false;
}
Thanks it sounds logical but when I use OnClick event application felt down while initializing.
firstbirth.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DialogFragment dd1 = new DatePickerFragment();
dd1.show(getFragmentManager(), "Select a date");
}
});
Problem is not with DialogFragment becasue it works when I use onTouch event.
Thanks :)
The most common way to handle a button click is:
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
//exeute some code here
}
});
So I click the button, hold for a while and it executes the code when my finger actually leaves the button. When I hold it, I can't click it anymore (I mean while clicking - nothing happens).
1) Is it possible to execute the code when I my finger touches the button (not when it leaves)?
2) Is it possible to execute the code when I hold the button and then my second finger touches it (I want to use multitouch feature)?
As #Raghunandan commented, use setOnTouchListener as follow..
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN) {
Log.e(TAG,"Down");
return true;
}
if (event.getAction()==MotionEvent.ACTION_MOVE){
Log.e(TAG,"Move");
return true;
}
if (event.getAction()==MotionEvent.ACTION_UP){
Log.e(TAG,"Up");
return true;
}
return false;
}
});
There is a list view. I wrote this to stop the scroll when the user touches the screen while scrolling:
public class OnTouchListner implements OnTouchListener{
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN)
{
businessResultListView.smoothScrollBy(0, 0);
return true;
}
return false;
}
}
When i scroll the list down and when I touch the screen, the list view stops scrolling. But if I again try to scroll down / up, without lifting my finger from the scroll view, i am unable to scroll it.
If I take off my finger, then touch the list and then try to scroll, then I can do so.
How can I make my list scrollable after touch ?
Any help is greatly appreciated...
Give the Scroll id like scroll_home
ScrollView scroll_home;
scroll_home = (ScrollView) findViewById(R.id.scroll_event);
and on touch listview row add this function ...
this working well
v.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
scroll_home.requestDisallowInterceptTouchEvent(true);
return false;
}
});
When application is started I run a custom pop-up till a user touches the screen. When screen is touched I catch it with event onTouch() and cancel the pop-up. From this point I don't need the event anymore.
The problem is the event is alive and continues to jump up every time a user touches the screen.
Is there any way to unsubscribe from this event? Something like in c# -= eventName.
The code is below:
#Override
public boolean onTouch(View v, MotionEvent event) {
if (!_stopToast)
{
_hintToast.cancel();
_stopToast = true;
}
return false;
}
There's no such method (lets say removeTouchListener or similar) which will help you to remove an already defined touch listener from a view. Setting null to setOnTouchListener won't help too. What you can do is to create a new object reference of OnTouchListener class which does nothing and set it in setOnTouchListener. For example:
public final OnTouchListener dummyOnTouchListener = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent rawEvent) {
return false;
}
};
And simply use it as below:
yourView.setOnTouchListener(dummyOnTouchListener);
I have an on click listener:
whiteKeyPressedArray[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}}
I see that this allows touches:
public boolean onTouch(View v, MotionEvent event) {
//Switch case for type of touch
}
But how can I detect touch rather than click on my whiteKeyPressedArray[i]?
Thanks!
OnTouch will fire many many times :), actually onTouch will be trigered over and over again as long as you keep your finger to that element (as long as you touch that element). Where onClick will be fire just ones but ONLY if you return false from your onTouch handler.
I don't know what the whiteKeyPressedArray[i] is, but have you tried:
whiteKeyPressedArray[i].setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return true; // or false if you want the event to pass on
}
});
Maybe this is what you are looking for?