Android Double Tap - android

How do I use double tap events in an activity? The double tap event or long click method working (though the method is getting overriden) I've just put in a toast message in each of these methods and yet no result! Can you help?

The easiest way to do a double-tap is to detect it with a GestureDetector. The "trick" is to be sure you delegate the Activity's onTouchEvent to the GestureDetector's onTouchEvent:
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.Toast;
public class MainActivity extends Activity {
private GestureDetector gestureDetector;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onDoubleTap(MotionEvent e) {
Toast.makeText(MainActivity.this, "double tap", Toast.LENGTH_SHORT).show();
return true;
}
});
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event))
return true;
return super.onTouchEvent(event);
}
}

Related

why onInterceptTouchEvent overrides the all touch event

I have a viewpager and several fragments.I extend a ViewPager and implement a my own class.Also i implemented onInterceptTouchEvent method.Now when i swipe it doesn't work.I think onInterceptTouchEvent intercept them. How to handle this?
package com.android.ho;
import com.sph.custom.ViewPagerWithTapGestreDetection;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class PDFActivity extends FragmentActivity implements ImageOnTapCallback,OnPageChangeListener{
static final int ITEMS = 20;
MyAdapter mAdapter;
//ViewPager mPager;
ViewPagerWithTapGestreDetection mPager;
//ViewPager mPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pager);
try {
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPagerWithTapGestreDetection) findViewById(R.id.pager);
mPager.setId(5000);
mPager.setImageOnTapCallback(this);
mPager.setAdapter(mAdapter);
//mPager.setOffscreenPageLimit(1);
mPager.setOnPageChangeListener(this);
/*
Button button = (Button) findViewById(R.id.first);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(0);
}
});
button = (Button) findViewById(R.id.last);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(ITEMS - 1);
}
});
*/
mPager.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
view.getParent().requestDisallowInterceptTouchEvent(true);
Log.e("Inside onTouch onTouch onTouch onTouch onTouch onTouch ", "onTouch");
return false;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
public static class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public int getCount() {
return ITEMS;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0: // Fragment # 0 - This will show image
return MyFragment.newInstance("1.pdf");
case 1: // Fragment # 1 - This will show image
return MyFragment.newInstance("2.pdf");
case 2: // Fragment # 1 - This will show image
return MyFragment.newInstance("3.pdf");
case 3: // Fragment # 1 - This will show image
return MyFragment.newInstance("4.pdf");
case 4: // Fragment # 1 - This will show image
return MyFragment.newInstance("5.pdf");
default:// Fragment # 2-9 - Will show list
return MyFragment.newInstance("1.pdf");
}
}
}
#Override
public void tapDetected() {
// TODO Auto-generated method stub
Log.e("tapDetected", "tapDetected");
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
Log.e("onPageScrollStateChanged", "onPageScrollStateChanged");
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
Log.e("onPageScrolled", "onPageScrolled");
}
#Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
Log.e("onPageSelected", "onPageSelected");
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.e("onDestroy", "onDestroy onDestroy onDestroy");
}
}
This is my fragment class
package com.android.ho;
import java.io.File;
import java.io.InputStream;
import com.epapyrus.plugpdf.core.viewer.ReaderView;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MyFragment extends Fragment{
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
public static int count = 1;
static String file;
TextView txt;
ReaderView readerView;
public static final MyFragment newInstance(String message) {
MyFragment f = new MyFragment();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
file = message;
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.myfragment_layout, container, false);
try {
File fl = new File(Environment.getExternalStorageDirectory(), "/pdf/2.pdf");
readerView = (ReaderView) v.findViewById(R.id.pdfviews);
//readerView.setOnClickListener(this);
Log.e("readerView", fl.toString());
v.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
view.getParent().requestDisallowInterceptTouchEvent(true);
Log.e("Inside onTouch onTouch onTouch onTouch onTouch onTouch ", "onTouch");
return true;
}
});
// readerView.openFile("/mnt/sdcard/pdf/1.pdf","");
if (count > 5)
count = 1;
readerView.openFile("/mnt/sdcard/pdf/" + count + ".pdf", "");
Log.e("Inside onCreateView count is ", "" + count);
count++;
v.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getActivity(), "Cilcked..",Toast.LENGTH_SHORT).show();
}
});
/*v.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
view.getParent().requestDisallowInterceptTouchEvent(true);
Log.e("Inside onTouch onTouch onTouch onTouch onTouch onTouch ", "onTouch");
return false;
}
});*/
} catch (Exception e) {
e.printStackTrace();
}
return v;
}
#Override
public void onDestroyView() {
super.onDestroyView();
// Log.d("PDFVIEW", "onDestroyView");
try {
if (readerView != null) {
readerView.clear();
// readerView.clearAllField(5);
// readerView.destroyDrawingCache();
// readerView.
// readerView=null;
// mPDFView.purgeMemory();
// mPDFView.destroy();//added to force release memory
// mPDFView = null;
}
Log.e("Fragment onDestroyView ", " onDestroyView readerView is "+readerView);
} catch (Exception e) {
e.printStackTrace();
}
}
}
This is my custom viewpager class
package com.android.ho
import java.util.Calendar;
import java.util.Currency;
import java.util.Date;
import com.sph.bh.ImageOnTapCallback;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.widget.Toast;
public class ViewPagerWithTapGestreDetection extends ViewPager {
private ImageOnTapCallback imageOnTapCallback;
private GestureDetector tapGestureDetector;
private OnGestureListener mGestureListener;
public ViewPagerWithTapGestreDetection(Context context) {
super(context);
try {
imageOnTapCallback = null;// set if needed
mGestureListener = new GestureListener();
tapGestureDetector = new GestureDetector(getContext(), mGestureListener, null, true);
} catch (Exception e) {
}
}
public ViewPagerWithTapGestreDetection(Context context, AttributeSet attrs) {
super(context, attrs);
try {
imageOnTapCallback = null;// set if needed
mGestureListener = new GestureListener();
tapGestureDetector = new GestureDetector(getContext(), mGestureListener, null, true);
} catch (Exception e) {
}
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
//Don't let any touches be passed down to the children automatically
Log.d("onInterceptTouchEvent ", "onInterceptTouchEvent");
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("onTouchEvent", "onTouchEvent");
if (imageOnTapCallback != null) {
tapGestureDetector.onTouchEvent(event);
}
return super.onTouchEvent(event);
}
public ImageOnTapCallback getImageOnTapCallback() {
return imageOnTapCallback;
}
public void setImageOnTapCallback(ImageOnTapCallback imageOnTapCallback) {
this.imageOnTapCallback = imageOnTapCallback;
}
public class GestureListener extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.d("onSingleTapConfirmed", "onSingleTapConfirmed");
if (null != imageOnTapCallback) {
imageOnTapCallback.tapDetected();
}
return super.onSingleTapConfirmed(e);
}
#Override
public boolean onDoubleTap(MotionEvent e) {
Log.d("onDoubleTap", "onDoubleTap");
return super.onDoubleTap(e);
}
#Override
public void onLongPress(MotionEvent e) {
Log.d("onLongPress", "onLongPress");
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.d("onScroll", "onScroll");
return super.onScroll(e1, e2, distanceX, distanceY);
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.e("onFling ", "onFling");
return super.onFling(e1, e2, velocityX, velocityY);
}
}
}
I think onInterceptTouchEvent intercept them.
Well, you are returning true from onInterceptTouchEvent(), which means, that as a ViewGroup you do not want any touch event to be passed to children, instead pass that touch event to onTouchEvent().
You should listen for event in onInterceptTouchEvent() return false until you see that this is such a gesture, that shouldn't be handled by children. And on that point only you should return true.

