Image View code snippet for Android Studio


Welcome. In this post you will learn how to add an Image View in Android Studio.

Android Image View Example

An Image View is a placeholder used to display images on the screen.  The image files are generally of jpg, png, bmp or gif format. It is considered a good practice to generally put the images inside the drawable folder.

Placing an Image View in your App

To insert an Image View in your app, add the <ImageView> tag to your XML (activity_main.xml) file. The corresponding code is shown below:-

Note that in this code, the android:src attribute is used to locate the path of the image file, which in our case is placed inside the drawable folder with the name ic_cycle1.

Image view also has different attributes to scale the image and support different scaling types. Some common scaling types are fit_xy, center_crop, center,  fitStart, etc. The default scaling type is fitCenter. You can also set a background for the image with the android:background attribute. You can also add some padding at the sides with the attribute android:padding according to your needs.


An Image View for example purpose is shown below, where the image of a cyclist is shown:-


Image View code snippet for Android Studio Example


The Full Java Code along with the XML layout for theis shown below:-



XML (activity_main.xml)


You can directly copy paste these snippets to your android studio, and they should work perfectly. Just save and run your project. You can also try to play with these layout settings on your own and see the corresponding changes accordingly. There are a variety of properties available which can be changed to make your Image more fancy looking.

For a more detailed guide, you can also visit android’s official guide below:-

Comment down below if you are facing any problems.


Also Read:-

Leave a Reply

Your email address will not be published. Required fields are marked *