1

I'm using SurfaceView in my 1st activity. On clicking an image using SurfaceView, I move to 2nd activity also storing path of the recently clicked image. In 2nd activity, I want to display that image in Gallery.

In 2nd activity on a button click, again I move to 1st activity and after clicking picture, I move to 2nd activity.

Now in 2nd activity, there will 2 images that should be displayed in Gallery.

My problem is after clicking 1st image, Gallery is not displaying anything. Note that getView() of adapter set for Gallery is getting called.

But after clicking 2nd image onwards, when there are 2 or more images in Gallery, they are getting displayed.

I also tried g.refreshDrawableState(); after

g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(this);     
g.setSelection(0, false);

but to no avail.

I know what I have written here sounds complex, but try to visualize the scenario.

Any help appreciated.

Edit

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {
        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mImageIds.length;//mImageIds is Array of image paths
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(mContext);

            final String imageUri = mImageIds[position].trim();
            try {
                if (imageUri.length() != 0) {
                    File f = new File(imageUri);
                    if (f.exists()) {
                        b = constants.shrinkBitmap(f.getPath(), 800, 800);
                        d = new BitmapDrawable(getResources(), b);
                        i.setImageDrawable(d);
                    } else {
                        i.setBackgroundColor(Color.WHITE);
                        i.setImageResource(R.drawable.ic_launcher);
                    }
                } else {
                    i.setBackgroundResource(R.drawable.ic_launcher);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            i.setAdjustViewBounds(true);
//BELOW "length" is some predefined value
            i.setPadding(Math.round(length * 0.05f), 0,
                    Math.round(length * 0.05f), 0);
            i.setLayoutParams(new Gallery.LayoutParams(Math
                    .round(length * 0.9f),
                    LayoutParams.WRAP_CONTENT));
            i.setScaleType(ScaleType.FIT_XY);
            return i;
        }
    }

Edit (2)

After Vikram's suggestion, I tried logging length value and it was indeed 0 for the first time i.e. when calling i.setLayoutParams(new Gallery.LayoutParams(Math.round(length * 0.9f),LayoutParams.WRAP_CONTENT));

I set this length variable just after onCreate() by this way:

g.post(new Runnable() {
     public void run() {
       length = g.getRight() - g.getLeft();
     }
    });

as I want to retrieve width of my parent layout in code and accordingly set Gallery item width.

I know the reason I get length as 0 is setContentView() takes bit of time to take effect. How can I retrieve before Adapter is called?

GAMA
  • 5,958
  • 14
  • 79
  • 126
  • Can you post your adapter code? – Vikram Jul 19 '13 at 12:09
  • In case of the 1st image click, check the contents of `mImageIds` using Logcat. Do this in `getView()`. Your adapter looks okay to me. – Vikram Jul 19 '13 at 13:09
  • `mImageIds` do have image URI's. Also `g.getChildCount` also gives 0+ result. – GAMA Jul 19 '13 at 13:31
  • Double check the value of `length`. It might be set to `zero` for the first item. – Vikram Jul 19 '13 at 14:06
  • Oh gosh. How did I missed that. Please check my edited question. – GAMA Jul 19 '13 at 14:29
  • How about you set `length` to the screen width? Or, you can call `myAdapter.notifyDataSetChanged()` from inside the `run()` method, after computing `length`. You will need to initialize your `myAdapter` variable before calling `g.post()`. – Vikram Jul 19 '13 at 14:55
  • Done that and it solved the issue. But it generates the the issue that is layout is loaded WITHOUT gallery item first. then it blinks and gallery is populated. I solved it by setting visibility to invisible initially and only after populating gallery, display the whole layout. – GAMA Jul 19 '13 at 15:01
  • Just integrate all your comments and post as answer. I'll accept it. And by the way, thanks a ton ! cheers. – GAMA Jul 19 '13 at 15:02

1 Answers1

1

The variable length seems to be the problem here. If it's value is zero, when getView() for the first item is called, the width parameter is set to zero.

In your edit, length is indeed getting a non-zero value, perhaps a bit late. You can make the following call to update the view:

myAdapter.notifyDataSetChanged();

This call should be placed inside run() after the computation of length.

Your workaround for the 'blinking' is fine. As an alternative(and if the gallery's width should match screen width), try this:

In place of:

g.post(new Runnable() {
 public void run() {
   length = g.getRight() - g.getLeft();
 }
});

put:

Point pointSize = new Point();
getWindowManager().getDefaultDisplay().getSize(pointSize);
length = pointSize.x;

Place this before initializing the adapter.

Another thing to note here is that g.getRight() is computed as:

g.getRight() = g.getLeft() + g.getWidth()

So, to compute length, you may as well do:

length = g.getWidth()

as:

getWidth() = getRight() - getLeft()
Vikram
  • 51,313
  • 11
  • 93
  • 122
  • For `Point` funda, I get *The method getSize(Point) is undefined for the type Display* error. – GAMA Jul 22 '13 at 11:10
  • So I used accepted answer of [this que.](http://stackoverflow.com/questions/9654016/getsize-giving-me-errors) – GAMA Jul 22 '13 at 11:12