Android: why does a swipe kill my app? - android

I was trying to make a TextView change when I swipe across the screen. I used GestureDector.OnGestureListener, and just recoded the onFling() methode as follows:
#Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float v, float v2) {
if (motionEvent.getRawY() < motionEvent2.getRawY()) {
((TextView) findViewById(R.id.textView)).setText("Next");
} else if (moveTaskToBack(motionEvent.getRawY() > motionEvent2.getRawY())) {
((TextView) findViewById(R.id.textView)).setText("Previous");
}
return true;
}
But in my case, for a weird reason, when I flick left the app just stops (like I pressed the home button).
What could be causing this?
EDIT: if I reopen the app and press the return button (physical), it show "previous", therefore I could can conclude that the swipe works but something happening after setting the TextView is the culprit.

findViewById(R.id.textView)
This will throw an error if the textView R.id.textView is not on the current content view.
Sometimes, with overrides, you will need to do this:
return super.onFling(motionEvent, motionEvent2, v, v2);

Well, this is awkward. In the else if statement, for some reason, auto-complete probably added the function call:
moveTaskToBack(boolean b)
Which moves the stack to the back of the activity stack.
http://developer.android.com/reference/android/app/Activity.html#moveTaskToBack(boolean)

Related

setOnTouchListener in Transparently Activity

Please Could You give me a bit of help, I have two activities the main activity (A) and the second activity (B) I made the layout background of activity(B) transparently, so now I can see the components of Activity(A) through Activity(B) until now everything is great, but now I want to access to activity(A) components through Activity(B) that have a transparent background, I used the "setOnTouchListener" to get the touch position but I have no idea how to set this position that I get to the Activity(A) and make it act as I touch it directly
my code
relativeLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN){
float x = (int) motionEvent.getX();
float y = (int) motionEvent.getY();
MainActivity m = new MainActivity();
m.simulateTouch(motionEvent);
}
return true;
}
});
Thank you in advance
I don't think that is possible, because when you are in second activity the first activity is paused and even though you can see the elements of first activity you wont be able to access them.
I suggest you to find another way for doing this.

Android - Pressed / Touched item in a ListView

Is it possible to know in a ListView if a item is pressed / touched (but not clicked) and know which?
I tried with "OnTouchListener" but without success (I can intercept UP and Down events on the ListView but not on the elements).
I tried also "OnItemLongClickListener" but I have to wait when I want information immediately.
Do you have an idea ?
Thank you in advance.
Edit (solution) :
I put the following code in my adapter in the item view.
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
image.setImageResource(R.drawable.image2);
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
image.setImageResource(R.drawable.image1);
}
return true;
}
});
And now, when I touch an item, the picture becomes "image2" and when I do not touch the screen, it returns to "image1".
But there is a problem, if I press an item (the image2 appears well) and I move my finger in the list view and I do not touch the screen, it stays on for the image2, "MotionEvent.ACTION_UP" could not execute.
Do you have any idea how to do that as soon as I do not touch the screen, it must return on image1?
Thank you for your help.
You can set SetOnTouchListener to item view when your adapter create it.
you just try yo implement the Listener then you can override the function or please add your rough work part

How to prevent multi-taps in android apps

I use onTouch as it is the best and more modifiable than for an instance onClick. It is a custom view that is being touched that I want to respond with action, and I have allready limited so you can't just touch and it does it over and over. Now I need it to limit the amount of simountanious taps.
EDIT:
#Override
public boolean onTouch(View v, MotionEvent event) {
lines++;
return false;
}
I have prevented constant action, but not the possibility to press with more than 1 finger, so that if you tap with more the rest of the fingers get ignored
There are several ways to do it.
best for you may be limit it inside in your overrided onTouch event.
if(event.getPointerCount() > 1) {
System.out.println("Multitouch detected!");
return true;
}
else
return super.onTouchEvent(event);
Another option is set attribute android:splitMotionEvents = false in your xml file.

Custom View, continues to pass on events?

I have a ListView. Inside the cells, I have a custom view. (You can draw in it.)
When drawing, I turn off scrolling of the list ..
theListView.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_MOVE)
{
if ( STATE.weAreDrawoning )
{
return true;
// so, do not forward and hence do not scroll the list
}
else
{
return false;
}
}
return false;
}
});
That's fine. But strangely, up-down touching in the custom View, is still passed on to the list and makes it scroll.
public class AmazingCustomView extends View
{
blah
#Override
public boolean onTouchEvent(MotionEvent event)
{
blah
return true;
}
}
notice in the custom view onTouchEvent is returning true (I also tried false! :) )
but the motion events appear to be still passed on .. what gives??
Is there another "on .. something" I'm missing in the custom view? Sorry, new to Android and lame. Thanks!
PS, I tried turning on "clickable" on the xml of the custom view, didn't help :O
--
Worse ...
I've just realised ALL controls in the ListView, say buttons, still "pass on scrolling"
I fear the system I use above for turning off scrolling is just no good. :/
important...
For anyone googling to here. The only real way I've found to turn off scrolling on an android listView ...
danosipov.com/?p=604
(don't forget to separately turn off your pull-to-refresh)
You may need to override onInterceptTouchEvent. Its an odd function, documentation here: http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent%28android.view.MotionEvent%29

Scroll gallery to next page

Using a widget.Gallery to display a horizontally scrolling list of items. I've implemented paging in the gallery with what seems to be the standard technique: subclass Gallery and implement:
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (velocityX>0) {
onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, null);
} else {
onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
}
}
When tapping the gallery, I fade up next and previous image buttons. When clicking these, I want the gallery to animated to the next/previous page, respectively. I've tried calling onKeyDown from my next-button handler, but strangely this has no effect.
AbsSpinner has setSelection(int position, boolean animate) but animate is ignored in Gallery.
Exactly what toucan said, but to elaborate further (comments limits are too short):
The problem seems to be the fact that the Gallery doesn't let the user scroll if no children exist in that position yet. scrollToChild() is the culprit when trying to inject the event:
private boolean scrollToChild(int childPosition) {
View child = getChildAt(childPosition);
if (child != null) {
int distance = getCenterOfGallery() - getCenterOfView(child);
mFlingRunnable.startUsingDistance(distance);
return true;
}
return false;
}
Interestingly, if you fling the gallery with your fingers, it will cause the child to be created. Then, if you let go of your finger (going back to the original position), and then press the button that activates the onKeyDown injection, it will work flawlessly - because the child is there.
Unfortunately there's no real solution since everything is private in that class. The only solution is to use setSpacing(-1) or something in the gallery, so left and right children are always created and visible, but just behind the currently selected view.
As a footnote, I'm really baffled as to why everything is private in that class (or in any other of the Android widget classes for that matter). This is one of those things that could easily be fixed with some small code change.
Edit (Aug 2012): For future reference, rather than trying to use a Gallery for this kind of pattern (when you want the user to swipe between different items while only one of them is visible), it's much better to use the Android's compatibility package's ViewPager class. In my opinion, the compatibility package is not as celebrated as it should be (it took me a while to get notice of it). For developers targeting Android 2.x+, it's a godsend.
this is working
create 2 image buttons and gallery
eg:
nextbtn,backbtn and gall
button code is avialable here
nextbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
gall.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
}
});
backbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
gall.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, null);
}
});
String strMethod = velocityX > 0 ? "movePrevious" : "moveNext";
try {
Method method = Gallery.class.getDeclaredMethod(strMethod, null);
method.setAccessible(true);
method.invoke(this, null);
} catch (Exception e) {
e.printStackTrace();
}

Categories

Resources