I’m trying to create an Android application where the user can drag any circle over the other and the textbox should show the sum of those two circles. Every time the programs starts it should show circles with random position and random number from 1 -9.
Unfortunately I am no where near that goal I have created one circle but when i try to drag it the application crashes. Please also help me with the rest of the functionality of the app. thanks.
The final output should look something like this:

MainActivity.java
package com.dragandadd;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout main = (FrameLayout) findViewById(R.id.activity_main);
main.addView(new Ball(this,50,50,25));
main.addView(new Ball(this,50,50,25));
main.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent e) {
float x = e.getX();
float y = e.getY();
FrameLayout flView = (FrameLayout) v;
flView.addView(new Ball(getParent(), x,y,25));
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Ball.java
package com.dragandadd;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;
public class Ball extends View {
final float x;
final float y;
final int r;
final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
public Ball(Context context, float x, float y, int r) {
super(context);
mPaint.setColor(0xFFFF0000);
this.x = x;
this.y = y;
this.r = r;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(x, y, r, mPaint);
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF66FF33" />
use this how to drag images
How to implement Drag and Drop in android 2.2?
and this
http://blahti.wordpress.com/2011/01/17/moving-views-part-2/