I have done a lot of search over the topic and have even referred the official documentation from android developer website. But still not clear with the concept.
I have read that for implementing the touch gestures i need to use the GestureDetector and MotionEvent packages. But its implementation kept me confusing over the things.
What i simply want is, my layout includes a many of textviews along with two imageviews. I want to detect a double tap on my images and want to start a new fragment activity. In the new fragment activity i want to show the same image in full screen in landscape mode.
I have done ton of reading but it kept me confusing.
Please help.
Thank you
Here is Double Tap Gesture ImageView.
public class CustomImageView extends ImageView {
private Context context;
private GestureListener mGestureListener;
private GestureDetector mGestureDetector;
public CustomImageView(Context context) {
super(context);
sharedConstructing(context);
}
public CustomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
sharedConstructing(context);
}
public CustomImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
sharedConstructing(context);
}
private void sharedConstructing(Context context) {
super.setClickable(true);
this.context = context;
mGestureListener=new GestureListener();
Log.e("Adding", "Listener:::");
mGestureDetector = new GestureDetector(context, mGestureListener, null, true);
setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
mGestureDetector.onTouchEvent(event);
//..my other code logic
invalidate();
return true; // indicate event was handled
}
});
}
public class GestureListener extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onDoubleTap( MotionEvent e ) {
// TODO DoubleTap Comparison
Log.e("onDoubleTap","onDoubleTap");
return true;
}
}
}
Reference Link
Related
On Android phone, I get the information of co-ordinates that, user touch on his/her phone screen or device. How do i then, use/pass these X-Y coordinates to run a specific function of mine, and use it in my code
reference from the post you can get the co-ordinates if the user touched the screen on your app. After you implement that on touch listener then in the if statement MotionEvent.ACTION_UP put this there
float myX = event.getX();
float myY = event.getY();
// now you can then pass myX,myY in your method or function as parameters
if you happen to be targeting Api 14+ then you can use onhover with the same approach as to the first one from that post, instead of using setOnTouchListener usegetWindow().getDecorView().setOnHoverListener() that's about it.
A side note is using the firs one is better because onHover can not be trusted to work,actually it doesn't work to me, thats just btw
hope i answered your question.
You can create custom view by extending android view and overriding onTouchEvent method. For example,
public class NotTouchableWebView extends WebView {
private Context context;
public NotTouchableWebView(Context context) {
super(context);
this.context=context;
}
public NotTouchableWebView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
}
public NotTouchableWebView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context=context;
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public NotTouchableWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
this.context=context;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (some condition){
//your specific function
return false;
}
return super.onTouchEvent(event);
}
}
I created a view that extend RelativeLayout, and I what that, this view to be able to handle touch events. So what I thought about was to implement the OnTouchListener in my custom control, like this:
class MyCustomControl extends RelativeLayout implements View.OnTouchListener {
public MyCustomControl (Context context) {
super(context);
}
public MyCustomControl (Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyCustomControl (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
other_methods;
#Override
public boolean onTouch(View view, MotionEvent event) {
//event handle here.
}
}
It doesn't work but if I do something like this, it does:
MyCustomControl control = (MyCustomControl) LayoutInflater.from(context).inflate(R.layout.my_control, null);
control.setOnTouchListener(control);
What should I do in order to have the touchListener built-in(already defined and activated) in my custom control ?
I know, it's late, but I solved my case by adding
this.setOnTouchListener(this);
to constructor
in my application I have a flipView using the emilsjolander library. It's great, but I can't stop automatically the flipping effect when I open the FlipView. Infact, if I touch the screen the effect stop, but I would stop it after some seconds. Do you know if the library contain a method for it or, alternatively, for simulate the touchEvent after some seconds?
Thank's
I know the answer is so late but it will help others if they stuck in that issue. You can disable automatic flipping on first page by just commenting:
//peakNext(false);
public class MyFlipView extends se.emilsjolander.flipview.FlipView
{
private boolean isEnabled = true;
public MyFlipView(Context context)
{
super(context);
}
public MyFlipView(Context context, AttributeSet attrs)
{
super(context, attrs, 0);
}
public MyFlipView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
if (isEnabled)
return super.onInterceptTouchEvent(ev);
else
return false;
}
public void disable()
{
isEnabled = false;
}
public void enable()
{
isEnabled = true;
}
}
Declare xml as
<"YourPackage".MyFlipView
/>
hi I am developing android application in which i am using one custom frame layout class. Inside that class I am using one drawable and with the help of canvas i m drawing that. I did this in following way :
public class BackgroundContainer extends FrameLayout implements OnTouchListener{
Drawable mShadowedBackground;
public BackgroundContainer(Context context) {
super(context);
init();
}
public BackgroundContainer(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public BackgroundContainer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
mShadowedBackground =
getContext().getResources().getDrawable(R.drawable.actionbar);
}
#Override
public boolean onTouchEvent(MotionEvent event)
{
Log.i("OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO", "OOOOOOOOOOOOOOOOOOOOOO");
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN: {
invalidate();
}
}
return true;
}
#Override
protected void onDraw(Canvas canvas) {
mShadowedBackground.setBounds(getWidth()-150, 0, getWidth(), mOpenAreaHeight);
canvas.save();
canvas.translate(0, mOpenAreaTop);
mShadowedBackground.draw(canvas);
canvas.restore();
}
}
}
Now I am want to listen click even on my drawable. I implement ontouch event but its not working. Am i doing it in wrong way. Need help thank you.
Drawables are not clickable as it is not considered as a view.
Like deepak already said: by implementing the corresponding listener you just provide the behavior what should happen for a specific event. You still need to add the listener :) In your case this would help (in your init()):
setOnTouchListener(this);
I want to add to the ImageView the pinch feature. So I extended the ImageView and I implemented the OnTouchListener interface. The ImageView is clickable. The feature itsel works, but when I use the custom ImageView with the Gallery widget, the gallery does not swipe. If I set focusable and clickable to false, gallery swipe. How can I let they work together?
Code for the custom ImageView
public class Custom6 extends ImageView implements OnTouchListener {
public Custom6(Context context) {
super(context);
setClickable(true);
setScaleType(ScaleType.MATRIX);
setOnTouchListener(this);
}
public Custom6(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
setClickable(true);
setScaleType(ScaleType.MATRIX);
setOnTouchListener(this);
}
public Custom6(Context context, AttributeSet attrs) {
super(context, attrs);
setClickable(true);
setScaleType(ScaleType.MATRIX);
setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event) {
return true;
}
Return false in ontouch lister and then try i hope it will work...