Disable parts of the touch screen in android - android

So I was trying to disable the screen for an app I am making for a brief period using this
#Override
public boolean onTouchEvent(MotionEvent pMotioneEvent) {
if(pMotioneEvent.getY() < TestSprite.getY()){
return false;
}else{
return true;
}
}
but this seems to have no effect. I read around and it seems like in general its a bad idea to disable the touch screen, but I'm still curious to know if there is a way.
Thanks

You could try
requestDisallowInterceptTouchEvent

If you want to check for touchevents in a certain area of your screen, you might want to put this in a View and set a touchEvent Listener to it.

Related

Is there a way to disable touch?

I have an app which uses geofire to identify if the user enters the designated boundaries and when the it is true, the user's phone will be temporarily disabled in which touch functionality will be disabled.Otherwise, it will be enabled if the user leaves the assigned boundaries.Anyone can help?It would be my pleasure to hear your thoughts.
Thank you!
If you want to disable the current activity then you can dispatchTouchEvent().
Example of using it:
private var inRange = false
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (!inRange)
return super.dispatchTouchEvent(ev);
return true;
}
If that's not what you are looking sorry, can you explain it a bit more?
I think you can make a service that create an transparent overlay then you disable on click for this overlay .. you can see this link for more details about this solution
How do I make my full-screen overlay remain full-screen after orientation changes?

OnSignleTapUp event fires twice

I have a Xamarin Android application with a GestureDetector. On my device (Sony Xperia S5303) and in emulators it works fine. However, on a Samsung Galaxy tablet, the OnSingleTapUp event is fired twice, even though I touch the screen just once. Since the event is checked by the OnSingleTapConfirmed, and the device behaves as if two taps were made, the following method is never called:
public bool OnSingleTapUp(MotionEvent e)
{
if (PersistentContext.LoggedPlayer.Token)
{
if (simpleOnGestureListener.OnSingleTapConfirmed(e))
{
Move(e);
}
return true;
}
return false;
}
The strange thing is, that when clicking on a button, the button method is called just once. Can anybody tell me what might cause this strange behaviour and how to avoid it?
You need to use onSingleTapConfirmed(MotionEvent event) instead. This way android makes sure it's a single tap and doesn't fire the event twice.
Edit: if you are determined to use onSingleTapUp(MotionEvent event), then you can use event.getEventTime()and if the time difference between the first and second events is less than a THRESHOLD like 200ms or something, consider it the unwanted second-time fired tap and ignore it.
Edit 2: Since I don't use Xamarin I might be totally wrong but I think you need to change your code like this: (I mean forget onSingleTapUp completely and move wyour whole code into onSingleTapConfirmed)
#Override
public boolean onSingleTapConfirmed(MotionEvent event) {
if (PersistentContext.LoggedPlayer.Token)
{
Move(e);
return true;
}
return false;
}
Hope it helps and Good Luck!

Intercept Touch events in Android

How do I intercept touch events in Android, ensuring that the existing touch workflow is not impacted. Basically I want to add some touch visualizer so as to know where the user is touching on the screen whereby ensuring that if the user is trying to scroll the tableview, touch visualizer is shown as the user drags his finger but also the tableview scrolls with ease.
In iOS, there is one method sendEvent of class UIWindow does this exactly. Not sure if Android has anything similar.
Thanks
Override Activity.dispatchTouchEvent() and do your touch handling there. Always return super.dispatchTouchEvent() to make sure it gets handled the normal way after your logic executes.
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
/* your code here */
return super.dispatchTouchEvent(event);
}
http://developer.android.com/reference/android/view/View.OnTouchListener.html have alook at this link. It is pretty straight forward. Google it there is lots of example on it.
yourview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});

Stop ListView scroll animation

