Adding Images And Accessing Them In Android
Images are widely used to explain so many things. Today, android has become the tool of marketing. We can add various graphics and images in our app. Think of an app without an image. At least the app has a logo which is an image.
Today, in this tutorial, we will see how to add images and access them in android.
For that, we primarily need images. That was not so funny was it ??
So, go to "Project Explorer" and expand your project "Hello" and and go to resources ie. res
Here you will see :
- drawable-hdpi
- drawable-mdpi
- drawable-xdpi
- drawable-xxdpi
Thu, each of them represent the quality of image. If you have an image with many quality like HD, Medium,etc. you can add it to respective directories. However, currently we will only use one image. Copy that image and paste it in drawable-mdpi.
Make sure that the image name contains only a-z characters and 0-9 numbers.
Thus, after pasting your image, you have added the image. Now we need to display that image.
To display the image, go to your newlayout which we created earlier and in the Pallet / Widget column, go to Images And Media and drag the Image View and drop it on the screen.
A dialog box will open asking you for the image. Select the image you just pasted from the list.
The Screen should display that image. Check Graphical Layout.
Check the xml and you will see
android:src="@drawable/wlogo" //your logo name
Thus, @ is an operator and drawable is your directory / folder and the name of file.
Now,you can change the height and width of that image.
You can also pass numeric values ie. 200 px and 300 px. But, personally, I do not recommend this as this will not make your app responsive.android:layout_width="match_parent"android:layout_height="match_parent"
Responsive app is an app that can adjust itself to layout of device.
No comments:
Post a Comment