Mohammed Asmar
Mohammed Asmar

Reputation: 328

drag and drop when i drop the textview in the bottom of the screen

what is my code do here is just drag the textview from the pink area and drop it to the yellow area

the problem is when i drag the textView in the bottom of the screen textview will be invisible

Please if you didn't get my question ask me in a comment

I tried to search a lot but not getting any pointers

here is the screenshot and the code

enter image description here

this is the main activity class

package com.javapapers.android.drag_drop;

import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.View.DragShadowBuilder;
import android.widget.LinearLayout;
import android.util.Log;
import com.javapapers.android.drag_drop.R;


public class MainActivity extends Activity implements OnTouchListener,OnDragListener{
    private static final String LOGCAT = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.textView1).setOnTouchListener(this);
        findViewById(R.id.pinkLayout).setOnDragListener(this);
        findViewById(R.id.yellowLayout).setOnDragListener(this);
    }
    public boolean onTouch(View view, MotionEvent motionEvent) { 
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
              DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
              view.startDrag(null, shadowBuilder, view, 0);
              view.setVisibility(View.INVISIBLE);
              return true;
            }
            else {
                return false;
            }

    }  
    public boolean onDrag(View layoutview, DragEvent dragevent) {
          int action = dragevent.getAction();
          switch (action) {
          case DragEvent.ACTION_DRAG_STARTED:
              Log.d(LOGCAT, "Drag event started");
            break;
          case DragEvent.ACTION_DRAG_ENTERED:
              Log.d(LOGCAT, "Drag event entered into "+layoutview.toString());
            break;
          case DragEvent.ACTION_DRAG_EXITED:
              Log.d(LOGCAT, "Drag event exited from "+layoutview.toString());
            break;
          case DragEvent.ACTION_DROP:
            Log.d(LOGCAT, "Dropped");
            View view = (View) dragevent.getLocalState();
            ViewGroup owner = (ViewGroup) view.getParent();
            owner.removeView(view);
            LinearLayout container = (LinearLayout) layoutview;
            container.addView(view);
            view.setVisibility(View.VISIBLE);
            break;
          case DragEvent.ACTION_DRAG_ENDED:
                  Log.d(LOGCAT, "Drag ended");
              break;
          default:
            break;
          }
          return true;
    }
}

and this is the activity_main xml

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/pinkLayout"
        android:layout_width="fill_parent"
        android:layout_height="210dp"
        android:background="#FF8989"
        android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/dragtext"
        android:textSize="70sp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/yellowLayout"
        android:layout_width="fill_parent"
        android:layout_height="250dp"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/pinkLayout"
        android:background="#FFCC00"
        android:orientation="vertical" >

    </LinearLayout>

</RelativeLayout>

Thank You

Upvotes: 0

Views: 474

Answers (1)

SemaphoreMetaphor
SemaphoreMetaphor

Reputation: 691

This is because is not hitting the dropped case but rather the exited and ended cases, so the visibility of the view is never set to visible again

Upvotes: 1

Related Questions