1

I am doing an app in which user selects an image from gallery and goes to second activity with the selected image from gallery and displays it in second activity.But it takes some time(approx 3 sec) to go to second activity after user clicks on an image in gallery.I want to display progress bar circle for that much of time after the user selects an image from gallery and want to make progress bar circle invisible when execution moves to second activity.I am not getting any idea how to do my task?Should I use any AsyncTask?Please help me.I am stuck here. I am providing my sample code.

My first Activity is

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
 import android.widget.ImageButton;



 public class LauncherActivity extends Activity 
{
private static int RESULT_LOAD_IMAGE = 2;
ImageButton gallery;
Bitmap bitmap_for_gallery;



 protected void onCreate(Bundle paramBundle)
 {
super.onCreate(paramBundle);
setContentView(R.layout.launcher);
gallery = (ImageButton)findViewById(R.id.select_photo);

  gallery.setOnClickListener(new OnClickListener() {

    @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub          
        Intent gallery_intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(gallery_intent, RESULT_LOAD_IMAGE);

    }
  });
    }
       protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)     {
      Uri selectedImage = data.getData();    
     ProgressDialog progress=new ProgressDialog(getApplicationContext());
     progress.setIndeterminate(true);
     progress.setTitle("Please wait");
     progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
     progress.show();


      String[] filePathColumn = { MediaStore.Images.Media.DATA };
      Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
      cursor.moveToFirst();
      int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
      String picturePath = cursor.getString(columnIndex);
      cursor.close();

      progress.dismiss();

      Intent intent = new Intent(LauncherActivity.this, MainActivity.class);
      intent.putExtra("path", picturePath); 
      startActivity(intent); 
     }
 }
}

my first activity xml

  <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent"
 >
  <ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:src="@drawable/homepage"
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentBottom="true" 
    android:scaleType="fitXY"/>

  <ImageButton
    android:id="@+id/select_photo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="126dp"
    android:background="@android:color/transparent"
    android:src="@drawable/select_photo" />

  </RelativeLayout>

my second activity is

   import android.app.Activity;
   import android.graphics.Bitmap;
   import android.graphics.BitmapFactory;
   import android.os.Bundle;
  import android.widget.FrameLayout;
  import android.widget.ImageView;
  import android.widget.RelativeLayout.LayoutParams;


  public class MainActivity extends Activity {
  ImageView background;
  Bitmap transfered;
   FrameLayout.LayoutParams layoutParams;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  background=(ImageView)findViewById(R.id.imageView1);

 layoutParams=new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
 Bundle extras = getIntent().getExtras();

    String picturePath=extras.getString("path");
     transfered=BitmapFactory.decodeFile(picturePath);  
 background.setImageBitmap(transfered); 
 background.setAdjustViewBounds(true);
 background.setLayoutParams(layoutParams);
}

  }

My second activity xml is

  <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.progressbarcircle.MainActivity"
  tools:ignore="MergeRootFrame" >

  <ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher"
    android:layout_gravity="center" />

  </FrameLayout>

Thanks in advance.please help me.

Lucky
  • 48
  • 1
  • 8

1 Answers1

0

If your first Activity is taking too much time create a ProgressDialog object and show() it after

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)    {
  Uri selectedImage = data.getData();          

and dismiss() it before startActivity(intent);

You can do the same for the second Activity if it takes too long.

Apoorv
  • 13,470
  • 4
  • 27
  • 33
  • can u please provide sample code to get the progress bar circle for the time taken to move to second activity. – Lucky Jun 04 '14 at 12:01
  • Did you place it in both activities? – Apoorv Jun 04 '14 at 12:02
  • Place it in the `onCreate` of second one too. – Apoorv Jun 04 '14 at 12:03
  • If i wrote my question wrong,sorry for that.I am moving from first activity to second activity.it is taking time.For that much of time,I need to display the progress bar circle after user selects image from gallery. – Lucky Jun 04 '14 at 12:07
  • That is probably because the `onCreate` of your second `Activity` is taking time to process – Apoorv Jun 04 '14 at 12:09
  • no,It is not taking much time.But i am unable to get progress bar circle. – Lucky Jun 04 '14 at 12:11
  • I have modified my question and first activity.please check it. – Lucky Jun 04 '14 at 12:20