Android apps how to fire long press event in video view - java

I have survey about this subject in a day.
What I mean is how to show toast when tapping the Videoview for a while.
Below is what I've found,
Android: Why can't I give an onClickListener to a VideoView?
detect double tap (Double click) or long click in a videoview
But these really can't solve my problem.I really don't know what has happend?
And is there any function can fire up long pressing event in the video view?
here's my code
these two event really can't work.
mVideoView.setOnLongClickListener(new OnLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
Log.e("devon","onitemlongclick");
return true;
}
#Override
public boolean onLongClick(View v) {
Log.e("devon","onLongClick");
return true;
}
});
need help !!!thanks!

add OnLongClickListener in your setupViewComponent call
try using onTouch
try attaching the OnLongClickListener to the videoview's surface
try wrapping the videoview with a transparent imageview/something that grabs focus, and use that as your 'touching pad'
post logcat.

This is a sample example on how to create your own TouchListsners for managing Click and LongClick on VideoView. In this example I pass to the listeners the idex of the data clicked and the index of the view (in behind I have several VideoView in arrays maps to data list, like in an Adapter)
/**
* Simple OnTouchListenerIndexed with Indexes for VideoView ClickListeners
* You have to handle longClick and click by yourself
*/
private abstract class OnTouchListenerIndexed implements OnTouchListener {
private static final int LONG_CLICK_DURATION=600;//in millis
int dataIndex = INDEX_NOT_DEFINED;
int imageViewIndex = INDEX_NOT_DEFINED;
long timeActionDown;
AtomicBoolean stillNotConsumed=new AtomicBoolean(true);
AtomicBoolean actionDone=new AtomicBoolean(false);
public OnTouchListenerIndexed(int dataIndex, int imageViewIndex) {
this.dataIndex = dataIndex;
this.imageViewIndex = imageViewIndex;
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
timeActionDown=System.currentTimeMillis();
stillNotConsumed.set(true);
actionDone.set(false);
//launch LongClick in 1s
v.postDelayed(new Runnable() {
#Override
public void run() {
if(stillNotConsumed.get()){
stillNotConsumed.set(false);
actionDone.set(true);
onLongTouch(dataIndex, imageViewIndex);
}
}
},LONG_CLICK_DURATION);
//consumed
return true;
}else if(event.getAction() == MotionEvent.ACTION_UP){
long timeActionUp=System.currentTimeMillis();
stillNotConsumed.set(false);
if(actionDone.get()){
//do nothing
return true;//you have consumed it
}else {
actionDone.set(true);
//Check Click or LongClick
if (timeActionUp - timeActionDown > LONG_CLICK_DURATION) {
//une seconde plus tard
return onLongTouch(dataIndex, imageViewIndex);
} else {
return onTouch(dataIndex, imageViewIndex);
}
}
}else{
//don't consume it
return false;
}
}
public abstract boolean onTouch(int dataIndex, int imageViewIndex);
public abstract boolean onLongTouch(int dataIndex, int imageViewIndex);
}

Related

ordered: Then, activate the button .. or after, activate the button

