I'm making a Letter Drag n Drop game (kinda like Guess the Brand) where you have an image and you have to drag the letters into layouts to form the correct answer.
At the top, I have Four layouts (layoutAnsw A to D), and in the bottom I have Four buttons (btnTop A to D) I have the OnTouchListener and OnDragListener working fine, except one single thing.
What happens when I have more than one similar character (letter)? For example in this image:
As you can see, I have to "A" letters that need to be dragged, and I want to do it regardless of which one you put first. In my code I managed to get something like this:
"If the First A is in the First space, then Second A goes to Second Space"
I'm trying to code the other way around including that previous statement. My code so far let's you put any "A" in any space, including 2 letters in the same space. Pretty useless.
My code so far
public class OneQuestionA extends Fragment implements OnTouchListener,
OnDragListener {
protected static final String LOGCAT = null;
int numDragged = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.questions_fragment5,
container, false);
btnTopA.setOnTouchListener(this); // Letter A
btnTopB.setOnTouchListener(this); // Second Letter A
btnTopC.setOnTouchListener(this);
btnTopD.setOnTouchListener(this);
rootView.findViewById(R.id.layoutAnswA).setOnDragListener(this); // Layout 1
rootView.findViewById(R.id.layoutAnswB).setOnDragListener(this); // Layout 2
rootView.findViewById(R.id.layoutAnswC).setOnDragListener(this); // Layout 3
rootView.findViewById(R.id.layoutAnswD).setOnDragListener(this); // Layout 4
return rootView;
}
My onTouch implementation:
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// TODO Auto-generated method stub
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(null, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
view.setVisibility(View.VISIBLE);
return true;
} else {
return false;
}
}
My onDrag implementation:
@Override
public boolean onDrag(View v, DragEvent e) {
int action = e.getAction();
View view = (View) e.getLocalState();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
return true;
case DragEvent.ACTION_DRAG_ENTERED:
return false;
case DragEvent.ACTION_DRAG_LOCATION:
return false;
case DragEvent.ACTION_DRAG_EXITED:
return false;
case DragEvent.ACTION_DROP:
if (view.getId() == R.id.btnTopA && v.getId() == R.id.layoutAnswA) {
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) v;
container.addView(view);
view.setVisibility(View.VISIBLE);
view.setOnTouchListener(null);
view.setOnDragListener(null);
numDragged++;
} else if (view.getId() == R.id.btnTopB && v.getId() == R.id.layoutAnswB) {
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) v;
container.addView(view);
view.setVisibility(View.VISIBLE);
view.setOnTouchListener(null);
view.setOnDragListener(null);
numDragged++;
}
}
if (numDragged >= 4) {
numDragged = 0;
Toast.makeText(getActivity(),
"All buttons in the Right place", Toast.LENGTH_SHORT)
.show();
}
case DragEvent.ACTION_DRAG_ENDED:
if (dropEventNotHandled(e)) {
view.setVisibility(View.VISIBLE);
}
}
return false;
}
private boolean dropEventNotHandled(DragEvent e) {
// TODO Auto-generated method stub
return !e.getResult();
}