I am learning how to make android apps, and I can't figure out how to implement a kind of OnMouseMoved event in android.
I've tried using an OnTouchListener, but It doesn't update when I touch, hold and move. It only updates when I tap a different points. Like this:
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
thread.getGameState().touch(x);
return false;
}
I've tried this
#Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
if(event.getAction() == MotionEvent.ACTION_MOVE) {
thread.getGameState().touch(x);
}
return false;
}
But this doesn't even respond at all to touch, maybe the point i'm getting from the event is an old point, and not the new drag point?
thread.getGameState().touch(x); sets the x-value of and object, which should make it like dragging.
You must return true in onTouch() to tell Android that you will handle the complete touch event, so you can receive events after ACTION_DOWN:
#Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
if(event.getAction() == MotionEvent.ACTION_MOVE) {
thread.getGameState().touch(x);
}
return true;
}
Related
I have WebView and other view behind/under.
How to control touches on WebView and pass behind to other view, but only these from specific area of WebView?
Is there maybe a function where you can catch events and other function that you can trigger these events on other view?
This code did the trick:
sourceView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
MotionEvent motionEvent = MotionEvent.obtain(event);
//these 3 lines is for bug when target and source has different on screen positions
sourceView.getLocationOnScreen(posTemp1);
targetView.getLocationOnScreen(posTemp2);
motionEvent.offsetLocation(posTemp1[0]-posTemp2[0], posTemp1[1]-posTemp2[1]);
targetView.dispatchTouchEvent(motionEvent);
return false; //true if we want sourceView to not handle it
}
});
I didn't include detecting specific area, but it's easy just test result of motionEvent getX() and getY();
So, basically I have a clickable ImageView with a listener:
imageView1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
//do something while pressed
}
if(event.getAction() == MotionEvent.ACTION_UP){
//when finger is lifted
}
return true;
}
});
It works, but the problem is that there is an annoying delay between the moment when I press the ImageView and when the code is actually executed.
Is there any way to fix it? I've read about a prepressed state that may fix it, can anyone explain it to me?
I've got the following code which fires every time my "redTime" EditText is touched.
redTime.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("click", "onMtouch");
redTime.setSelection(redTime.getText().length());
return false;
}
});
It is meant to keep the cursor on the right side of the EditText upon every touch. The problem is that the line containing the "setSelection" method doesn't work upon the FIRST touch of the control. That is, if another control has focus, and I touch the "redTime" control for the first time, the method is fired, but the cursor remains at the location I touched (not the right side).
How do I know the listener fires? The "Log.i" call works, but not the cursor change. I suspect the "setSelection" call is working, but some later event is negating it.
I tried a few things. I tried consuming the event by returning TRUE in the listener. Didn't work. I tried repeating the "setSelection" call in OnTouchListener, and OnFocusChanged as well. Still doesn't work.
I almost forgot. Here is the XML for the control in question.
<EditText
android:id="#+id/redEditText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:ellipsize="end"
android:gravity="right"
android:inputType="time"
android:text="#string/zeroTime"
android:textColor="#ff0000"
android:textSize="32sp" >
</EditText>
I ran into this issue recently and couldn't get anything to work, so ended up doing this:
setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
postDelayed(new Runnable() {
#Override
public void run() {
setSelection(getText().length());
}
}, 50);
return false;
}
});
To avoid the error do something like this:
yourEditText.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
yourEditText.setFocusable(true);
yourEditText.requestFocus();
yourEditText.setSelection(emailEditText.getText().length());
break;
case MotionEvent.ACTION_UP:
v.performClick();
break;
default:
break;
}
return true;
}
});
You could do
redTime.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("click", "onMtouch");
redTime.setFocusable(true);
redTime.requestFocus();
redTime.setSelection(redTime.getText().length());
return false;
}
});
Before you call setSelection, that way redTime will have the focus when you do the selection.
Just put the edittext as like below
<EditText
android:id="#+id/from"
android:focusable="false"
android:hint="#string/pickup_location"
android:imeOptions="actionNext"
android:inputType="textNoSuggestions"
android:padding="10dp"
android:textColor="#android:color/white" />
Set the ontouch listener in your activity
EditText et = (EditText)findViewById(R.id.et); et.setOnTouchListener(this)
#Override public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP)
switch (v.getId()) {
case R.id.from:
//do your action
break;
use this method:
redTime.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_UP){ //check the event
redTime.requestFocus(); //keep focus on the EditText(redTime)
redTime.selectAll(); //select all text
}
}
return true;
});
In a view I want to detect both single tap and double tap. It works fine with me. I can detect both single and double tap. Please find my code
#Override
public boolean onTouch(View view, MotionEvent me) {
// No dragging during animation at the moment.
// TODO: Stop animation on touch event and return to drag mode.
boolean result = true;
result = gestureDetector.onTouchEvent(me);//return the double tap events
if(result)
return false;
switch (me.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
}
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
private String fileName;
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
// event when double tap occurs
#Override
public boolean onDoubleTap(MotionEvent e) {
float x = e.getX();
float y = e.getY();
if(player!=null && player.isPlaying())
{
player.stop();
}
else
{
setFileName(x);
player = new AudioPlayer(fileName);
player.play();
}
return true;
}
}
I am doing some code in Action_UP event. So even when I do a double tap, for the first tap the ACTION_UP event is called and the corresponding code is executed. I want to prevent this from happening. How can I do this. I just want to make sure that the ACTION_UP is called only when the user is done with his gesture. Not immediately after his partial gesture . In this case its being called on finger up of first tap.
How can I make this work in android?
Thanks
According to gesture listener documentation, you need to return true for onSingleTapConfirmed & onDoubleTap. Otherwise the event is not consumed and gestureDetector.onTouchEvent(me); returns false.
I am following the book Professional Android development by Reto Meier, and there is an example of a ToDo list to be done in order to practice.
The problem is that I do everything as the book says, but I cannot add any task when pushing the central KeyPad, as I get an exception and the program has to close.
I tried to debug in Eclipse, and apparently it cannot find the .class file (?)
Here is the code where all the bad things are happening, specially in the todoItems.add line:
myEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
todoItems.add(0, myEditText.getText().toString());
myEditText.setText("");
aa.notifyDataSetChanged();
return true;
}
return false;
}
});
give this a shot - it uses View v that's passed in.
I assume that "todoItems" is properly instatiated
myEditText.setOnKeyListener(new View.OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
EditText edittxt = (EditText)v;
todoItems.add(0, edittxt.getText().toString());
return false;
}
});