I have an ImageView inside a ScrollView. The ImageView is clickable and opens up a new Activity.
Whenever I scroll through my ScrollView first touching the image (But not releasing my finger yet) and try to scroll it fires my onTouch() or onClick() method (I have tried with both...)
Here is my code:
btnAdd.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Intent intent = new Intent(Intent.ACTION_EDIT);
startActivity(intent);
return true;
}
});
As I said I also tried with onClick()...
What am I doing wrong?
When the OnTouchListener is triggered it listen for multiple actions (Action Down, Action Up, etc) and executes them all so you should specify exactly when you want to start the new activity (in your case on Action Up). So try to use this:
btnAdd.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP){
Intent intent = new Intent(Intent.ACTION_EDIT);
startActivity(intent);
}
return true;
}
});
Related
I have a RecyclerViewClickListener.RecyclerTouchListener and on that #onClick override method, I sat Intent to go another activity.
But the problem is on that RecyclerViewClickListener.RecyclerTouchListener there is one image view also and I implemented onClickListener of that InmageView so that it will open one dialog box.
Now, when I click on that ImageView, 2 things happened at same time.
1.Opened Dialog box
2.It is going to another activity also, because of Intent.
How can I fix?
Try this
holder.hamburgerMenu.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
if (menuInterface != null) {
menuInterface.onClickHamburger(position);
}
return true;
}
return false;
}
});
When i use an onTouchListener in the getView of my adapter the line
android:listSelector="#drawable/circle"
immediately stops working, if I set onTouch to return false it works again, however then the ACTION_DOWN ACTION_UP dosent work properly.
Heres what i have in onTouch
image.setOnTouchListener(new View.OnTouchListener() {
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Assets.playMusic(songID, false);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
Assets.mediaPlayer.stop();
Assets.mediaPlayer = null;
}
return true;
}
});
Its suppose to play music for as long as you have a finger held on the item and when you release it should stop the music. And it works well when returning true. However for some reason the circle stops appearing behind the tapped items. If it is set to false the circle appears, but then action_up dosent stop the music
ive tried using .setSelected .setActivated .setEnabled and none of them work
please help
Also i want it to work kinda like snapchats camera button, tap it and it does one thing, hold it and it does something for duration of your hold. I was going to use time variables in the Action up and down. but if anyone knows another way to do this id appreciate info about that too
In this situation is not encouraged to attach an OnTouchListener to the image, but to the items of the GridView instead.
You should have something like this:
GridView gridview = (GridView) findViewById(R.id.the_gridview_id);
gridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View v, int position, long id) {
doSomething();
}
});
EDIT:
In order to know how much time a view is pressed, you can do something like this:
// this goes somewhere in your class:
long lastDown;
long lastDuration;
...
// this goes wherever you setup your button listener:
gridview.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
lastDown = System.currentTimeMillis();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
lastDuration = System.currentTimeMillis() - lastDown;
}
}
};
I have 2 activities - MainActivity and page1. I have intent from the first like this:
button_forward.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Intent intent = new Intent(MainActivity.this, Page1.class);
startActivity(intent);
return true ;
}
However, my Page1 class's onCreate is called twice! i can detect it with toast message: it appears twice on the screen:
#Override
protected void onCreate(Bundle savedInstanceState) {
Toast.makeText(Page1.this, "onCreate 1", Toast.LENGTH_SHORT).show();
super.onCreate(savedInstanceState);
setContentView(R.layout.page1);
//some code here like findview by id...
}
});`
I do have some methods that use countdowntimer in my Page1 activity, timers that wait for 2 secs, i commented them out, but onCreate() still gets called twice.
I added code to prevent screen rotating, but the error persists
The problem is that your touch listener doesn't check which type of event we have, this means that your onTouch should be called like 100 times per second, but it actually is called only twice because the new Activity UI covers the button. Use this code to check which event we have:
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() != MotionEvent.ACTION_DOWN) return false;
Intent intent = new Intent(MainActivity.this, Page1.class);
startActivity(intent);
return true;
}
If the action is ACTION_DOWN it means that the user has just touched the button. If it is ACTION_UP it means that the user lifted his / her finger.
What I am having:
I am having a imageview on a linear layout. I want to detect
onTouch of imageview.
I do not want to use onClick because my implementation requires
onTouch Imageview is the child of linearLayout
What is happening:
Two touch events are firing when i click on image one from image and
another from the linear layout(parent)
Question:
How can I disable onTouch of linearLayout(parent)retaining the
onTouch of Imageview
Code:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
imgUsrClrId.setOnTouchListener(imgSourceOnTouchListener);
}
OnTouchListener imgSourceOnTouchListener= new OnTouchListener(){
#Override
public boolean onTouch(View view, MotionEvent event) {
Log.d("", "");
return true;
}};
Touch event is fired for only one view at a time, and here in your code touch event is fired for imageview but as we know touchListener will be called for every MotionEvent.ACTION_DOWN, MotionEvent.ACTION_UP, and MotionEvent.ACTION_MOVE. So if you want only one event to be fired at a time, ie MotionEvent.ACTION_DOWN or MotionEvent.ACTION_UP then write it in this way:
#Override
public boolean onTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
switch (action) {
// MotionEvent class constant signifying a finger-down event
case MotionEvent.ACTION_DOWN: {
//your code
break;
}
// MotionEvent class constant signifying a finger-drag event
case MotionEvent.ACTION_MOVE: {
//your code
break;
}
// MotionEvent class constant signifying a finger-up event
case MotionEvent.ACTION_UP:
//your code
break;
}
return true;
}
There are no multiple touch events generated from different views its all touch events from same ImageView I did test like below
Have a trace the viewID from which it
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Log.i("Tag","ImageView ID :"+imageView.getId());
imageView.setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
Log.i("Tag","OnTouch View ID :"+v.getId());
return true;
}
});
and when you return true from onTouch event will be consumed.
and here is output
ImageView ID :2131230721
OnTouch View ID :2131230721
OnTouch View ID :2131230721
OnTouch View ID :2131230721
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?