0

thats my xml layout which I am using

<com.example.pinto.myapplication.myworkspace.MyScrollView
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.pinto.myapplication.myworkspace.MyLinearLayout
        android:id="@+id/linearlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
    <com.example.pinto.myapplication.myworkspace.DragLayer
        android:id="@+id/relative_view_drag1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@android:color/holo_blue_dark">

    </com.example.pinto.myapplication.myworkspace.DragLayer>

        <com.example.pinto.myapplication.myworkspace.DragLayer
            android:layout_margin="20dp"
            android:id="@+id/relative_view_drag2"
            android:layout_width="match_parent"
            android:layout_height="600dp"
            android:background="@android:color/holo_orange_dark">
            </com.example.pinto.myapplication.myworkspace.DragLayer>

    </com.example.pinto.myapplication.myworkspace.MyLinearLayout>

</com.example.pinto.myapplication.myworkspace.MyScrollView>

I hope this helps you with the hierarchy and let me know any changes that I need to do the layout that I have used are custom

Deepak
  • 1
  • 3
  • You meant do you want to check whether are you touching inside or outside that particular view? – Jai Sep 28 '16 at 07:20
  • eg. I have two views and I touch down in first view and then move from first view to second view then in the second view i should get touch of second view but I am continuously getting Touch Move of first view – Deepak Sep 28 '16 at 07:50
  • as per your layout hierarchy you can't achieve it because your all child layouts are inside scrollview. So your touch listener of childview will conflict with scrollview. that's why the solution is not working. – Jai Sep 28 '16 at 11:23
  • What's your goal actually? Why you want to detect touch? Give me brief If I could give you other solution – Jai Sep 28 '16 at 11:24
  • okay fine thanks for the help – Deepak Sep 28 '16 at 12:58

3 Answers3

1

You can use the hidden function by giving view.hidden = true else refer to the previously asked questions.

How to get the Touch position in android?

@Override
public boolean onTouchEvent(MotionEvent event) {
    int x = (int)event.getX();
    int y = (int)event.getY();
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_MOVE:
        case MotionEvent.ACTION_UP:
    }
return false;
}
Community
  • 1
  • 1
Guru Teja
  • 127
  • 2
  • 13
  • Take an eg. I have two views and I touch down in first view and then move from first view to second view then in the second view i should get touch of second view but I am continuously getting Touch Move of first view – Deepak Sep 28 '16 at 07:51
1
@Override
    public boolean onTouch(View v, MotionEvent event) {
       float x = event.getX();  // x axis
       float y = event.getY();  //y axis
       return true;
    }
Sam
  • 51
  • 5
  • I have done this......eg. I have two views and I touch down in first view and then move from first view to second view then in the second view i should get touch of second view but I am continuously getting Touch Move of first view – Deepak Sep 28 '16 at 07:51
  • Did you try it: https://github.com/shell-software/viewmover? Also: http://stackoverflow.com/questions/23120807/android-move-view-on-touch-event – Sam Sep 29 '16 at 07:50
0

If you want to recognize specific child layout your figner touching on with the same touch move, you should pass the touch event of your parent layout to your childviews by implementing onTouchEvent for your childviews, you could identify on which child your finger moving on.

Let's say you are having 2 child layout inside one parent

1) Implement the onTouchEvent for your parent layout

parentLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                Log.d("Touch Parent : ",""+event.getAction());
                if(flag){
                     //dispatchTouchEvent method will help us to pass parent's touch event to specific child
                     FirstchildLayout.dispatchTouchEvent(event);
                     SecondchildLayout.dispatchTouchEvent(event);
                     return true;
                }

                return false;
            }
        });

2) Implement onTouchListener for FirstChildLayout and identify whether your finger is moving on it or outside of it?

FirstChildLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                Rect viewRect = new Rect();
                FirstChildLayout.getGlobalVisibleRect(viewRect);
                if (!viewRect.contains((int) event.getRawX(), (int) event.getRawY())) {
                    Log.d("Touch : ","Finger is moving outside of this FirstChildLayout");
                }else{
                    Log.d("Touch : ","Finger is moving on this FirstChildLayout");
                }
                return false;
            }
        });

3) Implement onTouchListener for SecondChildLayout and identify whether your finger is moving on it or outside of it?

SecondChildLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                Rect viewRect = new Rect();
                SecondChildLayout.getGlobalVisibleRect(viewRect);
                if (!viewRect.contains((int) event.getRawX(), (int) event.getRawY())) {
                    Log.d("Touch : ","Finger is moving outside of this SecondChildLayout");
                }else{
                    Log.d("Touch : ","Finger is moving on this SecondChildLayout");
                }
                return false;
            }
        });
Jai
  • 1,974
  • 2
  • 22
  • 42
  • Thanks much for that answer but how to handle these Touches if there are more than one child in a parent layout – Deepak Sep 28 '16 at 07:57
  • @Deepak : the same way we are passing touch for one childlayout, we could pass for multiple child layouts too. I have updated my answer with example of 2 childlayouts kindly check it again! – Jai Sep 28 '16 at 08:01
  • it goes on listening to touch of first childlayout i.e. prints Finger is moving outside of first layout instead of secondchildlayout when i move on secondlayout – Deepak Sep 28 '16 at 08:33
  • @Deepak : It must working check properly you implemented touch for secondlayout and check initialize flag=true which I used in parenlayout's touch event and it will be true all time – Jai Sep 28 '16 at 08:53
  • @Deepak : Check properly parent layout's touch event which is dispatching event properly to your both childviews – Jai Sep 28 '16 at 08:55
  • @Deepak : Is it done? Or still are you getting any issue? – Jai Sep 28 '16 at 09:20
  • @Deepak : What's the issue are you getting now exactly? – Jai Sep 28 '16 at 09:33
  • the same which i mentioned last – Deepak Sep 28 '16 at 09:35
  • @Deepak : Could you paste your code in your question so I can easily help you with that! Paste everything you did in your code – Jai Sep 28 '16 at 09:36
  • please take a look what i have done code is in my question – Deepak Sep 28 '16 at 10:06
  • @Deepak : And could you paste the log output while moving your touch. I wanna just see that your touch is passing properly or not? – Jai Sep 28 '16 at 10:09
  • @Deepak : Also I would like to see your layout file to check the hierarchy and placement of your dragerlayout 1 and 2 – Jai Sep 28 '16 at 10:11