This is the work flow.
When selecting the music, I want it
to play it, and if I select it on another, it will reset and stop the
clip that was playing, then it will move to the other and play it.
The question: Simply these are three codes. I want to activate the
latter after the previous one is activated.
Note: I succeeded in adding the command to press the play button But If I delete the first code (reset&stop) that plays by simply selecting it on
the list, But it never stops playing music and piles on top of each other nonstop
sound.reset();
sound.stop();
btn_play.callOnClick();
The code part is complete
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
view.setSelected(true);
sound.reset();
sound.stop();
btn_play.callOnClick();
sound = MediaPlayer.create(SoundActivity.this, listitems.get(i).sound);
tvTitle.setText(listitems.get(i).getTitle());
SoundTime();
}
});
final boolean[] isPlay = {false};
btn_play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!sound.isPlaying()) {
Thread updateSeekBar;
updateSeekBar = new Thread() {
#Override
public void run() {
int SoundDuration = sound.getDuration();
int currentPostion = 0;
seekBar.setMax(SoundDuration);
while (currentPostion < SoundDuration) {
try {
sleep(100);
currentPostion = sound.getCurrentPosition();
seekBar.setProgress(currentPostion);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
sound.start();
updateSeekBar.start();
}
Thanks to those who tried to help me. I found a very simple solution
after effort and effort:>
I moved (btn_play.callOnClick) down like this
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
view.setSelected(true);
sound.stop();
sound.reset();
sound = MediaPlayer.create(SoundActivity.this, listitems.get(i).sound);
tvTitle.setText(listitems.get(i).getTitle());
SoundTime();
btn_play.callOnClick();
}
});
Now it remains for me to auto-switch to the next music

How can I make a long click listener for an AppWidgetHostView

I'm making a launcher and I am stuck on making a long click listener for the widgets. I made a class that extends AppWidgetHost and another that extends AppWidgetHostView. They intercept the touch event and if it's action up it looks and sees if the action down lasted for 400L. It works ok unless there is no button on the widget. For example, the clock widget can not be long pressed.
Here is the implementation of the longClickListener on the host view:
hostView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
new AlertDialog.Builder(WidgetEdge.this)
.setTitle("Options")
.setMessage("Do you want to delete or resize widget?")
.setIcon(android.R.drawable.sym_def_app_icon)
.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int whichButton) {
removeWidget(hostView);
Toast.makeText(WidgetEdge.this, "Widget Deleted", Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("Resize", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
resizeView(hostView);
}
}).show();
return false;
}
});
Here is the AppWidgetHostView class:
public class LauncherAppWidgetHostView extends AppWidgetHostView{
private LayoutInflater mInflater;
WidgetEdge context;
private OnLongClickListener longClick;
private long down;
public LauncherAppWidgetHostView(Context context) {
super(context);
this.context = (WidgetEdge) context;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public void setOnLongClickListener(OnLongClickListener l) {
this.longClick = l;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean trueOrFalse = false;
switch(ev.getAction()) {
case MotionEvent.ACTION_DOWN:
down = System.currentTimeMillis();
this.getParent().requestDisallowInterceptTouchEvent(true);
trueOrFalse = false;
break;
case MotionEvent.ACTION_UP:
boolean upVal = System.currentTimeMillis() - down > 400L;
if( upVal ) {
longClick.onLongClick(LauncherAppWidgetHostView.this);
trueOrFalse = true;
}
break;
}
return trueOrFalse;
}
#Override
protected View getErrorView() {
return mInflater.inflate(R.layout.appwidget_error, this, false);
}
}
Here is the AppWidgetHost:
import android.appwidget.AppWidgetHost;
import android.appwidget.AppWidgetHostView;
import android.appwidget.AppWidgetProviderInfo;
import android.content.Context;
class LauncherAppWidgetHost extends AppWidgetHost {
LauncherAppWidgetHost(Context context, int hostId) {
super(context, hostId);
}
#Override
protected AppWidgetHostView onCreateView(Context context, int appWidgetId,
AppWidgetProviderInfo appWidget) {
return new LauncherAppWidgetHostView(context);
}
#Override
public void stopListening() {
super.stopListening();
clearViews();
}
}
I have tried using the code from this link but when I tested on the clock widget it launches the onLongClickListener twice. Also when the widget is scrolled, without a long press, it would also launch the onLongClick. Thank you for any help.
--UPDATE--
I was using the debugger and found out that when using the clock widget the only event intercepted was the first ACTION_DOWN. After that it never picked up the ACTION_UP.
If the widget doesn't behave like a button (so when it can't be clicked) you need to do something more advanced to detect the long click.
You can have a look at https://github.com/willli666/Android-Trebuchet-Launcher-Standalone/blob/master/src/com/cyanogenmod/trebuchet/LauncherAppWidgetHostView.java
If the Apache licence works for your project, you can copy-paste the whole file, you just need to remove getErrorView() and the inflater and you're good to go.
The idea is to start a timeout when detecting the initial ACTION_DOWN event, and when the timeout triggers, if the view still has focus, then you can performLongClick().
It's much harder to accomplish than one would expect, but at least this works on all widgets, even those that can't be clicked.

