I need to detect WHEN the screen is touched. The onTouchEvent method only detects when the finger is moving. I need method which returns boolean value true, when the finger touches screen and returns false, when its not.
Here's a very basic implementation of the onTouch method that modifies a boolean value to know if a the screen is touched. You may need to tweak it to suit your specific needs (and maybe handle multi touch)
private boolean mIsScreenTouched;
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN :
case MotionEvent.ACTION_MOVE :
mIsScreenTouched = true;
break;
case MotionEvent.ACTION_CANCEL :
case MotionEvent.ACTION_UP :
mIsScreenTouched = false;
break;
}
return true;
}
#Override
public boolean dispatchTouchEvent(MotionEvent e) {
// TODO Auto-generated method stub
super.dispatchTouchEvent(e);
if(btn.onTouchEvent(e)){
return btn.onTouchEvent(e);
}else{
return false;
}
}
Related
How can I detect when the touch event is over in Android,in Webviews specifically.
pseudocode:
if (touch event is over) then
do something
end
Thanks!
The MotionEvent object passed to the onTouchEvent(...) method has a getAction() method, you can use it to determine if the event is over. eg:
webViews.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//pointer down.
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_OUTSIDE:
//event has finished, pointer is up or event was canceled or the pointer is outside of the view's bounds
break;
}
return false;
}
}
Also, if you want to consume the event (stop it from propagating) just make the onTouch(...) method return true instead of false.
It's simple. The OnTouchListener does not work at all.
I'm fairly certain it's being initialized. I'm using a custom view to draw my UI, and as usual, I'm being impeded by Android's touchy API (pun intended):
public class ViewInterface extends View implements OnTouchListener{
public ViewInterface(Context context){
super(context);
...
}
public void update(){
...
}
#Override
public void onDraw(Canvas canvas){
...
}
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
keyboardEnabled = true;
return true;
case MotionEvent.ACTION_UP:
keyboardEnabled = false;
return true;
default: break;
}
return false;
}
Code for initializing the view in the main activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//setContentView(R.layout.activity_aidan);
activity = this;
viewInterface = new ViewInterface(this);
setContentView(viewInterface);
initializeSpeechRecognition();
findMe();
run.start();
}
What am I missing? Upon touching the screen and holding it, keyboardEnabled should be set to true (its just a debug value) - yet it does not do anything. The touch events don't seem to be responding at all.
I've attempted to use the onTouchEvent method built into View with the following code:
#Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
keyboardEnabled = true;
return true;
case MotionEvent.ACTION_UP:
keyboardEnabled = false;
return true;
default: break;
}
return false;
}
However this did not work either. The touch events are not responding at all - tapping the screen and releasing should be setting keyboardEnabled to false - which it does not.
You should override the onTouchEvent() method of the View class.
Right now you are simply implementing the OnTouchListener interface which should be used in conjunction with the setOnTouchListener() method.
For example:
#Override
public boolean onTouchEvent(MotionEvent event) {
// do whatever
}
Don't forget to call super where appropriate!
Alternatively, you could call setOnTouchListener(this); somewhere in your code, but that seems redundant.
I'm trying to create a view programmatically, and change its background according to its click state.
I know I can do this with xml drawables, but I want to learn to program it too.
But if I press on my view, then slide my finger, the view gets stuck in pressed state. (white = 0xaaffffff background)
My code is this :
Edit:
I solved the problem. Here is the working code :
mainContainer.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
if (action==MotionEvent.ACTION_DOWN)
{
v.setBackgroundColor(0xaaffffff);
return false;
}
if(action==MotionEvent.ACTION_MOVE)
{
}
if(action==MotionEvent.ACTION_CANCEL)
{
v.setBackgroundColor(0x00ffffff);
}
if (action==MotionEvent.ACTION_UP)
{
v.setBackgroundColor(0x00ffffff);
return true;
}
return false;
}
});
As you see, I tried returning true wherever I want onTouch() to be called again. But apparently there is something I do not understand here.
What am I doing wrong ?
Thanks for any help.
What about returning true in your ACTION_UP aswell?
if (action==MotionEvent.ACTION_UP)
{
v.setBackgroundColor(0x00ffffff);
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 want to lower (or upper) my media volume in my application when an "OnLongClickEvent" is detected.
Here my sources :
buttongauche.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
playSound(R.raw.volumevoixdiminue);
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER,AudioManager.FLAG_SHOW_UI);
return true;
}
});
Actually, it's work : when I do a longClick on my "buttongauche", the volume is decreased by 1.
Now I would like to know how could I do if I want to lower the sound continuously (for example, decrease sound by 1 every 2 seconde when the button is down).
My button "buttongauche" has already an "onClickEvent", who do other things (change the index of a menu).
Thanks
Declare field boolean touching = false; that says whether or not you are touching the button and use OnTouchListener to change it. When you start touching also start volumeThread that lowers the volume every 1 second, and dies when you stop touching.
buttongauche.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touching = true;
Thread volumeThread = new Thread() {
public void run() {
while (touching) {
audio.adjustStreamVolume(
AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER,
AudioManager.FLAG_SHOW_UI);
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
volumeThread.start();
break;
case MotionEvent.ACTION_UP:
touching = false;
break;
}
return false;
}
});