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:
ZoomControls in Android with Example
Next article icon

ZoomControls in Android with Example

Last Updated : 08 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Android, Zoom Control is a class that has some set of methods that are used to control the zoom functionality. It has two buttons that are used to control the zoom functionality (ie Zoom In and Zoom Out). Zoom Control Class has been deprecated in API Version 29. The functionalities offered by ZoomControls class are handled in a better way through Custom view and custom layouts then dedicated zoom controls widgets.

ZoomControls in Android

Important Methods of Zoom Controls

Let say ZoomControls be the reference of the ZoomControl class which is being used to call the different methods of the ZoomControls class.

ZoomControl zoomControls = (ZoomControls) findViewById(R.id.simpleZoomControl);

1. show(): This method is used to show the zoom controls on the  App UI.

// will show the zoom controls
zoomControls.show() 


2. hide(): This method is used to hide the zoom controls on the App UI.

// will hide the zoom controls
zoomControls.hide()


3. setOnZoomInClickListener(OnClickListenerlistener): This method is invoked when Zoom In button is pressed. It is used to customize the UI that will be shown when the zoom-in button is being pressed.

zoomControls.setOnZoomInClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// add the code which will be executed when  
// the zoom in button has been pressed
}
});


4. setOnZoomOutClickListener(OnClickListenerlistener): This method is invoked when Zoom Out is pressed. It too works in the same manner as the setOnZoomInClickListener() method works, but it minimizes ie converges the UI.

zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// add the code which will be executed when  
// the zoom out button has been pressed
}
});


5. setIsZoomInEnabled(boolean isEnabled): It is one of the methods of Zoom Controls which is used to enable or disable the zoom-in functionality.

// it will enable the zoomIn button
zoomControls.setIsZoomInEnabled(true)

// it will disable the zoomIn button
zoomControls.setIsZoomInEnabled(false)


6. setIsZoomOutEnabled(boolean isEnabled): It is also one of the methods of Zoom Controls, which is used to enable or disable the zoom out functionality.

// it will enable the zoomOut button
zoomControls.setIsZoomOutEnabled(true)

// it will disable the zoomOut button
zoomControls.setIsZoomOutEnabled(false)


7. setZoomSpeed(long speed): This is used to set the zoom speed of the zoom that is being done with zoom controls

Important Attributes of Zoom Controls

  • id: This attribute is used to give the zoom controls a unique identity.

<ZoomControls android:id="@+id/zoom_controls/>

  • background: This is used to give the background color to the zoom controls.

<ZoomControls
android:id="@+id/zoom_controls
android:background="#fffff"/>

  • padding: This is used to give padding on the sides of zoom controls.

<ZoomControls
android:id="@+id/zoom_controls
android:padding="20dp"/>

Example

A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using both Java and Kotlin language. 

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 Kotlin as the programming language.

Step 2: Working with the activity_main.xml file

Now go to the activity_main.xml file which represents the UI of the application. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.

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">      <!--Adding the image view-->     <ImageView         android:id="@+id/image_View"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:scaleType="fitXY"         android:src="@drawable/indiamap" />      <!--Adding the Zoom Controls          within the relative layout-->     <ZoomControls         android:id="@+id/zoom_controls"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentEnd="true"         android:layout_alignParentBottom="true"         android:layout_margin="10dp" />      </RelativeLayout> 


Step 3: Working with the MainActivity file