How to get click, double tap, and long click gestures for a view inside a gridview?

I have ImageViews inside of a GridView, I had been using an OnItemClickListener along with an OnItemLongClickListener set on the GridView to open the image on a larger page and to delete the item respectively. Now, I have to implement rearranging of the ImageViews in the GridView, so I plan to move the deletion function to a double tap gesture, (please do not lecture me on android style guidelines (including the possibility of contextual actionbars, which I suggested), as this is what my boss asks for to emulate functions inside our ios app) in order to reserve long click for the drag and drop. I set an OnTouchListener on each view in the getView of my custom adapter, feeding a GestureDetecter with a listener extending SimpleOnGestureListener the given MotionEvent with onTouchEvent. I know what to do up to that point, but when I included (onDown of course, to get other callbacks) onDoubleTap, onSingleTapConfirmed, and onLongPressed all taps were interpreted as long clicks. And when I removed the both callback methods to be replaced with their listener counterparts once again (ie OnItemClickListeners) I received those two gestures but not the double tap, which makes sense, as double taps start out as a single tap unless you wait for a bit less than a second to confirm them as singles rather than potential doubles. I also tried placing the OnItemClickListener, but not the OnItemLongClickListener, with the callback in the extended SimpleOnGestureListener. In this case, only long presses were ever interpreted, but other gestures caused no response. Here is my code as it stands now, and do note that I returned false in the onTouchEvent in order to allow others (itemclicklisteners) to consume the events following the attempts made in the GestureDetector.
public class MainBoardGridAdapter extends GenericBoardGridAdapter implements OnItemLongClickListener {
private class Ges extends GestureDetector.SimpleOnGestureListener {
int pos;
public Ges(View v) {
pos = (Integer) v.getTag();
}
#Override
public boolean onDown(MotionEvent me) {
//this does get called but none of these methods below
return true;
}
#Override
public boolean onDoubleTap(MotionEvent me) {
new DeleteConfirmationPrompt(c, "board") {
#Override
protected boolean onDeleteConfirmed() {
// delete the visionboard
return deleteBoard(pos);
}
}; // Constructor shows dialog
return false;
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
MainBoardGridAdapter.super.flagForUpdate(pos);
if (listener != null) {
listener.onBoardClick(pos, getName(pos));
} else {
Intent intent = new Intent(c, VisionBoardActivity.class);
intent.putExtra(VisionBoardActivity.EXTRA_VISION_BOARD_NAME, getName(pos));
frag.startActivityForResult(intent, MyBoardsFragment.REQUEST_EDIT);
}
return false;
}
}
#Override
public boolean onItemLongClick(AdapterView<?> parent, View v,
final int pos, long id) {
Toast.makeText(c, "Long", Toast.LENGTH_LONG).show();
return false;
}
// called by getView of extended adapter
#Override
public void onImageLoaded(ImageView iv, String data, View root) {
iv.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
(new GestureDetector(c, (new Ges(v)))).onTouchEvent(event);
return false;
}
});
}
}
And in the Activity, gv is my GridView:
gv.setOnItemLongClickListener(gridAdapter);
Also note that I had been using true in the return value in the GestureDetector methods, until trying the current configuration.There was no difference to be seen.
Thank you for your valuable time and help, I hope that someone will be able to point out what I am doing incorrectly.
-Jackson

How to determine a long touch on android?

