Android Gestures

5

Android Gestures

 

Android Gestures – In this chapter, you will learn following:

Gestures control

Android Gestures

Android Gestures App

Android Gestures List

Android Gestures Setting

Android Custom Gestures

Android Gestures Library

How To Use Android Gestures

Swipe Gestures Example

Here are 8 gestures you can use on Android that you might not know about.
  • 1) Two-finger swipe down.
  • 2) Press and hold on notifications.
  • 3) Press and hold to select.
  • 4) Swipe down from the Chrome address bar.
  • 5) Press and hold Power Off.
  • 6) Tap and hold the spacebar.
  • 7) Triple-tap to zoom in.
  • 8) Tap and hold on menus.

so lets we start our android gestures  you know tutorial that android facilitates different and special types of touch screen events e.g  double tab, long press, flinch and pinch. Yeah, you will know it nowadays everyone uses a smartphone and everyone knows that how is android gestures and how to use gestures in android. In some cases android apps ask users to double tap on the screen, sometimes they ask long press to copy the content and much more so these are android gestures. So if you don’t know about android gestures you will learn instantly. Just keep reading my android gestures tutorial I will teach everything that should know about android gestures

android facilitate GestureDetector class to receive movement events and reveal that these incidents match gestures or not. To put it to use , you need to generate an object of GestureDetector and prolong another class with GestureDetector.SimpleOnGestureListener to do something as a listener and override some methods. Its syntax is listed below

GestureDetector myG;
myG = new GestureDetector(this,new Gesture());
   
class Gesture extends GestureDetector.SimpleOnGestureListener{
   public boolean onSingleTapUp(MotionEvent ev) {
   }
   
   public void onLongPress(MotionEvent ev) {
   }
   
   public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
   float distanceY) {
   }
   
   public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
   float velocityY) {
   }
}

 

Android Handling Pinch Gestures

Android os provides ScaleGestureDetector class to take care of gestures like pinch. To be able to use it, you will need to instantiate an object of this class. Its syntax is given below

ScaleGestureDetector SGD;
SGD = new ScaleGestureDetector(this,new ScaleListener());

 

The first parameter is the context and the next parameter is the function listener. We must define the function listener and override a function OnTouchEvent to make it working. Its syntax is listed below

public boolean onTouchEvent(MotionEvent ev) {
   SGD.onTouchEvent(ev);
   return true;
}

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
   @Override
   public boolean onScale(ScaleGestureDetector detector) {
      float scale = detector.getScaleFactor();
      return true;
   }
}

 

Android Gestures Example

Here is a good example of android gestures demonstrating the utilization of ScaleGestureDetector class. It creates a simple application which allows you to focus in and out through pinch.

To test out this android gestures example , you can run this on a genuine device or within an emulator with touchscreen enabled.

 

      Steps     Description

  1. You use Android studio to generate an Android request under a deal com.example.sappal.myapplication.
  2. Modify src/MainActivity.java data file to include necessary code.
  3. Modify the res/design/activity_main to include respected XML components
  4. Run the application form and choose a operating google android device and mount the application onto it and check the results

Following is this content of the revised main activity data file src/MainActivity.java.

package com.example.sappal.myapplication;

import android.app.Activity;
import android.graphics.Matrix;
import android.os.Bundle;

import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.ImageView;

public class MainActivity extends Activity {
   private ImageView iv;
   private Matrix matrix = new Matrix();
   private float scale = 1f;
   private ScaleGestureDetector SGD;

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

      iv=(ImageView)findViewById(R.id.imageView);
      SGD = new ScaleGestureDetector(this,new ScaleListener());
   }

   public boolean onTouchEvent(MotionEvent ev) {
      SGD.onTouchEvent(ev);
      return true;
   }

   private class ScaleListener extends ScaleGestureDetector.
      SimpleOnScaleGestureListener {
      
      @Override
      public boolean onScale(ScaleGestureDetector detector) {
         scale *= detector.getScaleFactor();
         scale = Math.max(0.1f, Math.min(scale, 5.0f));
         matrix.setScale(scale, scale);
         iv.setImageMatrix(matrix);
         return true;
      }
   }
}

 

Following is the modified content of the xml res/layout/activity_main.xml.

Here abc indicates the logo of androdevelopment

<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:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" 
   tools:context=".MainActivity" >
   
   <TextView android:text="Gestures  
      Example" android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textview"
      android:textSize="35dp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Andro Development"
      android:id="@+id/textView"
      android:layout_below="@+id/textview"
      android:layout_centerHorizontal="true"
      android:textColor="#ff7aff24"
      android:textSize="35dp" />
      
   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:src="@drawable/abc"
      android:scaleType="matrix"
      android:layout_below="@+id/textView"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_alignParentBottom="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true" />
      
</RelativeLayout>

Following is the content of the res/values/string.xml.

<resources>
   <string name="app_name>My Application</string>
</resources>

Following is the content of AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sappal.myapplication" >

   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.example.sappal.myapplicationMainActivity"
         android:label="@string/app_name" >
         
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
   
   </application>
</manifest>

 

Let’s make an effort to run the application. I suppose you have linked your actual Android os Mobile device with your personal computer. To perform the software from Android studio room, available one of your project’s activity data files and click Run Eclipse Run Icon from the toolbar.The test output should end up like this

android gestures

Now just place two hands over android display , and distinct them the right part and you’ll note that the android image is zooming. It really is shown in the image below

android gestures

Now again place two fingertips over android display screen, and make an effort to close them and you’ll notice that the android image is currently shrinking. It really is shown in the image below –

android gestures