Working with Android Touch Event

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Android Working with Touch Events thumbnail

In this Example, we will discuss how to interact with the touch event in android.

Previously Touch Event mostly used will developing games but in today’s scenario, this also getting used in most of the Normal apps to give a more user-friendly interface.
Many views in Android Comes with Some Inbuilt method to interact with touch events but then also we must know the main Technic  how to implement
So guys Lets Start,
1. Create a new project by going to File ⇒ New Android Project. Fill all the details and name your activity as MainActivity.
 

2. Once the project is created open your activity_main.xml file and Add Below Code.

 
<LinearLayout 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"  
   tools:context="com.targetandroid.info.Touch.TouchActivity"  
   android:weightSum="10"  
   android:orientation="vertical">  
   <LinearLayout  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:layout_weight="1"  
     android:weightSum="8"  
     android:layout_marginLeft="10dp"  
     android:layout_marginRight="10dp"  
     >  
     <TextView  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:text="x:"  
       android:layout_weight="1"  
       android:background="@android:color/holo_red_light"/>  
     <TextView  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:text="12546"  
       android:id="@+id/tvx_touch"  
       android:layout_weight="1"  
       android:background="@android:color/holo_red_light"/>  
     <TextView  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:text="y:"  
       android:layout_weight="1"  
       android:background="@android:color/holo_green_light"/>  
     <TextView  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:text="51551"  
       android:id="@+id/tvy_touch"  
       android:layout_weight="1"  
       android:background="@android:color/holo_green_light"/>  
     <TextView  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:text="dx:"  
       android:layout_weight="1"  
       android:background="@android:color/holo_blue_light"/>  
     <TextView  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:text="42165"  
       android:id="@+id/tvdx_touch"  
       android:layout_weight="1"  
       android:background="@android:color/holo_blue_light"  
       />  
     <TextView  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:text="dy:"  
       android:layout_weight="1"  
       android:background="@android:color/holo_orange_dark"  
       />  
     <TextView  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:text="165151"  
       android:id="@+id/tvdy_touch"  
       android:layout_weight="1"  
       android:background="@android:color/holo_orange_dark"/>  
   </LinearLayout>  
   <LinearLayout  
     android:layout_width="match_parent"  
     android:layout_height="450dp"  
     android:layout_weight="9"  
     android:orientation="vertical"  
     android:id="@+id/lltoucharea_touch"  
     android:background="@android:color/darker_gray">  
   </LinearLayout>  
 </LinearLayout>
here we will use MotionEvent to track all the motion of the user on the screen.
here we will use some of the features of a motion event. Android Gives us tremendous feature if you want to work more with this you can refer this Documentation
3. Now Open Your MainActivity.java   Add Below Code In File

import android.support.v7.app.AppCompatActivity;   
  import android.os.Bundle;   
  import android.view.Menu;   
  import android.view.MenuItem;   
  import android.view.MotionEvent;   
  import android.view.View;   
  import android.widget.LinearLayout;   
  import android.widget.TextView;   
  import com.targetandroid.info.R;   


  public class TouchActivity extends AppCompatActivity {   

   //creating variables  
   float xaxis=0;   
   float yaxis=0;   
   float last_xaxis=0;   
   float last_yaxis=0;   
   float x;   
   float y;   
   private TextView tvx_touch,tvy_touch,tvdy_touch,tvdx_touch;   
   private LinearLayout lltoucharea_touch;   


   @Override   
   protected void onCreate(Bundle savedInstanceState) {   
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.activity_touch);  
 
    //Initializing all the variables   
    tvy_touch=(TextView)findViewById(R.id.tvy_touch);   
    tvx_touch=(TextView)findViewById(R.id.tvx_touch);   
    tvdy_touch=(TextView)findViewById(R.id.tvdy_touch);   
    tvdx_touch=(TextView)findViewById(R.id.tvdx_touch);   
    lltoucharea_touch=(LinearLayout)findViewById(R.id.lltoucharea_touch);  
 
    //add touch listeners to linear layout to track user touches  
    lltoucharea_touch.setOnTouchListener(new View.OnTouchListener() {   
     @Override   
     public boolean onTouch(View v, MotionEvent event) {   

      int actionPerformed=event.getAction();
   
      switch (actionPerformed){   
       case MotionEvent.ACTION_DOWN:   
        //this will track the single touch of the user and   
        //will remain same till user lift his finger for second time 

        x=event.getX();   
        y=event.getY();   
        last_xaxis=x;   
        last_yaxis=y;   
        tvx_touch.setText(Float.toString(last_xaxis));   
        tvy_touch.setText(Float.toString(last_yaxis));   
        break;   
       case MotionEvent.ACTION_MOVE:   

        //this will track the movement of touch how and where user moving his finger  
        x = event.getX();   
        y=event.getY();   
        double dx=x-last_xaxis;   
        double dy=y-last_yaxis;   
        xaxis+=dx;   
        yaxis+=dy;   
        tvdx_touch.setText(Float.toString(xaxis));   
        tvdy_touch.setText(Float.toString(yaxis));   
        break;   
      }   
      return true;   
     }   
    });   
   }   
  }
4. Now finally run Your Project

You Will See A Below Output

Final output of Woking with Android Touch Event
Final Output


For More Tutorial Share And Subscribe to my Blog So you will get all the updates in your mail , We Never Spam.

Thank You


  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Add a Comment

Your email address will not be published. Required fields are marked *