I am looking for a way for when a user long touches a mapview (lets say for 1000ms) that i can some how do a certain action.
How would i go about judging how long a user long touches a mapsview(or any view).
It would be similar to android google maps app, when you long touch, it brings up a balloon overlay item.
Edit added
mapView.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
Toast.makeText(mapView.getContext(), "Hello 123", 2000);
return false;
}
});
the above does not work... any ideas why?
Edit added
This is what i am trying at the moment, but it does not seem to work, even if i only press on the phone, it says the event is an action_move,
i am using an inner class in my MapActivity
private long startTime=0;
private long endTime=0;
class MapOverlay extends Overlay {
#Override
public boolean onTouchEvent(MotionEvent ev, MapView mapView) {
if(ev.getAction() == MotionEvent.ACTION_DOWN){
//record the start time
startTime = ev.getEventTime();
Log.d("LC", "IN DOWN");
}else if(ev.getAction() == MotionEvent.ACTION_UP){
//record the end time
endTime = ev.getEventTime();
Log.d("LC", "IN UP");
}else if(ev.getAction() == MotionEvent.ACTION_MOVE){
Log.d("LC", "IN move");
endTime=0;
}
//verify
if(endTime - startTime > 1000){
//we have a 1000ms duration touch
//propagate your own event
Log.d("LC", "time touched greater than 1000ms");
Toast.makeText(getBaseContext(), "Hello 123", Toast.LENGTH_SHORT).show();
startTime=0;
endTime=0;
return true; //notify that you handled this event (do not propagate)
}
return false;//propogate to enable drag
}
}
and here is my error log that does not make any sense to me
06-29 14:29:55.509: DEBUG/LC(7693): IN move
06-29 14:29:56.149: DEBUG/LC(7693): IN UP
06-29 14:29:56.149: DEBUG/LC(7693): 6346707 6349261
06-29 14:29:56.149: DEBUG/LC(7693): time touched greater than 1000ms
the end time should be set to zero...but it is not...any idea why?
This is how do you normally create an onLongClickListener. Try this:
mapView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View arg0) {
Toast.makeText(mapView.getContext(), "Hello 123", 2000);
return false;
}
});
Reference to your edit:
This might be the way to get what you want.
private final Handler handler = new Handler();
private final Runnable runnable = new Runnable() {
public void run() {
checkGlobalVariable();
}
};
// Other init stuff etc...
#Override
public void onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
// Execute your Runnable after 1000 milliseconds = 1 second.
handler.postDelayed(runnable, 1000);
mBooleanIsPressed = true;
}
if(event.getAction() == MotionEvent.ACTION_UP) {
if(mBooleanIsPressed) {
mBooleanIsPressed = false;
handler.removeCallbacks(runnable);
}
}
}
And now you can check with checkGlobalVariable function:
if(mBooleanIsPressed == true)
This is how you can handle this case. Good luck.
Your are probably looking for a normal long click?
You will have to set your view to be long clickable by adding android:longClickable to your views xml, or by calling setLongClickable(true).
Then you can add an OnLongClickListener to the view.
I dont know of a way to determine exactly how long the long click is. But the default long click is the same as the google maps long click that you mentioned.
OnLongClickListener
You can set up a longClickListener and a touchListener. Add a boolean class data member variable longClicked and set it to false initially. This is how you can set the longClickListener.
view.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
longClicked = true;
return false;
}
});
For touchListener
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(longClicked){
//Do whatever you want here!!
longClicked = false;
}
return false;
}
});
It will give you the same effect as google maps long click. Hope it helps.
use System.currentTimeMillis() instead of ev.getEventTime();
When MotionEvent.ACTION_UP, endTime will be set to ev.getEventTime(), this make setting endTime to zero when MotionEvent.ACTION_MOVE be not affect.
Instead of setting endTime to zero, you should set startTime to ev.getEventTime()
This is how I use long and short touches
View.setOnTouchListener(new View.OnTouchListener() {
double firsttouch=0;
boolean isup=false;
int millistotouch=1000;
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction()==MotionEvent.ACTION_DOWN) {
firsttouch = (double) System.currentTimeMillis();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if (!isup) {
//
// Do your long click work
//
}
else
{
firsttouch=0;
isup=false;
}
}
}
}, millistotouch);
return true;
}
if (motionEvent.getAction()==MotionEvent.ACTION_UP) {
if (((double) System.currentTimeMillis()-firsttouch)<millistotouch)
{
isup=true;
//
// Do your short click work
//
}
}
return false;
}
});

