How to use Gravity View for Android

AndroidMobile DevelopmentApps/Applications

Before getting into an example, we should know what gravity view in android is. Gravity view allows us to utilize the motion sensors of an Android device and allow the end user to explore the product by rotating his device.

This example demonstrates How to use Gravity View for Android.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Open build.gradle and add design support library dependency.

apply plugin: ''
android {
   packagingOptions {
      exclude 'META-INF/proguard/'
   packagingOptions {
      exclude 'META-INF/LICENSE'
      exclude 'META-INF/LICENSE.txt'
      exclude 'META-INF/license.txt'
      exclude 'META-INF/NOTICE'
      exclude 'META-INF/NOTICE.txt'
      exclude 'META-INF/notice.txt'
      exclude 'META-INF/ASL2.0'
   compileSdkVersion 28
   defaultConfig {
      applicationId "com.example.andy.myapplication"
      minSdkVersion 15
      targetSdkVersion 28
      versionCode 1
      versionName "1.0"
      testInstrumentationRunner ""
   buildTypes {
      release {
         minifyEnabled false
         proguardFiles getDefaultProguardFile('proguard-android.txt'), ''
dependencies {
   implementation fileTree(dir: 'libs', include: ['*.jar'])
   implementation ''
   implementation ''
   implementation 'co.gofynd.library:gravity-view:1.0'
   testImplementation 'junit:junit:4.12'
   androidTestImplementation ''
   androidTestImplementation ''

Step 3 − Add the following code to res/layout/activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<RelativeLayout xmlns:android = ""
   android:id = "@+id/activity_main"
   android:layout_width = "match_parent"
   android:layout_height = "wrap_content">
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content"
      android:scrollbars = "none">
         android:id = "@+id/imageView"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content" />

In the above code, we have taken a horizontal scroll view to scroll image and image view to show the image on the screen.

Step 4 − Add the following code to src/

package com.example.andy.myapplication;
import android.os.Bundle;
import android.widget.ImageView;
import co.gofynd.gravityview.GravityView;
public class MainActivity extends AppCompatActivity {
   GravityView gravityView;
   protected void onCreate(Bundle savedInstanceState) {
      ImageView imageView = findViewById(;
      gravityView = GravityView.getInstance(this);
      boolean is_supported = gravityView.deviceSupported();
      if(is_supported) {
   protected void onResume() {
   protected void onStop() {

In the above code we have taken an image from drawable as the back so add a scrollable image on drawable.

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from the android studio, open one of your project's activity files and click the Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen –

In the above screen is shown the center of the image, now move a left side and right side to the mobile device. It will show output as shown below –


Click here to download the project code

Published on 14-Mar-2019 12:03:28