- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use Gravity View for Android
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: 'com.android.application' android { packagingOptions { exclude 'META-INF/proguard/androidx-annotations.pro' } packagingOptions { exclude 'META-INF/DEPENDENCIES' 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 "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'co.gofynd.library:gravity-view:1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' }
Step 3 − Add the following code to res/layout/activity_main.xml.
<?xml version = "1.0" encoding = "utf-8"?> <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:id = "@+id/activity_main" android:layout_width = "match_parent" android:layout_height = "wrap_content"> <HorizontalScrollView android:layout_width = "match_parent" android:layout_height = "wrap_content" android:scrollbars = "none"> <ImageView android:id = "@+id/imageView" android:layout_width = "wrap_content" android:layout_height = "wrap_content" /> </HorizontalScrollView> </RelativeLayout>
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/MainActivity.java
package com.example.andy.myapplication; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ImageView; import co.gofynd.gravityview.GravityView; public class MainActivity extends AppCompatActivity { GravityView gravityView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView imageView = findViewById(R.id.imageView); gravityView = GravityView.getInstance(this); boolean is_supported = gravityView.deviceSupported(); if(is_supported) { gravityView .setImage(imageView,R.drawable.back) .center(); } } @Override protected void onResume() { super.onResume(); gravityView.registerListener(); } @Override protected void onStop() { super.onStop(); gravityView.unRegisterListener(); } }
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
- Related Articles
- How to use View Stub in android?
- How to use concept () in Android text view?
- How to use contains () in Android text view?
- How to Set opacity for View in Android?
- How to remove onClickListener for a view in android?
- How to use search functionality in custom list view in Android?
- How to check android mobile supports GRAVITY sensor?
- How to give on click listener for footer view in android listview?
- How to use fill() for Listview in Android?
- How to use rotate() for Listview in Android?
- How to add footer view to android listview?
- How to use comparator interface for Listview in Android?
- How to write a custom adapter for my list view on Android using Kotlin?
- How do I use tabHost for Android?
- How to use MySQL VIEW with WHERE clause?