Android OnLongClickListener not firing on MapView

I just registered an OnLongClickListener on my my MapView on an Android app I'm currently writing. For some reason however the onLongClick event doesn't fire.
Here's what I've written so far:
public class FriendMapActivity extends MapActivity implements OnLongClickListener {
private static final int CENTER_MAP = Menu.FIRST;
private MapView mapView;
private MapController mapController;
//...
private boolean doCenterMap = true;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.friendmapview);
this.mapView = (MapView) findViewById(R.id.map_view);
this.mapController = mapView.getController();
mapView.setBuiltInZoomControls(true);
mapView.displayZoomControls(true);
mapView.setLongClickable(true);
mapView.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
//NEVER FIRES!!
return false;
}
});
//...
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_3:
mapController.zoomIn();
break;
case KeyEvent.KEYCODE_1:
mapController.zoomOut();
break;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
int actionType = ev.getAction();
switch (actionType) {
case MotionEvent.ACTION_MOVE:
doCenterMap = false;
break;
}
return super.dispatchTouchEvent(ev);
}
...
}
May overlays which I'm adding cause the problem?? Any suggestions?
I ran into the same problem and there is a simple solution to your problem actually; it's because you're using the wrong type of listener.
You should use the OnMapLongClickListener() object from the OnMapLongClickListener interface.
Hopefully everything should work properly :)
Please tell me if it works.
I just ran into this problem. I tried the solution above, but it doesn't quite work 100% in that we want the long press action to fire, even if the user is still holding a finger down.
This is how I implemented a solution, using a handler and a delayed task -
As a side note, I used a similar type implementation, but in reverse, to hide/show zoom controls on touch/etc..
private Handler mHandler = new Handler();
private final Runnable mTask = new Runnable() {
#Override
public void run() {
// your code here
}
};
#Override
public boolean onTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
// record the start time, start the timer
mEventStartTime = ev.getEventTime();
mHandler.postDelayed(mTask, LONG_PRESS_TIME);
} else if (ev.getAction() == MotionEvent.ACTION_UP) {
// record the end time, dont show if not long enough
mEventEndTime = ev.getEventTime();
if (mEventEndTime - mEventStartTime < LONG_PRESS_TIME) {
mHandler.removeCallbacks(mTask);
}
} else {
// moving, panning, etc .. up to you whether you want to
// count this as a long press - reset timing to start from now
mEventStartTime = ev.getEventTime();
mHandler.removeCallbacks(mTask);
mHandler.postDelayed(mTask, LONG_PRESS_TIME);
}
return super.onTouchEvent(ev);
}
In the mean time I found the "solution" (or workaround, call it as you like) by myself. The way I worked through this issue is by using a GestureDetector and forwarding all touch events to that object by implementing an according OnGestureListener interface.
I've posted some code on my blog if anyone is interested:
http://juristr.com/blog/2009/12/mapview-doesnt-fire-onlongclick-event/
Don't ask me why this didn't work by hooking up the OnLongClickListener directly on the MapView. If someone has an explanation let me know :)
UPDATE:
My previously suggested solution using a GestureDetector posed some drawbacks. So I updated the blog post on my site.
In WebView framework code performLongClick() is used to handle long press event, this is how Android copy Text Feature is implemented in Browser, that is why onLongClick is not been fired.

Categories

Resources