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

  • SEARCH
  • Home
  • 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 5930709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:35:28+00:00 2026-05-22T14:35:28+00:00

There are two things I want to achieve with the Gallery view: 1) when

  • 0

There are two things I want to achieve with the Gallery view:

1) when a user makes a selection within the Gallery view I do not want the images to move (i.e. I do not want the selected image to move to the center)

2) when a user makes a selection within the Gallery view I want the selected image to be highlighted orange on ACTION_DOWN, and the highlight removed on ACTION_UP – just like on Buttons.

How is that done ?

  • 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-22T14:35:29+00:00Added an answer on May 22, 2026 at 2:35 pm

    Solved it myself …

    the following features are included in this sample code

    1. apparently-endless horizontal
      scrolling
    2. selection detection without using
      the GODAWFUL
      Gallery.setOnItemSelectedListener().
    3. highlighting the selection
      (although this needs more work –
      namely storing the selection because
      the Gallery will lose the
      highlighting after it’s scrolled
      off-screen)

    public class ActivityMine extends Activity implements OnTouchListener
    {
        //UI references
        private Gallery mGallery;
    
        //member variables
        private float fX, fY;
        private ImageBuffer mImageBuffer;//just an array of images
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.filter);
    
            //get references to UI components
            mGallery = (Gallery) findViewById(R.id.filter_gallery);
    
            //make widgets touch sensitive
            mGallery.setOnTouchListener(this);
    
            //connect Gallery with adapter
            mGallery.setAdapter(new GalleryAdapter(this, mImageBuffer));
    
            //auto-select first image (position 1073741820)
            final int iTemp = (int)( Integer.MAX_VALUE / 2 ) - ( ( Integer.MAX_VALUE / 2 ) % mImageBuffer.getCount() );
            mGallery.setSelection(iTemp);
        }
    
    
        @Override
        protected void onDestroy()
        {
            super.onDestroy();
        }
    
    
        public boolean onTouch(View v, MotionEvent event)
        {
            final int actionPerformed = event.getAction();
            final int widgetID = v.getId();
    
            if (actionPerformed == MotionEvent.ACTION_DOWN)
            {
                if (widgetID == R.id.filter_gallery)
                {
                    fX=event.getRawX();
                    fY=event.getRawY();
                }
            }
    
            if (actionPerformed == MotionEvent.ACTION_UP)
            {
                switch (widgetID)
                {
                    case R.id.filter_gallery:
                    {
                        final float posX = event.getRawX();
                        final float posY = event.getRawY();
    
                        final float MAXPRESSRANGE = 10.0f;
    
                        //detect if user performed a simple press
                        if ((posX < fX+MAXPRESSRANGE) && (posX > fX-MAXPRESSRANGE))
                        {
                            if ((posY < fY+MAXPRESSRANGE) && (posY > fY-MAXPRESSRANGE))
                            {
                                //valid press detected!
    
                                //convert gallery coordinates to a position
                                final int iPosition = mGallery.pointToPosition((int)event.getX(), (int)event.getY());
    
                                if (iPosition != AdapterView.INVALID_POSITION)
                                {
                                    //this is necessary to obtain the index of the view currently visible and pressed
                                    final int iVisibleViewIndex = iPosition - mGallery.getFirstVisiblePosition();
    
                                    //get a reference to the child and modify the border
                                    View child = mGallery.getChildAt(iVisibleViewIndex);
                                    if (child != null)
                                    {
                                        RelativeLayout borderImg = (RelativeLayout)child;
                                        borderImg.setBackgroundColor(0xFFFFFFFF);
                                    }
                                }
    
                                // consume event
                                return true;
                            }
                        }
                    }//end of: case R.id.filter_gallery
                }//end of: switch()
            }
    
            return false;//do not consume event otherwise the UI widget touched won't receive the event
        }
    
    
        public class GalleryAdapter extends BaseAdapter
        {
            //member variables
            private Context mContext;
            private ImageBuffer mImageBuffer;
    
    
            public GalleryAdapter(Context context, ImageBuffer imageBuffer)
            {
                mContext = context;
                mImageBuffer = imageBuffer;
            }
    
            //get count of images in the gallery
            public int getCount()
            {
                return Integer.MAX_VALUE;
            }
    
            public Object getItem(int position)
            {
                if (position >= mImageBuffer.getCount())
                { 
                    position = position % mImageBuffer.getCount(); 
                }
                return position;
            }
    
            public long getItemId(int position)
            {
                if (position >= mImageBuffer.getCount())
                { 
                    position = position % mImageBuffer.getCount(); 
                }
                return position;
            }
    
            //returns the individual images to the widget as it requires them
            public View getView(int position, View convertView, ViewGroup parent)
            {
                final ImageView imgView = new ImageView(mContext);
    
                if (position >= mImageBuffer.getCount())
                { 
                    position = position % mImageBuffer.getCount(); 
                }
    
                imgView.setImageBitmap(mImageBuffer.getBitmapFromBuffer(position));
    
                //put black borders around the image
                final RelativeLayout borderImg = new RelativeLayout(mContext);
                borderImg.setPadding(5, 5, 5, 5);
                borderImg.setBackgroundColor(0xff000000);
                borderImg.addView(imgView);
                return borderImg;
            }
        }
    }
    
    
    
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import android.app.Activity;
    import android.content.res.AssetManager;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Environment;
    
    public class ImageBuffer
    {
        //member variables
        private Activity mActivity;
        private Map<String,Integer> mIndexMap;
        private List<Bitmap> mBitmapList;
        private int mImageCount;
    
    
        public ImageBuffer(Activity activity)
        {
            mActivity = activity;
            mIndexMap = new HashMap<String,Integer>();
            mBitmapList = new ArrayList<Bitmap>();
            mImageCount=0;
        }
    
    
        public int getCount()
        {
            return mImageCount;
        }
    
    
        public int putAssetBitmapInBuffer(String assetfilename, String overrideFilename)
        {
            int iValueToReturn = -1;
    
            try
            {
                Bitmap bitmap = getBitmapFromAsset(assetfilename);
                if (bitmap != null)
                {
                    final int iIndex = mBitmapList.size();
                    if (overrideFilename == null)
                    {
                        mIndexMap.put(assetfilename, iIndex);
                    }
                    else
                    {
                        mIndexMap.put(overrideFilename, iIndex);
                    }
                    mBitmapList.add(bitmap);
                    iValueToReturn = iIndex;
                    mImageCount++;
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
    
            return iValueToReturn;
        }
    
    
        /**
         * put an image from filesystem folder into buffer
         * @param filename name of file, i.e. "image.png"
         * @param strSubFolder subfolder, i.e. "/products"
         * @param bGetFromSDCARD when true the image is read from SDCARD, otherwise main memory
         * @return on success: index of image in buffer. on error: -1
         */
        public int putLocalFSbitmapInBuffer(String filename, String strSubFolder, boolean bGetFromSDCARD)
        {
            int iValueToReturn = -1;
    
            Bitmap bitmap = getBitmapFromLocalFS(filename, strSubFolder, bGetFromSDCARD);
            if (bitmap != null)
            {
                final int iIndex = mBitmapList.size();
                mIndexMap.put(filename, iIndex);
                mBitmapList.add(bitmap);
                iValueToReturn = iIndex;
                mImageCount++;
            }
    
            return iValueToReturn;
        }
    
    
        public Bitmap getBitmapFromBuffer(String assetfilename)
        {
            Bitmap bitmap = null;
    
            if (mIndexMap.containsKey(assetfilename))
            {
                final int iIndex = mIndexMap.get(assetfilename);
                bitmap = mBitmapList.get(iIndex);
            }
    
            return bitmap;
        }
    
    
        public Bitmap getBitmapFromBuffer(int position)
        {
            Bitmap bitmap = null;
    
            if (position < mBitmapList.size() && position > -1)
            {
                bitmap = mBitmapList.get(position);
            }
    
            return bitmap;
        }
    
    
        /**
         * get bitmap from assets folder in project
         * @param assetfilename name of asset, i.e. "image.png"
         * @return Bitmap
         * @exception IOException when file is not found
         */
        private Bitmap getBitmapFromAsset(String assetfilename) throws IOException
        {
            AssetManager assetManager = mActivity.getAssets();
    
            InputStream istr = assetManager.open(assetfilename);
            Bitmap bitmap = BitmapFactory.decodeStream(istr);
            istr.close();
    
            return bitmap;
        }
    
    
        /**
         * get bitmap from file system
         * @param filename name of image, i.e. "image.png"
         * @param strSubFolder subfolder, i.e. "/products"
         * @param bGetFromSDCARD when true the image is read from SDCARD, otherwise main memory
         * @return Bitmap
         */
        private Bitmap getBitmapFromLocalFS(String filename, String strSubFolder, boolean bGetFromSDCARD)
        {
            File file;
    
            if (bGetFromSDCARD == true)
            {
                //store in SDCARD
                file = new File(Environment.getExternalStorageDirectory()+strSubFolder, filename);
            }
            else
            {
                //store in main memory
                file = new File(mActivity.getFilesDir().getAbsolutePath()+strSubFolder, filename);
            }
    
    
            Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
    
            return bitmap;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure about one thing. There are two things in object oriented programming.
There are two things that seem to be popular nowadays and I was wondering
I have a small app using Drive.Info. I want to do two things. Check
I notice there is a AutoGenerateSelectionButton function but it's not really what I want.
what I want to achieve is this: As you can see there is are
I want these two print functions to do the same thing: unsigned int Arraye[]
I have two things I need this for, these should explain what I mean:
The thing I hate most in CSS3 is that there is always two properties
I have read a document that they say: In java there two types of
There are two options when setting up your JBPM session. You can put your

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.