So I am trying to implement a piano like interface but with textviews instead of keys. When I slide from textView to textView it should trigger something. I tried using an onTouchListener instead of an onClickListener, but it doesn't seem to work. It only triggers the first textView that I touch. Anyone know how to do this?
tv.setOnTouchListener(new CustomTouchListener());
public class CustomTouchListener implements View.OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
switch(motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
// Action you you want on finger down.
Toast.makeText(this,"onTouch",Toast.LENGTH_LONG).show();
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
// Action you you want on finger up
break;
}
return false;
}
}
Is this exact what you need?? Please share some code of you which will make more easier to fix problem.
Related
I am using custom animations at ACTION_DOWN and ACTION_UP events of my button.
However, right now, I am manually setting OnTouchListener to each Button manually with code like this:
myButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View yourButton, MotionEvent theMotion) {
Animation animation;
switch (theMotion.getAction()) {
case MotionEvent.ACTION_DOWN:
animation = AnimationUtils.loadAnimation(NickNameScreenActivity.this, R.anim.button_down);
myButton.startAnimation(animation);
break;
case MotionEvent.ACTION_UP:
animation = AnimationUtils.loadAnimation(NickNameScreenActivity.this, R.anim.button_up);
myButton.startAnimation(animation);
break;
}
return false;
}
});
This is cumbersome as I have to repeat this for all Buttons.
Is there a way to set it automatically for all buttons or a tidy XML way of doing this?
You can write a custom view extending Button and use it in xml. Maybe you can replace NickNameScreenActivity.this with getContext().
I want to highlight text and then drag and drop it to a specific area. I know how basic drag and drop works and how to get the selected area to a String.
But I don't know how to to attach a setOnClickListener on a String? Anyone have any idea or is that a total wrong approach?
The string is in an editView
et =(EditText)findViewById(R.id.et);
int startSelection=et.getSelectionStart();
int endSelection=et.getSelectionEnd();
test = et.getText().toString().substring(startSelection, endSelection);
As the commenters pointed out, you can not set a a touch listener on a String, you have to do it on your EditText. You would do something like this:
et.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
et.setFocusable(true);
et.requestFocus();
// Do what you want
break;
case MotionEvent.ACTION_UP:
// Do what you want
break;
default:
break;
}
return true;
}
});
But still, I do not understand why do you want to use a OnTouchListener and not a OnDragListener, which seems to serve your purpose better...
I have a RelativeLayout with a single ImageView inside of it. The ImageView has an OnTouchListener attached to it which is working perfectly fine if I just press the ImageView. The problem I'm having though is that if I press and hold in the empty space outside of the ImageView and then try to press the ImageView the touch listener doesn't fire. It seems like the layout is absorbing the touch events somehow. Any ideas on how I could fix this?
The action defined by you here is a Multi-touch gesture. Go through this doc to know how to handle this type of actions.
Also take a look at the ACTION_POINTER_DOWN action define in MotionEvent. This is the event that will get called when additional fingers come down.
you mout use this code:
imageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
//your code
break;
case MotionEvent.ACTION_UP:
//your code
break;
case MotionEvent.ACTION_MOVE:
//your code
break;
default:
//your code
break;
}
return true;
}
});
Ok... in my app i update the layout on MotionEvent.ACTION_DOWN and then i check the motion event coordinates to locate my buttons. I can show a toast when finger is released on different buttons. The problem is i need a long touch on my buttons to call another action without conflicting with the MotionEvent.ACTION_UP. Implemented a long click handler but since i don't 'click' its not working. Hope you guys understand my problem.
Whats the best way to get my app working as intended?
My class implements OnTouchListener, OnGestureListener
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
// UPDATE LAYOUT
break;
case MotionEvent.ACTION_UP:
// GET BUTTON X Y
if (x and y match the button location){
// DO ACTION
}else{
// DO NOTHING
}
// CHANGE LAYOUT TO INITIAL STATE
break;
case MotionEvent.ACTION_MOVE:
break;
}
return false;
mybutton.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// DO STUFF
return true;
}
});
}
just try to return false in your onTouch(...) method and use onLongClickListener(...) as usual
I'm trying to create a custom GridView but i'm having troubles with the touch listeners.
What i want to do:
Create a GridView with custom Views.
Longpress on an item so it becomes 'editable'.
Drag the view horizontal or vertical to move it's position in the GridView.
Here's where i'm having trouble:
I'm implementing GestureDetector.OnGestureListener for the longpress functions, because for some reason using the gridview.setOnItemLongClickListener() isn't working when overriding the onTouchEvent() of the GridView itself (Which i need for the dragging part). So everything is fine at this point. Now i only need to know when the longpress is finished. So i though: "Well this shouldn't be hard." I couldn't have be more wrong. I've fiddled around for quite some time with this and it looks like using different touch events isn't helping me :/
When stepping through the onTouchEvent() i noticed that only 1 action is given: MotionEvent.ACTION_DOWN. So what am i doing wrong? i need the MotionEvent.ACTION_UP...
Found the culprit:
i was doing something like this
#Override
public boolean onTouchEvent(MotionEvent event) {
// Give everything to the gesture detector
boolean retValue = gestureDetector.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE :
onMove();
break;
case MotionEvent.ACTION_UP :
onUp();
break;
case MotionEvent.ACTION_CANCEL:
onCancel();
break;
}
return retValue;
}
i think retValue was always returning false so no other events were triggered.
this fixed the issue:
#Override
public boolean onTouchEvent(MotionEvent event) {
// Give everything to the gesture detector
gestureDetector.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE :
onMove();
break;
case MotionEvent.ACTION_UP :
onUp();
break;
case MotionEvent.ACTION_CANCEL:
onCancel();
break;
}
return true;
}