Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6735271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:59:22+00:00 2026-05-26T10:59:22+00:00

I am trying to modify the TouchListView component to allow me to drag an

  • 0

I am trying to modify the TouchListView component to allow me to drag an item from a list view over another view, to create a drag and drop selection system. The user will drag a list item and release it to indicate the selection.

So far I have modified the onDrop method of the DropListener class to pass along the MotionEvent. And I am using the getY() method of the motion even to determine if the user dropped the dragged view over the image view. I am using the raw value of getY() and if it is less than -50 I am accepting that as a valid drop.

But I am concerned, that this is a rather kludgy solution, and prone to errors. Is there a better way to determine when the user has released over the target view area? Are there cases in which i cannot simply look for a negative number? Is there a way to actually see what view lies beneath a given set of X/Y coordinates?

Please note the attached picture of the layout in-progress.

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:59:23+00:00Added an answer on May 26, 2026 at 10:59 am

    Here is the solution that i came up with that worked.

    package com.commonsware.cwac.tlv.demo;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    
    import android.app.ListActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.ViewTreeObserver;
    import android.view.ViewTreeObserver.OnGlobalLayoutListener;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import com.commonsware.cwac.tlv.TouchListView;
    
    public class TouchListViewDemo extends ListActivity {
        private static String[] items={"lorem", "ipsum", "dolor", "sit", "amet",
                                                                        "consectetuer", "adipiscing", "elit", "morbi", "vel",
                                                                        "ligula", "vitae", "arcu", "aliquet", "mollis",
                                                                        "etiam", "vel", "erat", "placerat", "ante",
                                                                        "porttitor", "sodales", "pellentesque", "augue", "purus"};
        private TouchListView tlv;
        private IconicAdapter adapter=null;
        private ArrayList<String> array=new ArrayList<String>(Arrays.asList(items));
    
        private TextView target_name_display;
        private ImageView target;
    
        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.main);
    
            tlv = (TouchListView)getListView();
            adapter=new IconicAdapter();
            setListAdapter(adapter);
    
            tlv.setDropListener(onDrop);
            tlv.setRemoveListener(onRemove);
    
    
            target = (ImageView) findViewById(R.id.drag_target_area);
            target_name_display = (TextView) findViewById(R.id.name_text);
            Log.d("TARGET", "Target:"+target);
            Log.d("TARGET", "TargeName Displayt:"+target_name_display);
    
    
            ViewTreeObserver vto = target.getViewTreeObserver();
            vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    
                @Override
                public void onGlobalLayout() {
                    target.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
                    int height = target.getHeight();
                    int top = target.getTop();
                    int tlv_y_position = tlv.getTop();
                    Log.d("ON LAYOUT", "Height:"+ height+ " top:"+top+" tlv_y_position:"+tlv_y_position);               
                }
            });
    
        }
    
        private TouchListView.DropListener onDrop=new TouchListView.DropListener() {
            @Override
            public void drop(int from, int to, MotionEvent e) {
                Log.i("TOUCH VIEW DEMO", "From: "+from + "  TO: "+to );
                Log.i("TOUCH VIEW DEMO", "Event: "+e.getY());
                    String item=adapter.getItem(from);
    
                    //
                    int[] target_coords = new int[2]; 
                    int[] tlv_coords = new int[2]; 
                    target.getLocationInWindow(target_coords);
                    tlv.getLocationInWindow(tlv_coords);
                    int height = target.getMeasuredHeight();
                    int negative_top = -1*(tlv_coords[1]-target_coords[1]);
                    Log.d("TOUCH VIEW DEMO", " negative_top:"+negative_top + " e.getY():"+e.getY() + " heigh:"+height);
                    Log.d("TOUCH VIEW DEMO", "Target Coords:"+ target_coords[0] +","+ target_coords[1]);
                    Log.d("TOUCH VIEW DEMO", "Tlv Coords:"+ tlv_coords[0] +","+ tlv_coords[1]);
                    if(e.getY() > negative_top && e.getY() < negative_top + height){
                        target_name_display.setText(item.toString());
                        adapter.remove(item);
                    }
                    //adapter.remove(item);
                    //adapter.insert(item, to);
            }
        };
    
        private TouchListView.RemoveListener onRemove=new TouchListView.RemoveListener() {
            @Override
            public void remove(int which) {
                    adapter.remove(adapter.getItem(which));
            }
        };
    
        class IconicAdapter extends ArrayAdapter<String> {
            IconicAdapter() {
                super(TouchListViewDemo.this, R.layout.row2, array);
            }
    
            public View getView(int position, View convertView,
                                                    ViewGroup parent) {
                View row=convertView;
    
                if (row==null) {                                                    
                    LayoutInflater inflater=getLayoutInflater();
    
                    row=inflater.inflate(R.layout.row2, parent, false);
                }
    
                TextView label=(TextView)row.findViewById(R.id.label);
    
                label.setText(array.get(position));
    
                return(row);
            }
        }
    }
    

    The code includes a lot of comments and varaibles getting calculated each time, which I will refactor out. But you can modify the TouchListView demo to make this work on your own.

    Here is the main.xml layout I used.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <RelativeLayout
            android:id="@+id/drag_here_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_weight="1.0"
            android:background="@drawable/matches_current_match_bg"
            android:paddingBottom="30dp" >
    
            <TextView
                android:id="@+id/drag_here_text"            
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="15dp"
                android:text="DRAG HERE"
                android:textSize="14dp" />
    
            <ImageView
                android:id="@+id/drag_target_area"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_below="@id/drag_here_text"
                android:layout_marginBottom="15dp"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:background="#00000000"
                android:scaleType="fitXY"
                android:src="@drawable/match_drag_here_target" />
            <TextView
                android:id="@+id/name_text"            
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_alignTop="@id/drag_target_area"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="15dp"
                android:text=""
                android:textSize="14dp" />
        </RelativeLayout>
    <com.commonsware.cwac.tlv.TouchListView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tlv="http://schemas.android.com/apk/res/com.commonsware.cwac.tlv.demo"
    
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false"
        tlv:normal_height="64dip"
        tlv:grabber="@+id/icon"
        tlv:remove_mode="slideRight"
        android:layout_weight="1.0"
    />
    </LinearLayout>
    

    Thanks again CommonsWare for your excellent TouchListView Widget.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to modify a newly placed UITabBarItem item from a UIViewController to
I am trying to modify model coming from View and then update my database
I'm trying to modify my GreaseMonkey script from firing on window.onload to window.DOMContentLoaded, but
I am trying to modify the amcap, an application from Windows SDK's example to
I'm trying to modify SOAP body for an outbound ws client SOAP message, from
I am trying to modify iperf to support another protocol (UDT). The UDT API
I'm trying to modify the contents of one pointer using another. I want string2
I'm trying to modify an XmlDocument by deleting nodes from it while I'm iterating
I'm trying to modify an existing Django Mezzanine setup to allow me to blog
I am trying to modify the tutorial Ray Wenderlich has created at: http://www.raywenderlich.com/1888/how-to-create-a-simple-iphone-app-tutorial-part-33 So

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.