onClick is not called in my viewpager

I have a viewpager with several fragments in the viewpager.I need to handle click event.I have already implement the onTouch event,but its not trigger for click event.When i swiping onTouch is triggering.I don't know why it's not trigger for click event.
This is my viewpager class
package com.android.ho;
import com.sph.custom.ViewPagerWithTapGestreDetection;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class PDFActivity extends FragmentActivity implements ImageOnTapCallback,OnPageChangeListener{
static final int ITEMS = 20;
MyAdapter mAdapter;
//ViewPager mPager;
ViewPagerWithTapGestreDetection mPager;
//ViewPager mPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pager);
try {
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPagerWithTapGestreDetection) findViewById(R.id.pager);
mPager.setId(5000);
mPager.setImageOnTapCallback(this);
mPager.setAdapter(mAdapter);
//mPager.setOffscreenPageLimit(1);
mPager.setOnPageChangeListener(this);
/*
Button button = (Button) findViewById(R.id.first);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(0);
}
});
button = (Button) findViewById(R.id.last);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(ITEMS - 1);
}
});
*/
mPager.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
view.getParent().requestDisallowInterceptTouchEvent(true);
Log.e("Inside onTouch onTouch onTouch onTouch onTouch onTouch ", "onTouch");
return false;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
public static class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public int getCount() {
return ITEMS;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0: // Fragment # 0 - This will show image
return MyFragment.newInstance("1.pdf");
case 1: // Fragment # 1 - This will show image
return MyFragment.newInstance("2.pdf");
case 2: // Fragment # 1 - This will show image
return MyFragment.newInstance("3.pdf");
case 3: // Fragment # 1 - This will show image
return MyFragment.newInstance("4.pdf");
case 4: // Fragment # 1 - This will show image
return MyFragment.newInstance("5.pdf");
default:// Fragment # 2-9 - Will show list
return MyFragment.newInstance("1.pdf");
}
}
}
#Override
public void tapDetected() {
// TODO Auto-generated method stub
Log.e("tapDetected", "tapDetected");
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
Log.e("onPageScrollStateChanged", "onPageScrollStateChanged");
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
Log.e("onPageScrolled", "onPageScrolled");
}
#Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
Log.e("onPageSelected", "onPageSelected");
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.e("onDestroy", "onDestroy onDestroy onDestroy");
}
}
This is my fragment class
package com.android.ho;
import java.io.File;
import java.io.InputStream;
import com.epapyrus.plugpdf.core.viewer.ReaderView;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MyFragment extends Fragment{
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
public static int count = 1;
static String file;
TextView txt;
ReaderView readerView;
public static final MyFragment newInstance(String message) {
MyFragment f = new MyFragment();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
file = message;
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.myfragment_layout, container, false);
try {
File fl = new File(Environment.getExternalStorageDirectory(), "/pdf/2.pdf");
readerView = (ReaderView) v.findViewById(R.id.pdfviews);
//readerView.setOnClickListener(this);
Log.e("readerView", fl.toString());
v.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
view.getParent().requestDisallowInterceptTouchEvent(true);
Log.e("Inside onTouch onTouch onTouch onTouch onTouch onTouch ", "onTouch");
return true;
}
});
// readerView.openFile("/mnt/sdcard/pdf/1.pdf","");
if (count > 5)
count = 1;
readerView.openFile("/mnt/sdcard/pdf/" + count + ".pdf", "");
Log.e("Inside onCreateView count is ", "" + count);
count++;
v.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getActivity(), "Cilcked..",Toast.LENGTH_SHORT).show();
}
});
/*v.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
view.getParent().requestDisallowInterceptTouchEvent(true);
Log.e("Inside onTouch onTouch onTouch onTouch onTouch onTouch ", "onTouch");
return false;
}
});*/
} catch (Exception e) {
e.printStackTrace();
}
return v;
}
#Override
public void onDestroyView() {
super.onDestroyView();
// Log.d("PDFVIEW", "onDestroyView");
try {
if (readerView != null) {
readerView.clear();
// readerView.clearAllField(5);
// readerView.destroyDrawingCache();
// readerView.
// readerView=null;
// mPDFView.purgeMemory();
// mPDFView.destroy();//added to force release memory
// mPDFView = null;
}
Log.e("Fragment onDestroyView ", " onDestroyView readerView is "+readerView);
} catch (Exception e) {
e.printStackTrace();
}
}
}
This is my custom viewpager class
package com.android.ho
import java.util.Calendar;
import java.util.Currency;
import java.util.Date;
import com.sph.bh.ImageOnTapCallback;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.widget.Toast;
public class ViewPagerWithTapGestreDetection extends ViewPager {
private ImageOnTapCallback imageOnTapCallback;
private GestureDetector tapGestureDetector;
private OnGestureListener mGestureListener;
public ViewPagerWithTapGestreDetection(Context context) {
super(context);
try {
imageOnTapCallback = null;// set if needed
mGestureListener = new GestureListener();
tapGestureDetector = new GestureDetector(getContext(), mGestureListener, null, true);
} catch (Exception e) {
}
}
public ViewPagerWithTapGestreDetection(Context context, AttributeSet attrs) {
super(context, attrs);
try {
imageOnTapCallback = null;// set if needed
mGestureListener = new GestureListener();
tapGestureDetector = new GestureDetector(getContext(), mGestureListener, null, true);
} catch (Exception e) {
}
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
//Don't let any touches be passed down to the children automatically
Log.d("onInterceptTouchEvent ", "onInterceptTouchEvent");
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("onTouchEvent", "onTouchEvent");
if (imageOnTapCallback != null) {
tapGestureDetector.onTouchEvent(event);
}
return super.onTouchEvent(event);
}
public ImageOnTapCallback getImageOnTapCallback() {
return imageOnTapCallback;
}
public void setImageOnTapCallback(ImageOnTapCallback imageOnTapCallback) {
this.imageOnTapCallback = imageOnTapCallback;
}
public class GestureListener extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.d("onSingleTapConfirmed", "onSingleTapConfirmed");
if (null != imageOnTapCallback) {
imageOnTapCallback.tapDetected();
}
return super.onSingleTapConfirmed(e);
}
#Override
public boolean onDoubleTap(MotionEvent e) {
Log.d("onDoubleTap", "onDoubleTap");
return super.onDoubleTap(e);
}
#Override
public void onLongPress(MotionEvent e) {
Log.d("onLongPress", "onLongPress");
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.d("onScroll", "onScroll");
return super.onScroll(e1, e2, distanceX, distanceY);
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.e("onFling ", "onFling");
return super.onFling(e1, e2, velocityX, velocityY);
}
}
}
Now when i click it identified by the onInterceptTouchEvent in the ViewPagerWithTapGestreDetection class.So i need to view a Horizontal scrollbar when i click.Is it the correct way to call it inside onInterceptTouchEvent ?

