android touch event example

android touch event example

 Create an application to draw line on the screen as user drag his finger.

android touch event example

android touch event example explains how you can use Touch Event while developing the game in android. Here is complete code which shows the steps to do for starting your android game development. also, check out android editt text validation tutorial.

I have written this blog for android tutorial, php tutorial, java tutorial, jsp tutorial, c programming tutorial, javascript tutorial, servlet tutorial,  all tutorials are written in easy way so you can understand easily, most of the tutorial are also available for download options, download sample code easily.

Questions Related with Touch Event Example : Android Canvas Example, Explain various methods of Canvas and Paint class with example, What is the purpose of Canvas and Paint object? Which methods should be used to set paint color to red, paint style to STROKE? Also write the code example to draw circle with linear gradient

Location : res\layout

FileName: activity_main.xml

We are not using this xml file to render your graphics, so not need to modify code

 

 

Location : src\package\

FileName : MainActivity.java

 

package com.example.gtu_android_prog20;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);

setContentView(new TouchEventExample(getApplicationContext()));

}

@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);
}
}

 

Location : src\package\

FileName : TouchEventExample.java

package com.example.gtu_android_prog20;

/*
*  GTU MCA SLMC Android Program 20 :  Create an application to draw line on the screen as user drag his finger.
*
*
*/

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;

public class TouchEventExample extends View{

private Paint paint = new Paint();

float x1,x2,y1,y2;

public TouchEventExample(Context context) {
super(context);
// TODO Auto-generated constructor stub

paint.setColor(Color.rgb(250, 50, 50));
}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
//super.onDraw(canvas);

//=====================================
//Set Line Width
//=====================================
paint.setStrokeWidth(3.4f);

//=============================================
//Draw Line from X to Y (As per Click)
//============================================
canvas.drawLine(x1, y1, x2, y2, paint);

canvas.drawText(“Touch and Move to Draw Line”, 80, 50, paint);

canvas.drawText(“Developed By: Adarsh Patel”, 80, 300, paint);
}

@Override
public boolean onTouchEvent(MotionEvent event) {

switch (event.getAction())
{

//=============================================================================
//Case to detect Down Event (Means When you Press Screen This Event will occure)
//=============================================================================
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
y1 = event.getY();
return true;

//=============================================================================
//Case to detect Move Event
//=============================================================================
case MotionEvent.ACTION_MOVE:
x2 = event.getX();
y2 = event.getY();
break;

default:
return false;
}

//======================================
//call redraw
//======================================
invalidate();

return false;

}

}

 

Output 1

android touch event example

Output 2

android touch event example

Download Complete Code

 

More Tutorials from Adarsh Patel, Check few more article using following links:

Adarsh Patel

My Self Adarsh Suryakant Patel. Currently I am working as Assistant Professor at Parul Instiute of Engineering and Technology, MCA Department, Limda, Vadodara. Major Responsibility : * Lectures * Organizing Training and Expert Sessions for * Placement and Recruitment * Faculty Representative