package com.example.drag01;


//import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;

import android.view.MotionEvent;

public class MainActivity extends Activity {

	int windowwidth;
    int windowheight;
    ImageView image;

    private LayoutParams layoutParams ;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		 windowwidth = getWindowManager().getDefaultDisplay().getWidth();
		 windowheight = getWindowManager().getDefaultDisplay().getHeight();
		 //image = (ImageView) findViewById(R.id.image); // initiate a ImageView
		 
		 final ImageView balls = (ImageView)findViewById(R.id.image);
		 
		 balls.setOnTouchListener(new View.OnTouchListener() {

             @Override
             public boolean onTouch(View v, MotionEvent event) {
                 LayoutParams layoutParams = (LayoutParams) balls.getLayoutParams();
                 switch(event.getAction())
                 {
                 case MotionEvent.ACTION_DOWN:   
                                                 break;
                 case MotionEvent.ACTION_MOVE:
                                                 int x_cord = (int)event.getRawX();
                                                 int y_cord = (int)event.getRawY();

                                                 if(x_cord>windowwidth){x_cord=windowwidth;}
                                                 if(y_cord>windowheight){y_cord=windowheight;}

                                                 layoutParams.leftMargin = x_cord -25;
                                                 layoutParams.topMargin = y_cord - 75;

                                                 balls.setLayoutParams(layoutParams);
                                                 break;
                 default:
                                                 break;
                 }
                     return true;
             }
     });
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
