in my onTouch(View view, Motionevent event) method, the view.getY() always returns the same (0.0). I want to check what view is being touched and compare it with int Y = event.getRawY(). Would something like this be possible?
The reason is that I have a couple of animated fields in my layout, and I would like to animate each view differently depending on which one is being touched. I will appreciate any advice or ideas. Thank you in advance
check api reference first. see View.getY and Motionevent.getRawY.
I did this to solve my problem:
LinearLayout animLayout = new LinearLayout(this);
animLayout.addView(//add some view);
animLayout.setId(1)
animLayout.setOnTouchListener(this);
#Override
public boolean onTouch(View view, MotionEvent event){
case MotionEvent.ACTION_UP
switch (view.getId()}
case 1:
//My code
break;
}
break;
}
}
Basically, give my view an ID and in my onTouch get the ID in a switch case and do what I want with it.
Related
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...
Im trying to play around with some multitouch in Android.
I have two RelativeLayout on screen where I would like to get touch events.
fooLayout = (RelativeLayout)findViewById(R.id.foo);
fooLayout.setOnTouchListener(onTouchListener);
barLayout = (RelativeLayout)findViewById(R.id.bar);
barLayout.setOnTouchListener(onTouchListener);
...
View.OnTouchListener onTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
...
I tried having a OnTouchListener for each layout but that didn't seem to work at all.
I figured since the onTouch method gets an View I would be able to make out with view the current touch event came from and save away the pointer id.
int pointerId = event.getPointerId(event.getActionIndex());
int action = event.getActionMasked();
switch (action) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
switch (v.getId()) {
case R.id.foo:
fooPointerId = pointerId;
break;
case R.id.bar:
barPointerId = pointerId;
break;
}
This does not work as I thought it would.
When my first finger touches in one of the layouts I will get the correct id from that layout. But when I put down a second finger in the other layout it will get the id of the layout where I put down my first finger.
Am I going about this the wrong way?
Is this possible to do or do I have to put the OnTouchListener on the top view and figure out which layout Im touching by the x/y values of the event?
I think you should extend relative layout and implement onTouchEvent(MotionEvent ev) over there to figure out which pointers are touching which child views(Put foo and bar inside your main relative layout/view group that implements onTouchEvent).
IMO onTouch(View v, MotionEvent ev) only lets you register one pointer at a time so it may not be possible using this approach. This link might help out
I am using onTouch method for doing some works. At first step, I want to show currentX value. However, my code does not work at all. How I resolve this issue?
public boolean onTouch(View view, MotionEvent event) {
int currentX = (int) event.getX();
if(event.getAction() == MotionEvent.ACTION_MOVE) {
Log.d("mesage:",currentX+"");
}
return true;
}
check out this link:
Detecting Common Gestures
You have there snippet like this:
View myView = findViewById(R.id.my_view);
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// your stuff
return true;
}
});
do you register your listener for a view? is it separated listener or implemented in Activity or other? is it MotionEventCompat or built in? we need more code...
but you said that you need to recognize "fingers to the right side". look in my link for gestures and "Detecting All Supported Gestures" or "Detecting a Subset of Supported Gestures". onFling method is what you want (right/left you can regonize by velocityX>0 or <0)
edit:
in your activity_main you should have more views. you also may set id for main viewGroup (LinearLayout/RelativeLayout?), which is in whole Activity (Screen) and get this view in findViewById, then setOnTouchLister for this particular view. but like I said above probably you need Gesture
I call this function in my activity :
#Override
public boolean dispatchTouchEvent(MotionEvent touchEvent)
That allows me to process action before any components get focused or even deny the focus to these elements.
PROBLEM : I was wondering how I could know what component (View) has been touched in this function, then I could choose if I want to consumme the event or not.
UGLY SOLUTION : I'm currently having an ugly solution which is : I know the position of the component that is allowed to get the event, and I do a plenty of condition to approximately decide if the user clicked on this component.
Thanks.
You probably want to use the OnTouchListener
private OnTouchListener mOnTouchListener= new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch(v.getId()){
case R.id.id1):
// Do stuff
break;
case R.id.id2:
// Do stuff
break;
}
return false/true;
}
};
view.getid==R.id.//id in layout// condition can be checked for the required view is clicked
I have created a custom View which I usually attach an onClickListener to. I'd like to have some button like behavior: if it is pressed, it should alter its appearance which is defined in the onDraw() method. However, this code does not work:
//In my custom View:
#Override
protected void onDraw(Canvas canvas)
{
boolean pressed = isPressed();
//draw depending on the value of pressed
}
//when creating the view:
MyView.setClickable(true);
pressed always has the value false. What's wrong?
Thanks a lot!
hey buddy,your fault is you are not implementing click or touch evnt for ur custom view.thr is no click evnt for view.you can use touch event instead of this:so below code work 4 u:
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
break;
}
return true;
}
});
in this code use action_up for click and you get it worked for you
have you considered just using a button to do what you want? you could use ToggleButton and write a short selector in xml that will allow you to specify an image to use when pressed or not. this question may be of some help to you.
To make your new custom button draw on clicks don't forget to invalidate the form as necessary. This is a little gotcha. E.g.
#Override
public boolean onTouch(View v, MotionEvent event)
{
/* Parse Event */
this.invalidate();
return true;
}