I have a ListView with about 100 entries. When the user does the "fling" from bottom to top it starts scrolling and keeps on scrolling even when the finger does not touch the display any more.
Is there a way to stop the scrolling animation at this point?
and we lookup the android source code (AbsListView), give it a ACTION_CANCEL touchEvent, can stop the fling. it is easy.
listView.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_CANCEL, 0, 0, 0));
I didn't try the solution of Pompe de velo but since smoothScrollToPosition() is not available for API level less than 8 this didnt work for me.
I agree, changing default behaviour is not a good Idea, but sometimes you need to. So here is my (dirty) solution which uses reflection. This is by far not the recommended way since it's a hack but it works for me. There might be a better solution but I didn't found it.
class StopListFling {
private static Field mFlingEndField = null;
private static Method mFlingEndMethod = null;
static {
try {
mFlingEndField = AbsListView.class.getDeclaredField("mFlingRunnable");
mFlingEndField.setAccessible(true);
mFlingEndMethod = mFlingEndField.getType().getDeclaredMethod("endFling");
mFlingEndMethod.setAccessible(true);
} catch (Exception e) {
mFlingEndMethod = null;
}
}
public static void stop(ListView list) {
if (mFlingEndMethod != null) {
try {
mFlingEndMethod.invoke(mFlingEndField.get(list));
} catch (Exception e) {
}
}
}
}
Well there surely is a way to do it. But the point is more whether or not it is advisable to do it, in my opinion.
The list is a standard Android control that behaves constistently across all applications. So I would be surprised if I found a list that did not behave the same in your application. You can stop the fling by putting your finger back on the screen at any time.
That said, if you want to do extra work, you could subclass the list view and override its on touch method. Best way to know what to do is to get the source code of ListView (ListView in Android 1.6).
You can prevent flinging for ListViews in API 8 by overriding onTouchEvent and calling smoothScrollBy.
#Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_UP:
this.smoothScrollBy(0, 0);
break;
}
return super.onTouchEvent(ev);
}
This takes over from the fling scrolling and scrolls 0px instead.
My opinion is that you shouldn't modify this behaviour, since the fling behaviour is what the user expects.
However, to your question. I haven't tried this but in theory it should work.
Implement an OnScrollListener to your ListView and use the onScrollStateChanged() method to check if the current state is SCROLL_STATE_FLING. After you've determined that the scrolling perfoms by a fling you can get your ListView's first visible position by using the getFirstVisiblePosition() method and from there you can use smoothScrollToPosition() where you put in your getFirstVisiblePosition() value as an argument.
if you what disable default animation from list view just need set id for root (main) layout in xml and call void onClickListener in class for root layout

Android OnLongClickListener strange / unreliable behaviour

I'm currently fighting against the OnLongClickListener on Android Api Lvl 8.
Take this code:
this.webView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
System.out.println("long click");
return true;
}
});
It works perfectly. I can press anywhere on the WebView and the event triggers every time.
Now take a look at this one:
this.webView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
final EditText editText = getUrlTextField();
switch (editText.getVisibility()) {
case View.VISIBLE:
editText.setVisibility(View.GONE);
return true;
case View.GONE:
editText.setVisibility(View.VISIBLE);
return true;
default:
return false;
}
}
});
Assuming the URL EditText components is currently visible, it gets gone from the display and should be shown again when another long click event is triggered.
But if you run this, the event just works once (!) when one performs a long click on any position on the WebView. To make things complicated, the long click works again when it is performed on a link on the website...
Can anyone explain if it is a bug in the sdk and/or if there is a mistake in my thinking how the OnLongClickListener is working?!? :/
EDIT:
I've run now several different scenarios on a Nexus One and come to following conclussion: Changing the layout on runtime more or less kills the OnLongClickListener... I haven't found a way to get it work reliably at all...
I would really appreciate if anyone could give me a hint... I'm at my wits end :(
Personnally, I ended up by re-setting the listener after each relayout.
I've run into this issue as well. It seems that if the view layout changes in a way that child view bounds need to be modified (i.e. TextView is wrap_content width and you set its text to something longer/shorter than it was before), views in the hierarchy will have their onStartTemporaryDetach method called (most likely due to a layout pass, although I haven't dug deep enough to find out for sure). If you look at the source for View that onStartTemporaryDetach ultimately unsets the pressed state of the view.
Changing the views in your layout that will be updated periodically to have bounds that will not change regardless of the value you set, will fix the issue. Although, that is still not awesome.

Categories

Resources