Android ontouchlistener help - android

I want to know that if I have two activities in a program then how can I switch between two activities using ontouchlistener, just only touching anywhere on the screen?
public class V19 extends Activity implements OnTouchListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lay19);
}
#Override
public boolean onTouch(View to_main, MotionEvent event) {
return false;
}
}

You can use layout inflater and method setContentView(View v)
public class V19 extends Activity implements OnTouchListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = getLayoutInflater().inflate(R.layout.lay19, null);
setContentView(view);
view.setOnTouchListener(this);
}
#Override
public boolean onTouch(View to_main, MotionEvent event) {
Intent i = new Intent(this, Activity2.class);
startActivity(i);
return false;
}
}
And than you can catch all touch events.
Other way is to override public boolean dispatchTouchEvent (MotionEvent ev) method. Reference says:
public boolean dispatchTouchEvent (MotionEvent ev)
Since: API Level 1
Called to process touch screen events. You can override this to intercept all touch screen events before they are dispatched to the window. Be sure to call this implementation for touch screen events that should be handled normally.
Parameters
ev The touch screen event.
Return true if this event was consumed.

You will need to pin a listener to different aspects in your layout (buttons, etc.)
To have the device pick up touch events anywhere, I've used public boolean onTouchEvent(final MotionEvent event), which will hold the entire event performed (how many fingers, position, etc.)

by following this tutorial you should be able to move from one to the other.
The tutorials uses a button click but it should be possible to replace that with what you want.
Basically you need to have your activity made(which I assume you do)
Call your intent and activity onTouch instead of on a button click.
And don't forget to declare your new activity in the manifest.
<activity android:name=".YourActivityName"></activity>

Related

OnClickListener in MyCustomView which extends SurfaceView

I have an extended view from SurfaceView like this:
public class MyView extends SurfaceView implements View.OnClickListener {
public MyView(Context context)
{
super(context);
mContext = context;
setOnClickListener(this);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case DOWN OR UP OR MOVE: <-- I do something here like move and drag and drop and
// any related behavior to MyView
}
invalidate();
return true;
}
Now I implemented OnClickListener and set it in my constructor.
OnClick method:
#Override
public void onClick(View v){
Log.e("as", "clicked");
}
But onClick method never not called. How can I solved that?
When replaced return true; with return super.onTouchEvent(event); my onClick method called, but when I want to move my custom view, onClick method again called and this is not my porpuse.
I want only when I tapped my custom view, onClick called, and when moved it not called.
Thanks In advance.
The problem is probably your onTouch Listener. You are overriding and not dealing with the onClick possibility. Additionally you are consuming the event with a return true so the onClick will never be called.
Remember that onClick is dealt in onTouchEvent implemented in the original view class.
You can check more info at:
http://developer.android.com/reference/android/view/View.html#onTouchEvent(android.view.MotionEvent)
where you can read:
public boolean onTouchEvent (MotionEvent event)
Added in API level 1 Implement this method to handle touch screen
motion events.
If this method is used to detect click actions, it is recommended that
the actions be performed by implementing and calling performClick().
This will ensure consistent system behavior, including:
obeying click sound preferences dispatching OnClickListener calls
handling ACTION_CLICK when accessibility features are enabled
Parameters event The motion event.
Returns True if the event was
handled, false otherwise.
Since you want to choose when click is performed, you should implement the performClick (like it states in the link I have referenced).

setonclicklistener and setontouchlistener at one time in android

i am new to android and having a requirement that setonclicklistener and setonTouchlistener both work at one time.i m performing some operation on the adapter side where i m having the imageview.some code to understand
adapter class:
public class Taukygridview extends BaseAdapter{
holder.imageview.setOnClickListener(new OnClickListener() {
//some click operation here
});
holder.imageview.setonTouchListener(new MyTouchListener());
holder.imageview.setonTouchListener(new MyDragListener());
}
here is the MyTouchListener class description:
public class MyTouchListener implements OnTouchListener
{
private Context mcontext;
public static int view_position=0;
public static View first_image_view;
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
//some touch operation here
}
}
here is some description on the drag operation..
public class MyDragListener implements OnDragListener
{
#Override
public boolean onDrag(View v, DragEvent event) {
// TODO Auto-generated method stub
switch(v.getId):
{
//some drag operation here
}
}
so while running the app only the touch operation work and if the touch operation return false then only the click operation work but can't omit or comprise any of the operation(actually i m performing click for selection and touch for the drag and drop operation).so can any one suggest me how can do all the operation without disabling any of the operation mention above.any reply will be very helpfull.thanks
As you can see, public boolean onTouch(View v, MotionEvent event) returns a boolean
Set it to true (return true;) and it will consume the event so the OnClickListener will not be triggered.
Set it to false (return false;) and releasing the screen will trigger the OnClickListener
BTW not leading to any obscure bugs !!!!!!!!
When you're working with touch events, you're essentially overriding the built-in gesture detection (which handles click/long click/etc). While it is doable, I'd advise against it, since it has the potential of leading to really obscure bugs.
Instead, use GestureDetector with a SimpleGestureDetector and handle onTapUpConfirmed and onScroll.

How to unsubscribe from onTouch() event

When application is started I run a custom pop-up till a user touches the screen. When screen is touched I catch it with event onTouch() and cancel the pop-up. From this point I don't need the event anymore.
The problem is the event is alive and continues to jump up every time a user touches the screen.
Is there any way to unsubscribe from this event? Something like in c# -= eventName.
The code is below:
#Override
public boolean onTouch(View v, MotionEvent event) {
if (!_stopToast)
{
_hintToast.cancel();
_stopToast = true;
}
return false;
}
There's no such method (lets say removeTouchListener or similar) which will help you to remove an already defined touch listener from a view. Setting null to setOnTouchListener won't help too. What you can do is to create a new object reference of OnTouchListener class which does nothing and set it in setOnTouchListener. For example:
public final OnTouchListener dummyOnTouchListener = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent rawEvent) {
return false;
}
};
And simply use it as below:
yourView.setOnTouchListener(dummyOnTouchListener);

Is there anyway to remove an onTouchListener from a view object?

I have an on touch listener for a webview, but it has a bad effect on the functionality of the webview, so I am wondering if there is anyway to removed the on touch listener after the initial interaction?
webView.setOnTouchListener(null);
So in you activity you would set your overridden onTouchListener:
mWebView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.setOnTouchListener(mWebView.mOnTouchListener);
return false;
}
});
And you would have to make a new class, extending WebView. And within it you would define an OnTouchListener.
public final OnTouchListener mOnTouchListener = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent rawEvent) {
return false;
}
};
Setting the ontouchlistener to null doesn't reset it to the default definition. You still have to provide an actual listener.
I was looking for help online and got to this post.
When I did
myView.setOnTouchListener(null);
my myView stopped responding to the onTouch.

Handling UI Events in Android

I'm trying to modify some code I found online to my needs. It is supposed to catch a MotionEvent (a fling in particular) and launch a separate activity. I am new to java, so I'm having some trouble understanding the code. Here's what I have so far:
public class Hypertension extends Activity {
private GestureDetector flingDetector;
View.OnTouchListener gestureListener;
private TextView redView;
private TextView greenView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
redView = (TextView)findViewById(R.id.buttonRed);
greenView = (TextView)findViewById(R.id.buttonGreen);
redView.setOnTouchListener(gestureListener);
greenView.setOnTouchListener(gestureListener);
flingDetector = new GestureDetector(new MyFlingListener());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (flingDetector.onTouchEvent(event)) {
//startActivity(new Intent(Hypertension.this, Green.class));
return true;
}
return false;
}
};
}
class MyFlingListener extends SimpleOnGestureListener {
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
startActivity(new Intent(Hypertension.this, Green.class));
return false;
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (flingDetector.onTouchEvent(event))
return true;
else
return false;
}
}
My understanding is that when the screen is touched, onTouchEvent is called. In the above code, this method is overridden to call flingDetector, which listens for a fling. If one is heard, it launches the activity.
As of now, however, nothing happens when I perform a fling in the emulator. Also, I am fairly confused as to what the return values of the various boolean methods actually represent.
You have two onTouchEvent methods in your code. One is in the GestureDetector class (not overridden), and the other is in your Hypertension activity class (which you have overridden at the bottom).
When someone triggers the TouchEvent in the main activity you explicitly calling the GestureDetector one (passing down the event) here:
if (flingDetector.onTouchEvent(event)) return true;
But if you haven't overridden the onTouchEvent method of that class then nothing is going to happen!
The purpose of overriding these "onSomething()" methods is so that they will get called automatically when an event triggers. In general the way to work with listeners is as follows:
Create a subclass of the Listener class for the event and override its "onEvent()" method to do something when the event is triggered
Call the "setListener( Listener)" method of the object you want to trigger said events after it has been initialized--passing in your previously created Listener
Sit back and watch :)
For all event listeners that return a boolean it should return true when it handles the event, so in your example if the flingDetector handles the onTouchEvent it returns true.
This question has been posed before and there are some great answers there.

Categories

Resources