C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs



Home » Android

ImageView in Android



In this article, we will learn how to use Image View with user defined images?
Submitted by Manu Jemini, on January 04, 2018

ImageViews are the necessity in app development. ImageView is the part of the view where we can show our own image, that image-data can come from anywhere like a database, from the server, an API or we can also store it locally.

In this example, we use the image-data from the local storage and it is also known as drawable. It is a directory in your project where we keep all our static images.

1) The First step is to make or open an android project.

2) The second step is to create your new layout file or open an old one.

3) The third step is to add an image view to your layout.

4) The fourth step is to link the image-view with the image-file or image-data in your drawable folder.

5) The fifth step is to give height and width to your ImageView.

Your java-file need not have any changes. After everything in place, this will the result.

Image using as the image source.

dlsr - image source for the code
android:src="@drawable/dslr"

Java file:

package com.example.hp_u.my_contact;

        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;

public class ImageView extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_view);
    }
}

Layout file:

<?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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hp_u.my_contact.ImageView">
    <ImageButton
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:id="@+id/imageButton3"
        android:layout_gravity="center"
        android:src="@drawable/dslr"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp_u.my_contact">

    <uses-feature android:name="android.hardware.camera2" />

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ListContact"
            android:label="@string/title_activity_list_contact" />
        <activity android:name=".ImageView"></activity>
    </application>

</manifest>

Output

ImageView in Android






You may also be interested in...

C/C++ Tips and Tricks...

Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.


Comments and Discussions