How to work with Glide in Android

This example demonstrates how to work with Glide

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.










Step 3 − Add the following code to src/MainActivity.java

package app.com.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   ImageView imageView;
   Button btnDrawableImage, btnUrlImage, btnErrorImage, btnPlaceholderImage, btnResizeImage, btnRotateImage;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      initView();
   }
   private void initView() {
      imageView = findViewById(R.id.imageView);
      btnDrawableImage = findViewById(R.id.btnDrawable);
      btnUrlImage = findViewById(R.id.btnUrl);
      btnPlaceholderImage = findViewById(R.id.btnPlaceholder);
      btnResizeImage = findViewById(R.id.btnResize);
      btnErrorImage = findViewById(R.id.btnError);
      btnRotateImage = findViewById(R.id.btnRotate);
      btnDrawableImage.setOnClickListener(this);
      btnPlaceholderImage.setOnClickListener(this);
      btnUrlImage.setOnClickListener(this);
      btnResizeImage.setOnClickListener(this);
      btnErrorImage.setOnClickListener(this);
      btnRotateImage.setOnClickListener(this);
   }
   @Override
   public void onClick(View view) {
      switch (view.getId()) {
         case R.id.btnDrawable:
         Glide.with(this).load(R.drawable.image).into(imageView);
         break;
         case R.id.btnPlaceholder:
      Glide.with(this).load("www.journaldev.com").placeholder(R.drawable.placeholder).into(imageView);
      break;
      case R.id.btnUrl:
      Glide.with(this).load("http://cdn.journaldev.com/wp-content/uploads/2017/01/android-constraint-      layoutsdk-tool-install.png").placeholder(R.drawable.placeholder).into(imageView);
      break;
      case R.id.btnError:
      Glide.with(this).load("www.journaldev.com").placeholder(R.drawable.placeholder).error(R.drawable.err
or).into(imageView);
      break;
      case R.id.btnResize:
      Glide.with(this).load(R.drawable.image).apply(new RequestOptions().override(200, 200)).into(imageView);
      break;
      case R.id.btnRotate:
      Glide.with(this).load(R.drawable.image).into(imageView);
      imageView.setRotation(90);
      break;
   }
   }
}

Step 4 − Add the following code to androidManifest.xml



   
      
      
         
         
      
   

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from the android studio, open one of your project's activity files and click the RunPlay Icon icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

Updated on: 2020-07-07T12:51:06+05:30

787 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements