Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7006813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:31:33+00:00 2026-05-27T21:31:33+00:00

I’m creating a gallery section for my app which will be used to show

  • 0

I’m creating a gallery section for my app which will be used to show an X amount of images. These images are being loaded from an external server. The user is shown a GridView containing thumbnails of the images, and upon clicking on these thumbnails the Gallery-view is opened which shows the image in full-screen mode.
An ArrayList is used to hold the String objects containing the links to these images. In the getView method of my Adapter this ArrayList is used to see which image needs to be shown.

So far this works fine, but I’ve run into a problem when a user picks a different image then the first image in the GridView. When the user clicks the 7th image for example, the Gallery shows the 7th image. But when the user flings to the right to see the previous images (6th image and before), nothing happens. When he flings to the left to see the upcoming images the user is presented with the 2nd items of the list, then the 3rd, then the 4th etc.

Obviously, the Gallery thinks that whatever image I provide it with first, is actually the first item on the list, and starts showing everything after that as it should have if the image was actually the first. But, the Gallery should show the image as the actual position it was on the GridView. How can I achieve something like that? I’ve tried messing around with the position I get when clicking my GridView, but this just results in the behavior I have described above.

Here’s the code I used for this:

PhotoFullView (the gallery and adapter for the gallery)

package com.mobowski.appfrag.json.pictures;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import com.mobowski.appfrag.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class PhotoFullView extends Activity {
    /** Called when the activity is first created. */
    CustomGallery gallery;
    public ArrayList<String> links;
    private int pos;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.full_photo_gallery);

        Bundle bun = getIntent().getExtras();
        links = bun.getStringArrayList("links");
        pos = bun.getInt("pos");

        /*
         * Find the gallery defined in the main.xml Apply a new (custom)
         * ImageAdapter to it.
         */
        gallery = (CustomGallery) findViewById(R.id.gallery);
        gallery.setAdapter(new ImageAdapter(this, links, pos));
        gallery.setSpacing(25);

    }

    public class ImageAdapter extends BaseAdapter {
        /** The parent context */
        private Context myContext;
        private ArrayList<String> links;
        int pos;

        Display display = getWindowManager().getDefaultDisplay();
        int width = display.getWidth();
        int height = display.getHeight();

        /**
         * All images to be displayed. Put some images to project-folder:
         * '/res/drawable/uvw.xyz' .
         */

        /** Simple Constructor saving the 'parent' context. */
        public ImageAdapter(Context c, ArrayList<String> links, int pos) {
            this.myContext = c;
            this.links = links;
            this.pos = pos;
        }

        /** Returns the amount of images we have defined. */
        public int getCount() {
            return this.links.size();
        }

        /* Use the array-Positions as unique IDs */
        public Object getItem(int position) {
            return position;
        }

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

        /**
         * Returns a new ImageView to be displayed, depending on the position
         * passed.
         */
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(this.myContext);

            String imageURL = links.get(position);
            if (position == 0) {
                imageURL = links.get(pos);
            }
            try {
                URL aURL = new URL(imageURL);
                URLConnection conn = aURL.openConnection();
                conn.connect();
                InputStream is = conn.getInputStream();
                /* Buffered is always good for a performance plus. */
                BufferedInputStream bis = new BufferedInputStream(is);
                /* Decode url-data to a bitmap. */
                Bitmap bm = BitmapFactory.decodeStream(bis);

                bis.close();
                is.close();
                /* Apply the Bitmap to the ImageView that will be returned. */
                i.setImageBitmap(bm);
            } catch (Exception e) {
                e.printStackTrace();
            }

            /* Image should be scaled as width/height are set. */
            // i.setScaleType(ImageView.ScaleType.FIT_XY);
            // /* Set the Width/Height of the ImageView. */
            // i.setLayoutParams(new Gallery.LayoutParams());
            return i;
        }

    }
}

Custom gallery class (Used to overload onFling so it only shows 1 image at a time)

package com.mobowski.appfrag.json.pictures;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Gallery;

public class CustomGallery extends Gallery{

    public CustomGallery(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
      return super.onFling(e1, e2, 0, velocityY);
    }

}

I just managed to find out how to keep the position correctly when scrolling further after picking a custom starting image using

String imageURL = links.get(pos+position);

where pos is the position of the image clicked in the GridView. However, this still does not give me the chance to scroll back to the images BEFORE the image clicked.


Well, that was way easier then I thought.
After reading the documentation again I found that I can just use setSelection(position) on my Gallery object to start at a specific position. If only all problems were this easy to fix. Thanks for reading though!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:31:34+00:00Added an answer on May 27, 2026 at 9:31 pm

    Well, that was way easier then I thought.
    After reading the documentation again I found that I can just use setSelection(position) on my Gallery object to start at a specific position. If only all problems were this easy to fix. Thanks for reading though!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.