{"id":809,"date":"2016-02-05T12:18:02","date_gmt":"2016-02-05T12:18:02","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=809"},"modified":"2019-06-12T09:47:09","modified_gmt":"2019-06-12T09:47:09","slug":"imageview","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/imageview","title":{"rendered":"ImageView Tutorial With Example In Android"},"content":{"rendered":"<p>In Android, ImageView class is used to display an image file in application. Image file is easy to use but hard to master in Android, because of the various screen sizes in Android devices. An android is enriched with some of the best UI design widgets that allows us\u00a0to build good looking and attractive UI based application.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important\u00a0Note:<\/strong><\/span> ImageView comes with different configuration options to support different scale types. Scale type options are used for scaling the bounds of an image to the bounds of the imageview. Some of them scaleTypes configuration properties are center, center_crop, fit_xy, fitStart etc. You can read our <a href=\"\/ui\/scaletype-imageview-example.html\">ScaleType tutorial<\/a> to learn all details on it.<\/p>\n<p><strong>Below\u00a0is an ImageView code in XML:<\/strong><\/p>\n<p>Make sure to save lion image in drawable folder<\/p>\n<pre>&lt;ImageView\r\nandroid:id=\"@+id\/simpleImageView\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:src=\"@drawable\/lion\" \/&gt;\r\n<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-811\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/ImageView-in-Android.jpg\" alt=\"ImageView in Android\" width=\"226\" height=\"275\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>Attributes of ImageView:<\/strong><\/h4>\n<p>Now let\u2019s \u00a0we discuss some important\u00a0attributes that helps us to configure a ImageView in your xml file.<\/p>\n<p><span style=\"color: #008000;\"><strong>1. id:<\/strong><\/span> id is an attribute used to uniquely identify a image view in android. Below is the example code in which we set the id of a image view.<\/p>\n<pre>&lt;ImageView\r\n<strong>android:id=\"@+id\/simpleImageView\"<\/strong>\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\n\/&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. src:<\/strong><\/span> src is an attribute used to set a source file or you can say image in your imageview to make your layout attractive.<\/p>\n<p>Below is the example code in which we set the source of a imageview lion which is saved in drawable folder.<\/p>\n<pre>&lt;ImageView\r\nandroid:id=\"@+id\/simpleImageView\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\n<strong>android:src=\"@drawable\/lion\"<\/strong> \/&gt;&lt;!--set the source of an image view--&gt;\r\n<\/pre>\n<p><strong>In Java:<\/strong><\/p>\n<p>We can also set the source image at run time programmatically in java class. For that we use setImageResource() method\u00a0as shown in below example code.<\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\nImageView simpleImageView=(ImageView) findViewById(R.id.<em>simpleImageView<\/em>);\r\nsimpleImageView.setImageResource(R.drawable.<em>lion<\/em>);\/\/set the source in java class\r\n<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-812\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/src-attribute-in-imageview-240x300.jpg\" alt=\"src attribute in imageview\" width=\"240\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/src-attribute-in-imageview-240x300.jpg 240w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/src-attribute-in-imageview.jpg 355w\" sizes=\"auto, (max-width: 240px) 100vw, 240px\" \/><\/center><span style=\"color: #008000;\"><strong>3. background:<\/strong><\/span>\u00a0background attribute is used to set the background of a ImageView. We can set a color or a drawable in the background of a ImageView.<\/p>\n<p>Below is the example code in which we set the black color in the background and an image in the src attribute of image view.<\/p>\n<pre>&lt;ImageView\r\n    android:id=\"@+id\/simpleImageView\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:src=\"@drawable\/lion\"\r\n    <strong>android:background=\"#000\"<\/strong>\/&gt;&lt;!--black color in background of a image view--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-813\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/Background-in-ImageView-Android-249x300.jpg\" alt=\"Background in ImageView Android\" width=\"249\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Background-in-ImageView-Android-249x300.jpg 249w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Background-in-ImageView-Android.jpg 254w\" sizes=\"auto, (max-width: 249px) 100vw, 249px\" \/><\/center><strong>In Java:<\/strong><\/p>\n<p>We can also set the background at run time programmatically in java class. In below example code we set the black color in the background of a image view.<\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\nImageView simpleImageView=(ImageView) findViewById(R.id.simpleImageView);\r\nsimpleImageView.setBackgroundColor(Color.BLACK);\/\/set black color in background of a image view in java class<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. padding: <\/strong><\/span>padding attribute is used to set the padding from left, right, top or bottom of the Imageview.<\/p>\n<ul>\n<li><strong>paddingRight:<\/strong> set the padding from the right side of the image view<strong>.<\/strong><\/li>\n<li><strong>paddingLeft:<\/strong> set the padding from the left side of the image view<strong>.<\/strong><\/li>\n<li><strong>paddingTop:<\/strong> set the padding from the top side of the image view<strong>.<\/strong><\/li>\n<li><strong>paddingBottom:<\/strong> set the padding from the bottom side of the image view<strong>.<\/strong><\/li>\n<li><strong>padding:<\/strong> set the padding from the all side\u2019s of the image view<strong>.<\/strong><\/li>\n<\/ul>\n<p>Below is the example code of padding attribute in which we set the 30dp padding from all the side\u2019s of a image view.<\/p>\n<pre>&lt;ImageView\r\n    android:id=\"@+id\/simpleImageView\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:background=\"#000\"\r\n    android:src=\"@drawable\/lion\"\r\n    <strong>android:padding=\"30dp\"<\/strong>\/&gt;&lt;!--set 30dp padding from all the sides--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-815\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/Padding-in-ImageView-Example-229x300.jpg\" alt=\"Padding in ImageView Example\" width=\"229\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Padding-in-ImageView-Example-229x300.jpg 229w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Padding-in-ImageView-Example.jpg 364w\" sizes=\"auto, (max-width: 229px) 100vw, 229px\" \/><\/center><strong><span style=\"color: #008000;\">5. scaleType:<\/span>\u00a0<\/strong>scaleType is an attribute used to control how the image should be re-sized or moved to match the size of this image view. <strong>The value for scale type attribute can\u00a0be fit_xy, center_crop, fitStart etc.<\/strong><\/p>\n<p>Below is the example code of scale type in which we set the scale type of image view to fit_xy.<\/p>\n<pre>&lt;ImageView\r\nandroid:id=\"@+id\/simpleImageView\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:src=\"@drawable\/lion\"\r\n<strong>android:scaleType=\"fitXY\"<\/strong>\/&gt;&lt;!--set scale type fit xy--&gt;\r\n<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-816\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/scaleType-in-ImageView-Example-268x300.jpg\" alt=\"scaleType in ImageView Example\" width=\"268\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/scaleType-in-ImageView-Example-268x300.jpg 268w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/scaleType-in-ImageView-Example.jpg 361w\" sizes=\"auto, (max-width: 268px) 100vw, 268px\" \/><\/center><strong>Let\u2019s we take an another example of scale type to understand the actual working of scale type in a image view.<\/strong><\/p>\n<p>In below example code we set the value for scale type \u201cfitStart\u201d\u00a0 which is used to fit the image in the start of the image view as shown below:<\/p>\n<pre>&lt;ImageView\r\n    android:id=\"@+id\/simpleImageView\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:src=\"@drawable\/lion\"\r\n    <strong>android:scaleType=\"fitStart\"<\/strong>\/&gt;&lt;!--set scale type fit start of image view--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-817\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/scaleType-fitstart-in-ImageView-Example-248x300.jpg\" alt=\"scaleType fitstart in ImageView Example\" width=\"248\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/scaleType-fitstart-in-ImageView-Example-248x300.jpg 248w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/scaleType-fitstart-in-ImageView-Example.jpg 358w\" sizes=\"auto, (max-width: 248px) 100vw, 248px\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>Example of ImageView:<\/strong><\/h4>\n<p>Below is the example of imageview in which we display two animal images of Lion and Monkey. And whenever user\u00a0click on an image Animal name is displayed as\u00a0toast on screen. Below is the final output and code:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/ImageViewExample\" 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><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-818\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/ImageView-Example-in-Android-Studio-153x300.jpg\" alt=\"ImageView Example in Android Studio\" width=\"153\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/ImageView-Example-in-Android-Studio-153x300.jpg 153w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/ImageView-Example-in-Android-Studio.jpg 350w\" sizes=\"auto, (max-width: 153px) 100vw, 153px\" \/><\/center><strong>Step 1:<\/strong> Create a new project and name it ImageViewExample.<\/p>\n<p>In this step we create a new project in android studio by filling all the necessary\u00a0details of the app like app name, package name, api versions etc.<\/p>\n<pre>Select File -&gt; New -&gt; New Project and Fill the forms and click \"Finish\" button.\r\n<\/pre>\n<p><strong>Step 2:<\/strong> Download two images lion and monkey from the web. Now save those images in the drawable folder of your project.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-819\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/save-images-in-drawable-folder-Android-Studio.jpg\" alt=\"save images in drawable folder Android Studio\" width=\"274\" height=\"286\" \/><\/center><strong>Step 3:<\/strong>\u00a0Now open res -&gt; layout -&gt; activity_main.xml (or) main.xml\u00a0and add following code:<\/p>\n<p>In this step we\u00a0add the code for displaying an image view on the screen in a\u00a0relative layout. <strong>Here make sure you have already saved two images name lion and monkey in your drawable folder.<\/strong><\/p>\n<pre>&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:paddingBottom=\"@dimen\/activity_vertical_margin\"\r\n    android:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingTop=\"@dimen\/activity_vertical_margin\"\r\n    tools:context=\".MainActivity\"&gt;\r\n\r\n\r\n    &lt;ImageView\r\n        android:id=\"@+id\/simpleImageViewLion\"\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"200dp\"\r\n        android:scaleType=\"fitXY\"\r\n        android:src=\"@drawable\/lion\" \/&gt;\r\n\r\n    &lt;ImageView\r\n    android:id=\"@+id\/simpleImageViewMonkey\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"200dp\"\r\n    android:layout_below=\"@+id\/simpleImageViewLion\"\r\n    android:layout_marginTop=\"10dp\"\r\n    android:scaleType=\"fitXY\"\r\n    android:src=\"@drawable\/monkey\" \/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><strong>Step 4:<\/strong>\u00a0Now open app\u00a0-&gt; java -&gt; package -&gt;\u00a0MainActivity.java and add the following code:<\/p>\n<p>In this step we add the code to\u00a0initiate the image view&#8217;s and then perform click event on them.<\/p>\n<pre>package example.abhiandriod.imageviewexample;\r\n\r\nimport android.graphics.Color;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.Menu;\r\nimport android.view.MenuItem;\r\nimport android.view.View;\r\nimport android.widget.ImageView;\r\nimport android.widget.Toast;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        ImageView simpleImageViewLion = (ImageView) findViewById(R.id.simpleImageViewLion);\/\/get the id of first image view\r\n        ImageView simpleImageViewMonkey = (ImageView) findViewById(R.id.simpleImageViewMonkey);\/\/get the id of second image view\r\n        simpleImageViewLion.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View view) {\r\n                Toast.makeText(getApplicationContext(), \"Lion\", Toast.LENGTH_LONG).show();\/\/display the text on image click event\r\n            }\r\n        });\r\n        simpleImageViewMonkey.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View view) {\r\n                Toast.makeText(getApplicationContext(), \"Monkey\", Toast.LENGTH_LONG).show();\/\/display the text on image click event\r\n            }\r\n        });\r\n    }\r\n\r\n    \r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<p>Now start AVD in Emulator and run the App. You will see the images of Lion and Monkey displayed on screen. Click on any Animal image and his name will appear on Screen. We clicked on Lion.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-820\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/ImageView-Example-Run-App-in-Android-Studio-197x300.jpg\" alt=\"ImageView Example Run App in Android Studio\" width=\"197\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/ImageView-Example-Run-App-in-Android-Studio-197x300.jpg 197w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/ImageView-Example-Run-App-in-Android-Studio.jpg 229w\" sizes=\"auto, (max-width: 197px) 100vw, 197px\" \/><\/center><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Android, ImageView class is used to display an image file in application. Image file is easy to use but hard to master in Android, because of the various screen sizes in Android devices. An android is enriched with some of the best UI design widgets that allows us\u00a0to build good looking and attractive UI &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/imageview\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">ImageView Tutorial With Example In Android<\/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-809","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>ImageView Tutorial With Example In Android \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Learn imageview and its attribute like scaleType, padding, src etc. with example in Android Studio. Also create one complete project on ImageView and download its code for free.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/imageview\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/809","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=809"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/809\/revisions"}],"predecessor-version":[{"id":2789,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/809\/revisions\/2789"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}