How To Create Image View in an Android Application using Eclipse:

In this tutorial, you will be Learning, How to display Image View in an activity

Here you go!!!!

 

Before creating an android application using Eclipse, let us see some of the key terms involved while developing this application.

Key terms involved in this project:

Imageview: Displays an arbitrary image, such as an icon. The ImageView class can load images from various sources (such as resources or content providers), takes care of computing its measurement from the image so that it can be used in any layout manager, and provides various display options such as scaling and tinting.

Drawable: A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon. There are several different types of drawables: Bitmap File, Nine-Patch File, Layer list, State List, Level list, Transition Drawable, Inset Drawable, Clip Drawable, Scale Drawable, Shape Drawable. This project requires bitmap file/image file to fill the activity.

Bitmap File: A bitmap image. Android supports bitmap files in a three formats: .png (preferred) .jpg (acceptable), .gif (discouraged). You can reference a bitmap file directly, using the filename as the resource ID, or create an alias resource ID in XML.

The tutorial is about how to display an image file.

This project is developed in Eclipse 4.2 (Juno) and tested with Android 2.2

If you are new to new to android application project, to create a new project in Eclipse refer Creation of Android Project.

Coding:

Now let’€™s go to the coding section. This project requires following files.

Source Code:

  1. Image.java – Activity (main screen)

Activity Files:

  1. activity_image.xml – main screen/ layout

res – Values:

  1. strings.xml -€“ strings available in layout/activity

Manifest file:

  1. AndroidManifest.xml

Here are the coding for the above files.

Image.java:


package com.example.imageview; // Package Image View
import android.os.Bundle; // A mapping from String values to various Parcelable types.
import android.app.Activity; // Required to create an activity
import android.view.Menu; //Interface for managing the items in a menu.
public class Image extends Activity { // all class extends Activity
	@Override
	protected void onCreate(Bundle savedInstanceState) { // Create a new activity
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_image); // display activity image  when app starts
	}
}

activity_image.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-Setting the linear layout with vertical orientation-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/info">
<!-- Displays text for the app on the screen -->
<!-- Drawable right refers to the user defined image file in res/drawable -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/image"
android:drawableRight="@drawable/image1" android:padding ="5dp"/>

</LinearLayout>

Menu : activity_image.xml

    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    </menu>

[box type=”bio”] Note: Since there are no menu settings involved in this project, delete the contents of the menu settings. This may cause errors in R.java while building the project.[/box]

AndroidManifest.xml:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.imageview.Image"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<! -- Application Name -->

    <string name="app_name">Imageview</string>
<! -- Text message -->

    <string name="image">Image successfully loaded</string>


</resources>

Styles. xml:

<resources>
<! -- specify properties such as height, padding, font color, font size, background color -->


    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

Run the android application:

Android applications can be run on the android devices. You can either connect a hardware to the system to test the application or you can use the android virtual devices (AVD) Manager to create/manage the virtual devices running on emulator to run the application.

If you are new to create a Android Virtual Device (AVD), refer Creating Android Virtual Device.

To run the application/ project, Build your project using Project -> Build Project.

[box]

Build project
[/box]

This will show errors in Console window at the bottom of the working area in case your project contains.

If your build is successful, Run your application using Run -> Run or Press Ctrl +F11.

[box]

Run Project
[/box]

Upon running the application, the emulator will be launched which displays the AVD on your screen.

[box]

Start Emulator
[/box]

You can see your app with the image set during the android project creation in AVD.

[box]

AVD_Home screen_7
[/box]

Now the activity created for image view is shown as in the figure below with the image file available in drawable folder.

[box]

Image view
[/box]

Thus the android application project is executed successfully.

 

Click Here To Share This..!!