Go to the MainActivity file, and refer to the following code. Below is the code for the both MainActivity.kt and MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Java
// Java code to implement the zoom controls import androidx.appcompat.app.AppCompatActivity; import android.graphics.Color; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; import android.widget.ZoomControls;  public class MainActivity extends AppCompatActivity {      ImageView imageView;     ZoomControls zoomControls;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);                imageView=findViewById(R.id.image_View);         zoomControls=findViewById(R.id.zoom_controls);                zoomControls.setBackgroundColor(Color.BLACK);         zoomControls.show();        // onTouch listener function  when the image is clicked         imageView.setOnTouchListener(                 new View.OnTouchListener() {                     @Override                     public boolean onTouch(View view, MotionEvent motionEvent) {                         zoomControls.show();                         return false;                     }                 }         );          // This function will be automatically called out,when         // zoom in button is being pressed         zoomControls.setOnZoomInClickListener(                 new View.OnClickListener() {                     @Override                     public void onClick(View view) {                         float x=imageView.getScaleX();                         float y=imageView.getScaleY();                                                // setting the new scale                         imageView.setScaleX((float)(x+0.5f));                         imageView.setScaleY((float)(y+0.5f));                         zoomControls.hide();                     }                 }         );          // This function will be called when         // zoom out button is pressed         zoomControls.setOnZoomOutClickListener(                 new View.OnClickListener() {                     @Override                     public void onClick(View view) {                         float x=imageView.getScaleX();                         float y=imageView.getScaleY();                         if(x==1 && y==1)                         {                             // the scale will remain same,since                             // it is maximum possible zoom out                             imageView.setScaleX((float)(x));                             imageView.setScaleY((float)(y));                             zoomControls.hide();                         }                         else                         {                             // setting the new scale                             imageView.setScaleX((float)(x-0.5f));                             imageView.setScaleY((float)(y-0.5f));                             // hiding the zoom controls                             zoomControls.hide();                         }                     }                 }         );     } } 
Kotlin
// Kotlin code to implement the zoom controls import android.os.Bundle import android.view.View import androidx.appcompat.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_main.*  class MainActivity : AppCompatActivity() {     override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)          // onTouch listener function when the image is clicked         image_View.setOnTouchListener { v, m -> // Perform tasks here             zoom_controls.show()             false         }          // This function will be automatically called out,when         // zoom in button is being pressed         zoom_controls.setOnZoomInClickListener(                 View.OnClickListener {                     val x: Float = image_View.getScaleX()                     val y: Float = image_View.getScaleY()                      // setting the new scale                     image_View.setScaleX((x + 0.5f) as Float)                     image_View.setScaleY((y + 0.5f) as Float)                     zoom_controls.hide()                 }         )          // This function will be called when         // zoom out button is pressed         zoom_controls.setOnZoomOutClickListener(                 View.OnClickListener {                     val x: Float = image_View.getScaleX()                     val y: Float = image_View.getScaleY()                     if (x == 1f && y == 1f) {                         image_View.setScaleX(x as Float)                         image_View.setScaleY(y as Float)                         zoom_controls.hide()                     } else {                         // setting the new scale                         image_View.setScaleX((x - 0.5f) as Float)                         image_View.setScaleY((y - 0.5f) as Float)                         // hiding the zoom controls                         zoom_controls.hide()                     }                 }         )     } } 

Output



Next Article
ZoomControls in Android with Example

L

lavishgarg26
Improve
Article Tags :
  • Java
  • Technical Scripter
  • Kotlin
  • Android
  • Technical Scripter 2020
  • Android-Animation
Practice Tags :
  • Java

Similar Reads

    ViewSwitcher in Android with Example
    All android apps will have a feature to switch different views in order to explain/promote their site or product. Visual representation of a product by showing different views will impress the customers easily. In this article, let us see how to bring the "ViewSwitcher" to Android. ViewSwitcher is a
    6 min read
    GridView in Android with Example
    A GridView is a type of AdapterView that displays items in a two-dimensional scrolling grid. Items are inserted into this grid layout from a database or from an array. The adapter is used for displaying this data, setAdapter() method is used to join the adapter with GridView. The main function of th
    5 min read
    PhotoView in Android with Example
    In this article, PhotoView is added to android. PhotoView aims to help produce an easily usable implementation of a zooming Android ImageView using multi-touch and double-tap. Besides that, it has many more features like it notifying the application when the user taps on the photo or when the displa
    2 min read
    Implement Zoom In or Zoom Out in Android
    Zoom In and Zoom Out animations are used to enlarge and reduce the size of a view in Android applications respectively. These types of animations are often used by developers to provide a dynamic nature to the applications. Users also feel the changes happening in the application by watching these k
    4 min read
    Popup Menu in Android With Example
    In Android development, Menus are an important part of the user interface, providing users with easy access to common functionalities and ensuring a smooth and consistent experience throughout the application. In Android, we have three types of Menus available to define a set of options and actions
    4 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