i want to catch double click on textview for that i have used below code
but it still not working :(
TextView txtOne;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtOne = (TextView) findViewById(R.id.txtOne);
txtOne.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("DRAG");
return gestureDetector.onTouchEvent(event);
}
});
}
final GestureDetector gestureDetector = new GestureDetector(
new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onDoubleTap(MotionEvent e) {
System.out.println("Double Tap");
return super.onDoubleTap(e);
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
System.out.println("One Click");
return super.onSingleTapUp(e);
}
});
only drag is calling but not "Double Tap" and "One Click" never called
Try following steps.
Step 1
Write following code in your activity.
// initialize the Gesture Detector
gd = new GestureDetector(this,new OnGestureListener() {
#Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
});
// set the on Double tap listener
gd.setOnDoubleTapListener(new OnDoubleTapListener() {
#Override
public boolean onDoubleTap(MotionEvent e) {
Toast.makeText(SplashActivity.this,"Double Tap",Toast.LENGTH_LONG).show();
return false;
}
#Override
public boolean onDoubleTapEvent(MotionEvent e) {
// if the second tap hadn't been released and it's being moved
return false;
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
});
Step 2
Write following code for activity. Here gd will be GestureDetector object.
txt.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
gd.onTouchEvent(event);
return false;
}
});
Instead of:
GestureDetector.SimpleOnGestureListener()
Try:
GestureDetector.OnDoubleTapListener()
For SimpleGestureDetector, you need to override onDown() and return true to trigger double tap detector.
Whether or not you use GestureDetector.OnGestureListener, it's best practice to implement an onDown() method that returns true. This is because all gestures begin with an onDown() message. If you return false from onDown(), as GestureDetector.SimpleOnGestureListener does by default, the system assumes that you want to ignore the rest of the gesture, and the other methods of GestureDetector.OnGestureListener never get called. This has the potential to cause unexpected problems in your app. The only time you should return false from onDown() is if you truly want to ignore an entire gesture.
Source: http://developer.android.com/training/gestures/detector.html
This is a good site for performing double click... I used it and worked
doubleCLICK
Related
I am curently working on a GUI and when clicking a certain button:
settingsB.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Settings");
}
});
Instead of just printing it there should a new window open where you can adjust settings with sliders textboxes, ... How would you approach that? With new classes or all inside the action listener?
Edit:
My "current solution" is based on the great suggestion from user Holger to call a method showSettingsDialogue():
private void showSettingsDialogue() {
if(settingsOpen) {
return;
}
else {
settingsOpen = true;
settingWindow = new SettingsWindow();
settingWindow.addWindowListener(new WindowListener() {
#Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
#Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
#Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
#Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
#Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
}
#Override
public void windowClosed(WindowEvent e) {
settingsOpen = false;
}
#Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
});
}
Unfortunally with this way I have to write down even unimplemented methods. Any bether solution then this?
I made a game and used an ads company on it (StartApp). I wanna use Interstitial Ads on it. I followed the step they show but can't achieve it. I'm getting error. Here is the LogCat:
http://i.stack.imgur.com/SaIXE.png
Here is the codes:
GameActivity.java
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws IOException
{
StartAppAd.init(this, "10254544", "20454573");
}
MenuScene.java
private StartAppAd startAppAd = new StartAppAd(activity);
#Override
public void createScene() {
startAppAd.showAd();
startAppAd.loadAd();
}
Check the Manifest.xml file. Be sure you wrote the right package name in there.
Try This code and u should update manifest file also
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StartAppSearch.showSearchBox(this);
StartAppAd.init(this, "107181003", "211487617");
StartAppSearch.init(this, "107181003", "211487617");
btnnext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startAppAd.showAd(); // show the ad
startAppAd.loadAd(); //load next add
Intent intent=new Intent(getApplicationContext(),Second.class);
startActivity(intent);
}
});
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
boolean _active = true;
}
return true;
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
startAppAd.onPause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
startAppAd.onResume();
}
#Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
private StartAppAd startAppAd = new StartAppAd(this);
#Override
public void onBackPressed() {
startAppAd.onBackPressed();
super.onBackPressed();
}
and update in manifest file also
You can not use the
startAppAd.showAd();
startAppAd.loadAd();
inside the Scene class (MenuScene). Instead you can try below method.
In GameActivity class you must be having KeyEvent method. put the code to show the startApp ads in there as below,
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
if(SceneManager.getInstance().getCurrentSceneType().toString() == "SCENE_MENU"){
try{
startAppAd.showAd(); // show the ad
startAppAd.loadAd(); // load the next ad
}catch(Exception e){
}
}
try{
SceneManager.getInstance().getCurrentScene().onBackKeyPressed();
}catch(Exception e){
}
}
return false;
}
Regards,
Deepak
I would like to get user location when I tap/longpress on the screen using the mapforge API...
this code is not fired even if I longpressed on the screen.
mapView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
toastLong("onlongclick");
return false;
}
});
With Little Modify
mapView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Long Clicked is working ", Toast.LENGTH_SHORT).show();
return true;
}
});
How do I set a button to listen for a long press event, where the button is on a view which is already using gesture detector to listen to onFling event?
I have added the code for reference. Please explain how I can implement multiple listeners in an activity. Also, can multiple listeners be implemented for a single view? One of which is a long press listener while the other is a onfling listener
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.knowledge);
summary =(TextView)findViewById(R.id.textSummary);
buckview = (View) findViewById(R.id.bucketView);
ball = (View)findViewById(R.id.meatBall);
gestureDetector = new GestureDetector(new MyGestureDetector());
View mainview = (View) findViewById(R.id.mainView);
/*
//Set touch release listener for bucket view
mainview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_UP)
{
buckview.setVisibility(RelativeLayout.GONE);
}
return false;
}
});
*/
// Set the touch listener for the main view to be our custom gesture listener
mainview.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
});
ball.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
buckview.setVisibility(ImageView.VISIBLE);
return false;
}
});
ball.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction()==MotionEvent.ACTION_UP)
{
buckview.setVisibility(ImageView.GONE);
return false;
}
else
return true;
}
});
}
class MyGestureDetector extends SimpleOnGestureListener {
public void onShowPress(MotionEvent event){
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Intent intent = new Intent(Knowledge.this.getBaseContext(), Knowledge.class);
/* If only horizontal allowed
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
return false;
}
*/
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
startActivity(intent);
Knowledge.this.overridePendingTransition(
R.anim.slide_in_right,
R.anim.slide_out_left
);
return false;// left to right swipe
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
startActivity(intent);
Knowledge.this.overridePendingTransition(
R.anim.slide_in_left,
R.anim.slide_out_right
);
return false;// top to bottom swipe
} else if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
startActivity(intent);
Knowledge.this.overridePendingTransition(
R.anim.slide_in_top,
R.anim.slide_out_bottom
);
return false;// bottom to top swipe
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
startActivity(intent);
Knowledge.this.overridePendingTransition(
R.anim.slide_in_bottom,
R.anim.slide_out_top
);
return false;
}else
return true;
}
// It is necessary to return true from onDown for the onFling event to register
#Override
public boolean onDown(MotionEvent event) {
return true;
}
}
}
Simply try
button.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
return false;
}
})
It will work perfectly untill you return true from onLongClick. If you return ture from here then setOnLongClickListener only get invoked. If you return false all Listeners belong to this button will get invoked. In your case both Listensers will get invoked whether you made setOnLongClickListener or setOnTouchListener.
I hope this will help you.
I have the following code:
public boolean stopped = false;
public void fadeOut(final ImageView obj, final int time, int delay)
{
if(stopped)
{
stopped = false;
anim = new AlphaAnimation(1.00f, 0.00f);
anim.setDuration(time);
if(delay > 0)
{
anim.setStartOffset(delay);
}
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
Log.v("FADER", "Fading out. Duration: " + time + "ms.");
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
Log.v("FADER", "Fading out finished.");
//obj.setAlpha(255);
stopped = true;
}
});
obj.startAnimation(anim);
}
}
and this code works fine. My object (an ImageView) fades out beautifully. But when I run this:
public void fadeIn(final ImageView obj, final int time, int delay)
{
if(stopped)
{
stopped = false;
anim = new AlphaAnimation(0, 1);
anim.setDuration(time);
if(delay > 0)
{
anim.setStartOffset(delay);
}
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
Log.v("FADER", "Fading in. Duration: " + time + "ms.");
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
Log.v("FADER", "Fading in finished.");
//obj.setAlpha(255);
stopped = true;
}
});
obj.startAnimation(anim);
}
}
It doesn't work - there is no fading in. The ImageView stays completely transparent.
Why is this, any ideas?
And for those who are wondering, yes, I have declared and set the boolean stopped, so it's not the issue, because when I look at LogCat, it's printing the text as it runs the fadeIn() function.
Solved!
Turns out, if you use [ImageView Object].setAlpha, and set it to 0 for instance, then when you run AlphaAnimation, it works between the boundaries of 0 and the current alpha of the image.
So, if you want to keep the image invisible after fadeout, the solution is to use [ImageView Object].setVisibility(View.INVISIBLE), and just set it back to View.VISIBLE before you run your animation.
Mission Accomplished.
Is your view's visibility set to View.VISIBLE?