{"id":1496,"date":"2016-04-16T05:56:35","date_gmt":"2016-04-16T05:56:35","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=1496"},"modified":"2019-06-12T12:09:57","modified_gmt":"2019-06-12T12:09:57","slug":"zoomcontrols","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/zoomcontrols","title":{"rendered":"Zoom Controls Tutorial With Example In Android Studio"},"content":{"rendered":"<p>In Android, Zoom Controls class display simple set of controls that is used for zooming and provides callback to register for events. Zoom Controls has two buttons ZoomIn and ZoomOut which are used to control the zooming functionality.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1540\" src=\"\/ui\/wp-content\/uploads\/2016\/04\/ZoomControls-In-Android.jpg\" alt=\"ZoomControls In Android\" width=\"298\" height=\"135\" \/><\/p>\n<p><strong>Zoom Controls code in XML:<\/strong><\/p>\n<pre>&lt;ZoomControls\r\nandroid:id=\"@+id\/simpleZoomControl\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\" \/&gt;\r\n<\/pre>\n<hr \/>\n<h4><strong>Important Methods Of Zoom Controls:<\/strong><\/h4>\n<p>Now let\u2019s discuss some common methods which are used to configure ZoomControls in our application.<\/p>\n<p><span style=\"color: #008000;\"><strong>1. hide():<\/strong><\/span> This method is used to hide the ZoomControls from the screen. In some cases we need to hide the ZoomControls from the screen so that we use this function.<\/p>\n<p><span style=\"color: #008000;\"><strong>2. show():<\/strong><\/span> This method is used to show the ZoomControls which\u00a0we\u00a0hide from the screen by using hide method.<\/p>\n<p>Below we show the use of hide and show methods of ZoomControls:<\/p>\n<p><strong>Step 1:<\/strong> In this example\u00a0first in\u00a0xml file we display ZoomControls with two buttons hide and show which are used to hide and show the ZoomControls.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1532\" src=\"\/ui\/wp-content\/uploads\/2016\/04\/hide-show-in-ZoomControls-Android.jpg\" alt=\"hide show in ZoomControls Android\" width=\"237\" height=\"404\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/04\/hide-show-in-ZoomControls-Android.jpg 237w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/04\/hide-show-in-ZoomControls-Android-176x300.jpg 176w\" sizes=\"auto, (max-width: 237px) 100vw, 237px\" \/><\/p>\n<p><strong>xml code of above image UI:<\/strong><\/p>\n<pre>&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\nxmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"match_parent\"\r\nandroid:paddingBottom=\"@dimen\/activity_vertical_margin\"\r\nandroid:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\nandroid:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\nandroid:paddingTop=\"@dimen\/activity_vertical_margin\"\r\ntools:context=\".MainActivity\"&gt;\r\n\r\n&lt;ZoomControls\r\nandroid:id=\"@+id\/simpleZoomControl\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_centerHorizontal=\"true\"\r\nandroid:layout_marginTop=\"50dp\" \/&gt;\r\n\r\n&lt;Button\r\nandroid:id=\"@+id\/show\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_centerInParent=\"true\"\r\nandroid:layout_margin=\"20dp\"\r\nandroid:background=\"#0f0\"\r\nandroid:text=\"Show\"\r\nandroid:textColor=\"#fff\" \/&gt;\r\n\r\n&lt;Button\r\nandroid:id=\"@+id\/hide\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_below=\"@+id\/show\"\r\nandroid:layout_margin=\"20dp\"\r\nandroid:background=\"#f00\"\r\nandroid:text=\"Hide\"\r\nandroid:textColor=\"#fff\" \/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;\r\n<\/pre>\n<p><strong>Step 2:<\/strong> Now in\u00a0Java Class we use hide() and show() methods to hide and show Zoom Controls option.<\/p>\n<pre>\/*Add below setContentView() method in Oncreate()*\/\r\n        final ZoomControls simpleZoomControls = (ZoomControls) findViewById(R.id.simpleZoomControl); \/\/ initiate a ZoomControls\r\n        Button show = (Button) findViewById(R.id.show); \/\/ initiate show Button\r\n        Button hide = (Button) findViewById(R.id.hide); \/\/ initiate hide Button\r\n\/\/ perform setOnClickListener on show button\r\n        show.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View v) {\r\n\/\/ show a ZoomControls\r\n                simpleZoomControls.show();\r\n            }\r\n        });\r\n\/\/ perform setOnClickListener on hide button\r\n        hide.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View v) {\r\n\/\/ hide a ZoomControls\r\n                simpleZoomControls.hide();\r\n\r\n            }\r\n        });<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>Now run the App and\u00a0you can see ZoomControls can be hided and showed again from user.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1538\" src=\"\/ui\/wp-content\/uploads\/2016\/04\/show-hide-in-ZoomControls-Android.gif\" alt=\"show hide in ZoomControls Android\" width=\"302\" height=\"377\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>3. setOnZoomInClickListener(OnClickListenerlistener):<\/strong><\/span> This is a listener event automatically called when we click on the Zoom In button of ZoomControls. In this listener we add the code to\u00a0zoom in image.<\/p>\n<p>Below we show the use of setOnZoomInClickListener in android.<\/p>\n<pre>final ZoomControls simpleZoomControls = (ZoomControls) findViewById(R.id.simpleZoomControl); \/\/ initiate a ZoomControls\r\n\r\n\/\/ perform\u00a0 setOnZoomInClickListener event on ZoomControls\r\nsimpleZoomControls.setOnZoomInClickListener(new View.OnClickListener() {\r\n@Override\r\npublic void onClick(View v) {\r\n\/\/ add zoom in code here\r\n}\r\n});\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. setOnZoomOutClickListener(OnClickListenerlistener):<\/strong><\/span> This is a listener event automatically called when we click on the Zoom Out button of ZoomControls. In this listener we add the code for zoom out a image.<\/p>\n<p>Below we show the use of setOnZoomOutClickListener in android.<\/p>\n<pre>final ZoomControls simpleZoomControls = (ZoomControls) findViewById(R.id.simpleZoomControl); \/\/ initiate a ZoomControls\r\n\r\n\/\/ perform\u00a0 setOnZoomOutClickListener event on ZoomControls\r\nsimpleZoomControls.setOnZoomOutClickListener(new View.OnClickListener() {\r\n@Override\r\npublic void onClick(View v) {\r\n\/\/ add zoom out code here\r\n}\r\n});\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>5. setIsZoomInEnabled(boolean isEnabled):<\/strong><\/span> This method is used to enable or disable the zoom In button of ZoomControls. In this method we set a Boolean value either\u00a0true or false. By default it has true value but sometime after a limit of zoom in we need to disable the zoom in functionality i.e.\u00a0after\u00a0that we didn\u2019t need more zoom in.<\/p>\n<p>Below we set the false value for setIsZoomInEnabled that disable zoom in button of ZoomControls.<\/p>\n<pre>ZoomControls simpleZoomControls = (ZoomControls) findViewById(R.id.simpleZoomControl); \/\/ initiate a ZoomControls\r\nsimpleZoomControls.setIsZoomInEnabled(false); \/\/ disable zoom in button of ZoomControls\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>6. setIsZoomOutEnabled(boolean isEnabled):<\/strong><\/span> This method is used to enable or disable the zoom Out button of ZoomControls. In this method we set a Boolean value means true or false. By default it has true value but sometime after a limit of zoom out we need to disable the zoom out functionality means at that time we didn\u2019t need more zoom out.<\/p>\n<p>Below we set the false value for setIsZoomOutEnabled that disable zoom out button of ZoomControls.<\/p>\n<pre>ZoomControls simpleZoomControls = (ZoomControls) findViewById(R.id.simpleZoomControl); \/\/ initiate a ZoomControls\r\nsimpleZoomControls.setIsZoomOutEnabled(false); \/\/ disable zoom out button of ZoomControls\r\n<\/pre>\n<hr \/>\n<h4><strong>Attributes Of\u00a0<\/strong><strong>Zoom Controls in Android<\/strong><strong>:<\/strong><\/h4>\n<p>Now let\u2019s \u00a0we discuss some important\u00a0attributes that helps us to configure a ZoomControls in our xml file.<\/p>\n<p><span style=\"color: #008000;\"><strong>1. id:\u00a0<\/strong><\/span>This attribute is used to uniquely identify a ZoomControls.<\/p>\n<pre>&lt;ZoomControls\r\nandroid:id=\"@+id\/simpleZoomControl\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\" \/&gt; &lt;!--\u00a0 id of ZoomControls used to uniquely identify it --&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. background:\u00a0<\/strong><\/span>This attribute is used to set the background of a ZoomControls. We can set a color or a drawable in the background of a ZoomControls.<\/p>\n<p>Below we set the black color for the background of ZoomControls.<\/p>\n<pre>&lt;ZoomControls\r\nandroid:id=\"@+id\/simpleZoomControl\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:background=\"#000\" \/&gt;\r\n&lt;!-- set black color in the background of a ZoomControls --&gt;\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1536\" src=\"\/ui\/wp-content\/uploads\/2016\/04\/background-in-ZoomControls-Android.jpg\" alt=\"background in ZoomControls Android\" width=\"286\" height=\"232\" \/><\/p>\n<p><strong>Setting background in ZoomControls In Java class:<\/strong><\/p>\n<pre>ZoomControls simpleZoomControls = (ZoomControls)findViewById(R.id.simpleZoomControl); \/\/ initiate a ZoomControls\r\nsimpleZoomControls.setBackgroundColor(Color.BLACK); \/\/ set black color in the background of ZoomControls\r\n<\/pre>\n<p><strong><span style=\"color: #008000;\">3. padding:<\/span>\u00a0<\/strong>This\u00a0attribute is used to set the padding from left, right, top or bottom side of a ZoomControls .<\/p>\n<ul>\n<li><strong>paddingRight:\u00a0<\/strong>set the padding from the right side of a ZoomControls<strong>.<\/strong><\/li>\n<li><strong>paddingLeft:\u00a0<\/strong>set the padding from the left side of a ZoomControls<strong>.<\/strong><\/li>\n<li><strong>paddingTop:\u00a0<\/strong>set the padding from the top side of a ZoomControls<strong>.<\/strong><\/li>\n<li><strong>paddingBottom:\u00a0<\/strong>set the padding from the bottom side of a ZoomControls<strong>.<\/strong><\/li>\n<li><strong>Padding:\u00a0<\/strong>set the padding from the all side\u2019s of a ZoomControls<strong>.<\/strong><\/li>\n<\/ul>\n<p>Below we set the 20dp padding from all the sides of a ZoomControls.<\/p>\n<pre>&lt;ZoomControls\r\nandroid:id=\"@+id\/simpleZoomControl\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:background=\"#000\"\r\nandroid:padding=\"20dp\"\/&gt;&lt;!-- 20dp padding from all the sides of a ZoomControls --&gt;\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1537\" src=\"\/ui\/wp-content\/uploads\/2016\/04\/padding-in-ZoomControls-Android.jpg\" alt=\"padding in ZoomControls Android\" width=\"289\" height=\"227\" \/><\/p>\n<hr \/>\n<h4><strong>Zoom Controls Example In Android Studio<\/strong><\/h4>\n<p>Below is an\u00a0example of ZoomControls in which we display an ImageView and a ZoomControls. In this first we hide the ZoomControls from the screen and show it on the touch event of image. We also perform setOnZoomInClickListener and setOnZoomOutClickListener events for implementing zoom in and zoom out functionality. After zoom in and zoom out the ZoomControls is automatically hide from the screen and re-shown\u00a0if user click on the image. A Toast is displayed to show the zoom in and zoom out message on the screen.<\/p>\n<p>Below you can download complete Android Studio project code, final output and step by step explanation of the example:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/ZoomControlsExample\" target=\"_blank\" rel=\"nofollow\">Download Code<\/a><a class=\"help\" title=\"Learn How To Download Code And Import In Android Studio\" href=\"\/androidstudio\/download-code-abhiandroid\" target=\"_blank\" rel=\"nofollow\"> ? <\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1539\" src=\"\/ui\/wp-content\/uploads\/2016\/04\/ZoomControls-Example-In-Android-Studio.gif\" alt=\"ZoomControls Example In Android Studio\" width=\"318\" height=\"483\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span><strong>\u00a0<\/strong>Create a new project and name it ZoomControlsExample.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span><strong>\u00a0<\/strong>Open res -&gt; layout -&gt;activity_main.xml (or) main.xml and add following code :<\/p>\n<p>In this step we add the code for displaying a ImageView and ZoomControls.<\/p>\n<pre>&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\nxmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"match_parent\"\r\nandroid:background=\"#000\"\r\nandroid:paddingBottom=\"@dimen\/activity_vertical_margin\"\r\nandroid:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\nandroid:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\nandroid:paddingTop=\"@dimen\/activity_vertical_margin\"\r\ntools:context=\".MainActivity\"&gt;\r\n\r\n&lt;ImageView\r\nandroid:id=\"@+id\/image\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_centerInParent=\"true\"\r\nandroid:src=\"@drawable\/image\" \/&gt;\r\n\r\n&lt;ZoomControls\r\nandroid:id=\"@+id\/simpleZoomControl\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_alignParentBottom=\"true\"\r\nandroid:layout_centerHorizontal=\"true\"\r\nandroid:layout_marginTop=\"50dp\" \/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span><strong>\u00a0<\/strong>Open src -&gt; package -&gt; MainActivity.java<\/p>\n<p>In this step firstly we initiate the ImageView and ZoomControls and then hide the ZoomControls from the screen and show it on the touch event of image.<\/p>\n<p>We also perform setOnZoomInClickListener and setOnZoomOutClickListener events for implementing zoom in and zoom out functionality. After zoom in and zoom out the ZoomControls is automatically hide from the screen and for re-showing it user need to click on image. A Toast is displayed to show the zoom in and zoom out message on the screen.<\/p>\n<pre>package example.abhiandroid.zoomcontrolsexample;\r\n\r\nimport android.os.Bundle;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.view.Menu;\r\nimport android.view.MenuItem;\r\nimport android.view.MotionEvent;\r\nimport android.view.View;\r\nimport android.widget.Button;\r\nimport android.widget.ImageView;\r\nimport android.widget.Toast;\r\nimport android.widget.ZoomControls;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    ImageView image;\r\n    ZoomControls simpleZoomControls;\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        image = (ImageView) findViewById(R.id.image); \/\/ initiate a ImageView\r\n        simpleZoomControls = (ZoomControls) findViewById(R.id.simpleZoomControl); \/\/ initiate a ZoomControls\r\n        simpleZoomControls.hide(); \/\/ initially hide ZoomControls from the screen\r\n        \/\/ perform setOnTouchListener event on ImageView\r\n        image.setOnTouchListener(new View.OnTouchListener() {\r\n            @Override\r\n            public boolean onTouch(View v, MotionEvent event) {\r\n                \/\/ show Zoom Controls on touch of image\r\n                simpleZoomControls.show();\r\n                return false;\r\n            }\r\n        });\r\n        \/\/ perform setOnZoomInClickListener event on ZoomControls\r\n        simpleZoomControls.setOnZoomInClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View v) {\r\n                \/\/ calculate current scale x and y value of ImageView\r\n                float x = image.getScaleX();\r\n                float y = image.getScaleY();\r\n                \/\/ set increased value of scale x and y to perform zoom in functionality\r\n                image.setScaleX((float) (x + 1));\r\n                image.setScaleY((float) (y + 1));\r\n                \/\/ display a toast to show Zoom In Message on Screen\r\n                Toast.makeText(getApplicationContext(),\"Zoom In\",Toast.LENGTH_SHORT).show();\r\n                \/\/ hide the ZoomControls from the screen\r\n                simpleZoomControls.hide();\r\n            }\r\n        });\r\n        \/\/ perform setOnZoomOutClickListener event on ZoomControls\r\n        simpleZoomControls.setOnZoomOutClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View v) {\r\n                \/\/ calculate current scale x and y value of ImageView\r\n                float x = image.getScaleX();\r\n                float y = image.getScaleY();\r\n                \/\/ set decreased value of scale x and y to perform zoom out functionality\r\n                image.setScaleX((float) (x - 1));\r\n                image.setScaleY((float) (y - 1));\r\n                \/\/ display a toast to show Zoom Out Message on Screen\r\n                Toast.makeText(getApplicationContext(),\"Zoom Out\",Toast.LENGTH_SHORT).show();\r\n                \/\/ hide the ZoomControls from the screen\r\n                simpleZoomControls.hide();\r\n            }\r\n        });\r\n    }\r\n\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>Now run the App and you can see a image on the screen. Click on the image and Zoom Controls button will appear on the screen. Now do Zoom In or Zoom Out the image as you wish.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Android, Zoom Controls class display simple set of controls that is used for zooming and provides callback to register for events. Zoom Controls has two buttons ZoomIn and ZoomOut which are used to control the zooming functionality. Zoom Controls code in XML: &lt;ZoomControls android:id=&#8221;@+id\/simpleZoomControl&#8221; android:layout_width=&#8221;wrap_content&#8221; android:layout_height=&#8221;wrap_content&#8221; \/&gt; Important Methods Of Zoom Controls: Now let\u2019s &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/zoomcontrols\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Zoom Controls Tutorial With Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-1496","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>Zoom Controls Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Learn Zoom Controls, how its methods and buttons ZoomIn and ZoomOut adds zooming functionality in Android. We explain step by step using example in Android Studio.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/zoomcontrols\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1496","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=1496"}],"version-history":[{"count":2,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1496\/revisions"}],"predecessor-version":[{"id":2815,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1496\/revisions\/2815"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=1496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}