Android Glide Tutorial with Example

Here you will get android glide tutorial with example.

Glide is an android library that allows us to fetch image from internet or url in single line of code. In one of my previous tutorial I have shown you the usage of picasso image library. Glide is a good alternative of Picasso library. Most of the features are common in both of them.

Glide is popular android image library which is recommended by Google and even Google used it in various applications. Below are some features of this library.

Android Glide Tutorial with Example

Features of Glide Image Library

  • Supports fetching of images, gifs and video stills.
  • Placeholder and error image can be added.
  • Support disk caching.
  • Image resize and cropping.

One of the biggest advantage of Glide over Picasso is that Glide supports gifs.

Android Glide Tutorial

Lets quickly jump to the actual tutorial part.

How to fetch image using Glide?

For this you just have to use one line of code given below.

Glide.with(context).load(IMAGE_URL).into(imageView);

Placeholder and Error Image

You can add a placeholder image until the image loaded from internet. You can also add an error image in case any error occurs while fetching image.

Glide.with(context)
	.load(IMAGE_URL)
	.placeholder(R.drawable.PLACEHOLDER_IMAGE_NAME)
	.error(R.drawable.ERROR_IMAGE_NAME)
	.into(imageView);

Loading GIFs

You can load a gif by just adding asGif() method.

Glide.with(context)
	.load(IMAGE_URL)
	.asGif()
	.into(imageView);

Resizing and Cropping Image

The following line of code resizes image to 300×300 pixel and make it center cropped.

Glide.with(context)
	.load(IMAGE_URL)
	.override(200, 200)
	.centerCrop()     
	.into(imageView);

Android Glide Example

Lets make a simple android app that shows the usage of Glide image library.

1. Create an android studio project with package name com.androidglide

2. Now include Glide library to your project by adding following line of code under dependencies section in app level build.gradle file.

compile 'com.github.bumptech.glide:glide:3.7.0'

3. Add internet access permission in AndroidManifest.xml file.

<uses-permission android:name="android.permission.INTERNET"/>

4. Add following code in respective files.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.androidglide.MainActivity"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Load Image"
        android:id="@+id/button"
        android:layout_marginBottom="5dp"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/image"/>

</LinearLayout>

MainActivity.java

package com.androidglide;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

public class MainActivity extends AppCompatActivity {
    String IMAGE_URL = "http://thecrazyprogrammer.com/wp-content/uploads/2015/09/Neeraj-Mishra.png";
    Button button;
    ImageView image;

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

        button = (Button)findViewById(R.id.button);
        image = (ImageView)findViewById(R.id.image);

        //load image on button click
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Glide.with(MainActivity.this).load(IMAGE_URL).into(image);
            }
        });
    }
}

5. Save and run the project.

The image is fetched from internet on button click, see below screenshot.

Screenshot

Android Glide Tutorial with Example

Comment below if you have any queries related to above glide android tutorial.

1 thought on “Android Glide Tutorial with Example”

Leave a Comment

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