7

Can someone help me with my Syntax? I am dynamically adding some views(custom view) to a FrameLayout that is already defined in XML. those custom view's are different type or same type. I'm able to add views to the screen but unable delete a particular view (either it is same type or different) from ViewGroup. those custom view having onTouch().

Here i'm facing problem : unable to trigger for long click, always takes touch listener

I have to create two options here if user selects a view when long click on that

  1. change background color
  2. delete view.

EDIT : according to tao suggestion i am able to get index of long pressed view if there is no touch listener's to view. but i have to implement touch listeners along with long press...

How can I do this?

RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166

5 Answers5

3

if each one of your views have a delete button or something like that, you can do that:

yourLayout.setTag(view);

on your delete part:

yourLayout.setOnLongClickListener(new Button.OnLongClickListener() {
        public boolean onLongClick(View view) {
...
...
yourLayout.removeView((View) view.getTag());
return _value;
}

i hope it helps you.

yiddow
  • 121
  • 1
  • 8
  • ok then you can change the part that written yourDeleteButton to yourLayout. by the way sorry for that i had written the codes for normal click, not for long click but as you know it's approximetly same things =) – yiddow Jul 11 '12 at 12:38
  • i was edited my question, you got my requirement clearly, if not let me know i will explain it.. – RajaReddy PolamReddy Jul 11 '12 at 13:00
  • now, as i understand, your problem is not about to delete your view, it's about when long click to layout, you want to be opened something like menu. Is it? – yiddow Jul 11 '12 at 13:08
  • hmm i have no idea about how can you find long clicked view to menuitem. but may be you can try put invisible buttons(change back color, delete) to each one of the views, then you long click to view, this buttons will be shown. Then you can get the view which have long clicked as i wrote to dode. But it's just a suggesstion. i don't want to tamper your interface =) . – yiddow Jul 11 '12 at 13:24
3

Ok, your problem is "unable to trigger for long click, always takes touch listener", but this is not enought. I need more details:

  1. which view you supposed to handle the long click, parent view or children view?
  2. which listener you used to handle long click, android.view.View.setOnLongClickListener or android.view.GestureDetector?

actually I done the same job last week. My experiences is: do not using android.view.View.setOnLongClickListener neither android.view.GestureDetector, handle the long click on parent view by yourself. View.java is a good example.


EDIT:

I dont's have compiler on my hand, so I just typing the pseudo-code which will handle the long press by self, for the real-code, the View.java will give you the best answer.

Firstly, you need a runnable to implement your action

class CheckForLongPress implements Runnable {
    public void run() {
        if (getParent() != null) {
            // show toast
        }
    }
}

Secondly, modify your onTouchEvent to detecting long press

boolean onTouchEvent(...) {
    switch (event.getAction()) {
        case MotionEvent.ACITON_DOWN:
            // post a delayed runnable to detecting long press action.
            // here mPendingCheckForLongPress is instance of CheckForLongPress
            postDelayed(mPendingCheckForLongPress, ViewConfiguration.getLongPressTimeout());
            ...
            break;

        case MotionEvent.ACTION_MOVE:
            // cancel long press action
            if (distance(event, lastMotionEvent) > mTouchSlop) {
                removeCallbacks(mPendingCheckForLongPress);
            }

            ...

            break;

        case MotionEvent.ACTION_UP:
            // cancel long press action
            removeCallbacks(mPendingCheckForLongPress);

            ...

            break;

EDIT again:

following is real code, not pseudo one, which is very simple and shows how to handle long press in View.onTouchEvent(), may it would be help.

public class ItemView extends View {

    public ItemView(Context context) {
        super(context);
    }   

    public ItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    Runnable mLongPressDetector = new Runnable() {
            public void run() {
                Toast.makeText(getContext(), "Hello long press", Toast.LENGTH_SHORT).show();
            }
        };

    MotionEvent mLastEvent;

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        mLastEvent = MotionEvent.obtain(event);

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            postDelayed(mLongPressDetector, ViewConfiguration.getLongPressTimeout());
            break;

        case MotionEvent.ACTION_MOVE:
            final int slop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
            if (Math.abs(mLastEvent.getX() - event.getX()) > slop ||
                Math.abs(mLastEvent.getY() - event.getY()) > slop) {
                removeCallbacks(mLongPressDetector);
            }
            break;

        case MotionEvent.ACTION_UP:
            removeCallbacks(mLongPressDetector);
            break;

        }
        return true;
    }

}
tao
  • 299
  • 2
  • 8
  • I want to handle the long click for children view. present i am using android.view.View.setOnLongClickListener. i want to get the index of that view also.. – RajaReddy PolamReddy Jul 13 '12 at 03:31
  • I'd write a sample, and the long-press-detection works well. you would take a look: https://gist.github.com/3108898 – tao Jul 14 '12 at 02:38
  • How can i call this long click from main activity. – RajaReddy PolamReddy Jul 14 '12 at 04:50
  • Just add the custom widget into your layout, and I'd update the https://gist.github.com/3108898 , please check. – tao Jul 14 '12 at 06:30
  • "not working" means that the toast doesn't shows? It's weird, because the code works fine on my phone. – tao Jul 14 '12 at 06:43
  • i am not getting any Toast messages i am using this code http://pastebin.com/X3XyxevX and i am adding view to layout dynamically like this frame.addView(td); – RajaReddy PolamReddy Jul 14 '12 at 06:52
  • I'd checked your code via http://pastebin.com/X3XyxevX. The root cause of your view ThreePointDraw can't handle long press is the override of onTouchEvent(). briefly speaking, View.onTouchEvent has the ability to detecting and launching the long-press-callback. So, if you override it, you will lose the ability. so you got 2 options: 1. don't override the onTouchEvent. 2. override the onTouchEvent, and do the long-press detecting and launch work by self. – tao Jul 14 '12 at 14:10
  • Then how can i solve this problem, can you suggest me how to achieve this requirement .... – RajaReddy PolamReddy Jul 14 '12 at 17:04
  • i did code like what u said, but long press triggered even i am Touch event case MotionEvent.ACTION_MOVE in this also.. – RajaReddy PolamReddy Jul 16 '12 at 05:44
  • what do you want to me post, check this link this is the full source code for my custom view.http://pastebin.com/X3XyxevX – RajaReddy PolamReddy Jul 17 '12 at 11:54
  • due to cannot compile your code, I'm writing a minimal long-press-detecting View, you can take is as reference. – tao Jul 19 '12 at 13:29
  • i was created like this, final Runnable mLongPressDetector = new Runnable() { public void run() { if (getParent() != null) { Log.i(TAG, "long press detected for 3 point"); } } }; still no luck.. – RajaReddy PolamReddy Jul 21 '12 at 04:27
  • that's weird. you've tried my ItemView? how about it working? – tao Jul 21 '12 at 07:11
  • no i was tested this in my code only, you can check here http://pastebin.com/9xHEtHSz – RajaReddy PolamReddy Jul 21 '12 at 07:16
1

Look here:

http://developer.android.com/reference/android/view/View.html

there is a method that int getId(). so every view has an unique idetifier. you can use it for ever view.

bmavus
  • 892
  • 1
  • 7
  • 21
1

Here my question is how can i identify the selected view from the group of views when user touch on that view.

I assume TwoPointsDraw & OnePointDraw is extending View.

So in this case what you can do is after creating object of TwoPointsDraw.assign unique Id or Tag to it.

TwoPointsDraw drawView = new TwoPointsDraw(context);
drawView.setTag("unique identifier"); <-Must be object type
drawView.setId(unique id); <-must be integer type

And when you will click particular view.You can check its identity using.

view.getTag() or view.getId()

Snippet should look like

circle.setOnLongClickListener(new OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        Log.i("Long", v.getTag().toString());
        return false;
    }
});

Hope this helps you.

Vipul
  • 27,808
  • 7
  • 60
  • 75
1

For your OnLongClickListener to work, you should return false when you detect a long click in your onTouch function!

For example:

In your GestureDetector, to give a chance for your onLongClickListener you should:

@Override
public boolean onDown(MotionEvent ev) {
    return false;
    // return super.onTouchEvent(ev);
}

Otherwise:

In your Views, implement the setOnLongClickListener function and save the onLongClickListener passed instance. Whenever a long click is detected, call the instance's onLongClick function.

Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106