Show Progressbar on Loading ImageView image via http URL programmatically

Dynamically display imageview image from website url and display progress bar while loading image into imageview.

In this android application application development tutorial we are displaying a progress bar while loading image inside imageview and after finish loading we are hiding the progress bar from activity screen. So here is the complete step tutorial for Show Progressbar on Loading ImageView image via http URL programmatically.

android-project-download-code-button

How to Show Progressbar on Loading ImageView image via http URL programmatically.

Note: Please add internet permission into AndroidManifest.xml file.

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

Code for MainActivity.java file.

 package com.showprogressbarimageloading_android_examples.com;

import java.io.InputStream;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
public class MainActivity extends Activity {

 Button ImageLoadButton;
 ImageView SetImageViewHolder;
 Bitmap Imagebitmap;
 String ImageUrl = "https://www.android-examples.com/wp-content/uploads/2016/01/image_view_check.png";
 ProgressBar pbar;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 ImageLoadButton = (Button)findViewById(R.id.button1);
 
 SetImageViewHolder = (ImageView)findViewById(R.id.imageView1);
 
 pbar = (ProgressBar)findViewById(R.id.progressBar1);
 
 pbar.setVisibility(View.GONE);
 
 ImageLoadButton.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View arg0) {
 // TODO Auto-generated method stub
 new ImageLoaderClass().execute(ImageUrl);
 }
 });
 
 
 }
 private class ImageLoaderClass extends AsyncTask<String, String, Bitmap> {
 @Override
 protected void onPreExecute() {
 super.onPreExecute();
 
 pbar.setVisibility(View.VISIBLE);
 
 }
 protected Bitmap doInBackground(String... args) {
 try {
 Imagebitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());
 
 } catch (Exception e) {
 e.printStackTrace();
 }
 return Imagebitmap;
 }
 
 protected void onPostExecute(Bitmap image) {
 
 if(image != null){
 SetImageViewHolder.setImageBitmap(image);
 
 pbar.setVisibility(View.GONE);
 
 }
 }
 }
 
 }

Code for activity_main.xml layout file.

 <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.showprogressbarimageloading_android_examples.com.MainActivity" >

 <ImageView
 android:id="@+id/imageView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="100dp"
 
 />

 <ProgressBar
 android:id="@+id/progressBar1"
 style="?android:attr/progressBarStyleLarge"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:text="Click here to Show Progressbar on Loading ImageView image via http URL programmatically"
 android:layout_marginTop="290dp" />

</RelativeLayout>

 

Code for AndroidManifest.xml file.

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

 <uses-sdk
 android:minSdkVersion="8"
 android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
 <application
 android:allowBackup="true"
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name"
 android:theme="@style/AppTheme" >
 <activity
 android:name=".MainActivity"
 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>

Screenshots:

Show Progressbar on Loading ImageView image via http URL programmatically

done image loading

Click here to download Show Progressbar on Loading ImageView image via http URL programmatically project.