My RelativeLayout
has an image. I am just trying to make it draggable in the entire layout.The issue is that every time I drag and drop, it goes back to the original position. Here is my
drag_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainView"
android:background="@drawable/bg_animation"
tools:context="com.example.activities.AnimationActivity">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher"
android:id="@+id/appLogo"
android:layout_alignBottom="@+id/ba_cell_animation"
android:layout_centerHorizontal="true"
android:layout_marginBottom="47dp" />
</RelativeLayout>
AnimationActivity.java
public class AnimationActivity extends ActionBarActivity implements View.OnTouchListener, View.OnDragListener {
static ImageView mLogo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animation);
setTitle("Animation");
Typeface mAnimPromptTF = Typeface.createFromAsset(getAssets(), "fonts/Jelloween - Machinato ExtraLight.ttf");
Typeface mAnimBonusTF = Typeface.createFromAsset(getAssets(), "fonts/Jelloween - Machinato SemiBold Italic.ttf");
mAnimPrompt = (TextView) findViewById(R.id.anim_prompt);
mAnimBonus = (TextView) findViewById(R.id.anim_bonus);
mAnimPrompt.setTypeface(mAnimPromptTF);
mAnimBonus.setTypeface(mAnimBonusTF);
mLogo = (ImageView) findViewById(R.id.appLogo);
mLogo.setOnTouchListener(this);
findViewById(R.id.mainView).setOnDragListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent e) {
// TODO Auto-generated method stub
if (e.getAction() == MotionEvent.ACTION_DOWN) {
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(null, shadowBuilder, v, 0);
v.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
@Override
public boolean onDrag(View v, DragEvent e) {
// TODO Auto-generated method stub
switch (e.getAction()) {
case DragEvent.ACTION_DROP:
View view = (View) e.getLocalState();
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
RelativeLayout to = (RelativeLayout) v;
to.addView(view);
view.setVisibility(View.VISIBLE);
break;
//the drag point has entered the bounding box of the View
case DragEvent.ACTION_DRAG_ENTERED:
break;
//the user has moved the drag shadow outside the bounding box of the View
case DragEvent.ACTION_DRAG_EXITED:
break;
// the drag and drop operation has concluded.
case DragEvent.ACTION_DRAG_ENDED:
break;
default:
break;
}
return true;
}
@Override
public void onBackPressed() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
I have tried all the following links but still don't know where I am going wrong:
http://www.vogella.com/tutorials/AndroidDragAndDrop/article.html
http://codingjunkie.net/android-drag-and-drop-part1
http://tech-papers.org/android-drag-and-drop
I have really tried a lot.