multiple intent's starting by onTouch event

Hey guys i am getting to start an intent if a user clicks on specific location.at first touch he opens a menu and on second he opens the activity.The problem is that many copy's of same intent are started
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.os.Bundle;
import android.text.TextPaint;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Toast;
public class gfx extends Activity implements OnTouchListener{
Bitmap a,b;
gfx1 drw;
String a1;
boolean flag=false,flag1=false,flag2=false;
Canvas c1;
float x=0,y=0,z=0,bitx=0,bity=0;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
drw = new gfx1(this);
drw.setOnTouchListener(this);
setContentView(drw);
}
public class gfx1 extends View implements Runnable {
public gfx1(Context context) {
super(context);
// TODO Auto-generated constructor stub
a = BitmapFactory.decodeResource(getResources(),
R.drawable.greenball);
}
void callin(String a1)
{
Intent inte = new Intent(a1);
startActivity(inte);
}
#Override
protected void onDraw(Canvas c1) {
// TODO Auto-generated method stub
super.onDraw(c1);
c1.drawColor(Color.YELLOW);
b=Bitmap.createScaledBitmap(a,c1.getWidth()/3, c1.getHeight()/3, true);
bitx=(c1.getWidth()/2)-(a.getWidth()/2);
bity=(c1.getHeight()/2)-(a.getHeight()/2);
c1.drawBitmap(a, bitx, bity, null);
Paint textPaint = new Paint();
textPaint.setARGB(50, 254, 10, 50);
textPaint.setTextAlign(Align.CENTER);
textPaint.setTextSize(30);
if(flag)
{
c1.drawText("clicked",300, 300,textPaint);
c1.drawBitmap(b,(c1.getWidth()/2)-(b.getWidth()/2),(c1.getHeight()/2)+(a.getHeight()/2), null);
c1.drawBitmap(b,(c1.getWidth()/2)-(b.getWidth()/2),(c1.getHeight()/2)-(a.getHeight()/2)-(b.getHeight()), null);
}
float bitbx1=(c1.getWidth()/2)-(b.getWidth()/2);
float bitbx2=(c1.getWidth()/2)+(b.getWidth()/2);
float bitby1=(c1.getHeight()/2)-(b.getHeight())-(a.getHeight()/2);
float bitby2=((c1.getHeight()/2)-(a.getHeight()/2));
if(flag2)
{
c1.drawText("Opening...please wait", 600, 600, textPaint);
flag1=true;
if(flag1)
{
a1="com.example.claci.MAINACTIVITY";
callin(a1);
}
flag1=false;
}
if((x>bitx&&x<bitx+(a.getWidth()))&& (y>bity&&y<bity+(a.getHeight())))
{
flag=true;
}
if((x>bitbx1&&x<bitbx2)&&(y>bitby1&&y<bitby2))
{
if(flag)
{
flag2=true;
}
}
invalidate();
}
#Override
public void run() {
// TODO Auto-generated method stub
}
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
x=event.getX();
y=event.getY();
break;
}
return true;
}
}
A much better structure would be to have your menu creation, and subsequent activity creation called from onTouch method. As psink mentioned, that is not the proper use of onDraw, and it unnecessarily links tow very unrelated things.
I would also use two class variables-- one that is a flag for if the menu exists yet, and another that is a reference to the newly created activity. If an activity already exists, you don't create another one. When the existent activity completes, though, it needs to return a result to this activity so you can clear that reference and be ready to create a new one when needed.

