1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.apis.view;
18 
19 import com.example.android.apis.R;
20 
21 import android.app.Activity;
22 import android.os.Bundle;
23 import android.view.DragEvent;
24 import android.view.View;
25 import android.widget.TextView;
26 
27 public class DragAndDropDemo extends Activity {
28     TextView mResultText;
29     DraggableDot mHiddenDot;
30 
31     @Override
onCreate(Bundle savedInstanceState)32     protected void onCreate(Bundle savedInstanceState) {
33         super.onCreate(savedInstanceState);
34         setContentView(R.layout.drag_layout);
35 
36         TextView text = (TextView) findViewById(R.id.drag_text);
37         DraggableDot dot = (DraggableDot) findViewById(R.id.drag_dot_1);
38         dot.setReportView(text);
39         dot = (DraggableDot) findViewById(R.id.drag_dot_2);
40         dot.setReportView(text);
41         dot = (DraggableDot) findViewById(R.id.drag_dot_3);
42         dot.setReportView(text);
43 
44         mHiddenDot = (DraggableDot) findViewById(R.id.drag_dot_hidden);
45         mHiddenDot.setReportView(text);
46 
47         mResultText = (TextView) findViewById(R.id.drag_result_text);
48         mResultText.setOnDragListener(new View.OnDragListener() {
49             public boolean onDrag(View v, DragEvent event) {
50                 final int action = event.getAction();
51                 switch (action) {
52                     case DragEvent.ACTION_DRAG_STARTED: {
53                         // Bring up a fourth draggable dot on the fly. Note that it
54                         // is properly notified about the ongoing drag, and lights up
55                         // to indicate that it can handle the current content.
56                         mHiddenDot.setVisibility(View.VISIBLE);
57                     } break;
58 
59                     case DragEvent.ACTION_DRAG_ENDED: {
60                         // Hide the surprise again
61                         mHiddenDot.setVisibility(View.INVISIBLE);
62 
63                         // Report the drop/no-drop result to the user
64                         final boolean dropped = event.getResult();
65                         mResultText.setText(dropped ? "Dropped!" : "No drop");
66                     } break;
67                 }
68                 return false;
69             }
70         });
71     }
72 }