Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
GravityView in Android
Next article icon

GravityView in Android

Last Updated : 07 Jun, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we are going to show the GravityView in android. In this article, we are going to see the gravity effect on an image. As we move our phone we will see different parts of the image. Here we will be using Horizontal ScrollView so we will be moving our phone horizontally. In the below video you can visualize how this is happening

Step by Step Implementation

Step 1: Create a new Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.  

Step 2: Add this into build.gradle file

implementation 'co.gofynd.library:gravity-view:1.0'

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

XML
<?xml version="1.0" encoding="utf-8"?> <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"     tools:context=".MainActivity">      <HorizontalScrollView         android:layout_width="match_parent"         android:layout_height="wrap_content">          <ImageView             android:id="@+id/imageView"             android:layout_width="match_parent"             android:layout_height="wrap_content" />     </HorizontalScrollView>      </RelativeLayout> 

Step 4: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file 

if(!gravityView.deviceSupported()){             // show the error / imageView             Toast.makeText(GravityViewActivity.this,"Not Supported",Toast.LENGTH_LONG).show();         }         else         {           // if device is supported then we will add the image           // in imageview and will show the image using gravity view           gravityView.setImage(imageView,R.drawable.image).center(); }

Below is the code for the MainActivity.java file.

Java
import android.os.Bundle; import android.widget.ImageView; import android.widget.Toast;  import androidx.appcompat.app.AppCompatActivity;  import co.gofynd.gravityview.GravityView;  public class MainActivity extends AppCompatActivity {          GravityView gravityView;     ImageView imageView;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          imageView = findViewById(R.id.imageView);         gravityView = GravityView.getInstance(this);         if (!gravityView.deviceSupported()) {             // show the error             Toast.makeText(MainActivity.this, "Not Supported", Toast.LENGTH_LONG).show();         } else {             // if device is supported then we will add the image              // in imageview and will show the image using gravity view             gravityView.setImage(imageView, R.drawable.image).center();         }     }      @Override     protected void onStop() {         super.onStop();         gravityView.unRegisterListener();     }      @Override      protected void onResume() {         super.onResume();         gravityView.registerListener();     } } 

Output:


Next Article
GravityView in Android

A

annianni
Improve
Article Tags :
  • Java
  • Android
  • Android-View
Practice Tags :
  • Java

Similar Reads

    Zigzag View in Android
    Zigzag View is a unique UI element that enhances the user experience in Android applications. You can often find this design element in eCommerce apps, where it makes the existing interface unique. The main purpose of Zigzag View is to create eye-catching banner ads or to display images and text in
    2 min read
    PopView in Android
    In this article, PopView is added to android. When users tap on the view a pop animation with a circular dust effect will appear. A new view can also appear after the popping of the older view. PopView can be used to hide the view or change the view. It makes the user interface more attractive. Supp
    3 min read
    Scroll ImageView in Android
    In this article, we are going to implement a very important feature related to the ImageView. The image will keep on scrolling horizontally by itself. When we click on the image then it will stop scrolling and when we again click it will again keep scrolling. We can use this feature to show animatio
    3 min read
    FrameLayout in Android
    Android Framelayout is a ViewGroup subclass that is used to specify the position of multiple views placed on top of each other to represent a single view screen. Generally, we can say FrameLayout simply blocks a particular area on the screen to display a single view. Here, all the child views or ele
    3 min read
    ProtractorView in Android
    In this article, ProtractorView is added to android. ProtractorView is a semicircular Seekbar view for selecting an angle from 0° to 180. Seek bar is a type of progress bar. Change the cursor from 0° to 180 for selecting an angle. Below is the image of ProtractorView. Step By Step ImplementationStep
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences