free web tracker
Course Content
Java Programming Basics for Android
Learn the basics of Java programming for Android app development using Android Studio. This guide covers key concepts like variables, loops, and classes to help you start building your first Android apps with confidence. Perfect for beginners!
0/10
Android UI with XML
Create stunning Android interfaces using XML in Android Studio. Learn to design responsive layouts and UI elements with Java integration for dynamic app experiences. Perfect for developers aiming to build professional Android apps.
0/7
Mastering Java Android Development – Beginner

In Android development, ImageView is a commonly used widget to display images. This tutorial will show you how to use ImageView in Java with Android Studio, including how to display images from the drawable folder, and how to load images from a URL using a library like Glide.

Step 1: Add ImageView to Layout XML

Open your activity_main.xml and add the following:

image-1024x677 Using ImageView to Display Images

<ImageView
    android:id="@+id/myImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/sample_image"
    android:contentDescription="Sample Image"
    android:layout_gravity="center"
/>

Make sure you have an image named sample_image.jpg or sample_image.png inside the res/drawable/ folder.

Step 2: Reference ImageView in Java Code

Open your MainActivity.java and use the following code:

image-1 Using ImageView to Display Images

import android.os.Bundle;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    ImageView myImageView;

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

        myImageView = findViewById(R.id.myImageView);
        
        // Optional: Set image programmatically
        myImageView.setImageResource(R.drawable.sample_image);
    }
}

Load Image from URL (Using Glide Library)

To load images from a URL, add Glide dependency in your build.gradle:

image-3 Using ImageView to Display Images

implementation 'com.github.bumptech.glide:glide:4.16.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.16.0'

Then load the image like this:

image-4 Using ImageView to Display Images

import com.bumptech.glide.Glide;

// Inside onCreate()
String imageUrl = "https://example.com/image.jpg";
Glide.with(this)
     .load(imageUrl)
     .into(myImageView);

Final Tips:

    • Always use contentDescription for accessibility.
    • Use wrap_content or match_parent wisely to control image size.
    • Use Glide or Picasso to load large images efficiently.