Double tap event in android

I need to toast something on double tap the screen. I tried the following code. But it's not working. No toast is coming on double tapping. What is wrong with these code?
package a.b.c;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.Toast;
public class SampleActivity extends Activity implements OnDoubleTapListener,OnGestureListener
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onDoubleTap(MotionEvent e) {
if(e.getAction()==1)
{
Toast.makeText(getBaseContext(), "onDoubleTap", Toast.LENGTH_LONG).show();
}
return true;
}
#Override
public boolean onDoubleTapEvent(MotionEvent e) {
if(e.getAction()==1)
{
Toast.makeText(getBaseContext(), "onDoubleTapEvent", Toast.LENGTH_LONG).show();
}
return true;
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
if(e.getAction()==1)
{
Toast.makeText(getBaseContext(), "onSingleTapConfirmed", Toast.LENGTH_LONG).show();
}
return true;
}
I also implemented the OnGestureListener methodes. But have no effect. Please help.
Where do you set the doubletap listener? Try adding this to your oncreate and see if it fixes the issue
GestureDetector detector = new GestureDetector(this, this);

How to reposition an ImageView

I have some ImageViews in my xml file. In my code section I want to move an image according to my wish. For that, I've done event.getx() and event.getY() and then use imageView.layout(). This process is not working. How can i move that image?
I think this might help:
package com.example.moveimageview;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
ImageView im;
RelativeLayout rl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//getting the wrapper layout from the xml
rl = (RelativeLayout) findViewById(R.id.relative1);
//getting the ImageView from xml
im = (ImageView) findViewById(R.id.myImageView);
rl.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent e) {
// TODO Auto-generated method stub
if(e.getAction()==android.view.MotionEvent.ACTION_DOWN)
{
//sending the new coordinates to the method
//that will change the view's location
setImageViewLocation(e.getX(), e.getY());
return true;
}
return false;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void setImageViewLocation(float x, float y)
{
//setting the coordinates
im.setX(x);
im.setY(y);
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
im.invalidate();//invoking the view onDraw
}
});
}
}
(Edit) If you want to your ImageView around, here's the onTouchListner code for this (Same code as before just replace the OnTouchListner with this):
rl.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent e) {
// TODO Auto-generated method stub
if(e.getAction()==android.view.MotionEvent.ACTION_DOWN)
{
//sending the new coordinates to the method
//that will change the view's location
setImageViewLocation(e.getRawX(), e.getRawY());
return true;
}
if(e.getAction()==android.view.MotionEvent.ACTION_MOVE)
{
setImageViewLocation(e.getRawX(), e.getRawY());
return true;
}
return false;
}
});

Categories

Resources