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 6739615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:30:36+00:00 2026-05-26T11:30:36+00:00

I’m making an app that requires the use of a Gallery . My problem

  • 0

I’m making an app that requires the use of a Gallery. My problem is, the Gallery is really choppy. My code is pretty much similar to the sample code that android provides, but rather than just having the ImageAdapter return an ImageView, I have it return a LinearLayout, because I need text underneath the image. Any suggestions?

Here’s my code:

package org.example.gallery;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class GalleryTestActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Gallery gallery = (Gallery) this.findViewById(R.id.gallery);
    gallery.setAdapter(new ItemAdapter(this));

    gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
            int position, long id) {
        Toast.makeText(GalleryTestActivity.this, "" + position,
            Toast.LENGTH_SHORT).show();
        }
    });
    }

    public class ItemAdapter extends BaseAdapter {
    private Context mContext;

    private final Integer[] mImageIds = { R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,
        R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_8 };

    private final String[] mStringIds = { "1-pc. Chicken with Rice",
        "2-pc. Chicken with Rice", "Tower Burger", "Bucket", "Barrel",
        "Chicken Fillet", "Chicken burger", "Another Chicken Burger" };

    public ItemAdapter(Context c) {
        this.mContext = c;
    }

    public int getCount() {
        return mImageIds.length;
    }

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        LinearLayout l = new LinearLayout(this.mContext);
        l.setOrientation(LinearLayout.VERTICAL);

        ImageView iv = new ImageView(this.mContext);
        iv.setImageResource(mImageIds[position]);
        iv.setScaleType(ImageView.ScaleType.FIT_CENTER);

        TextView tv = new TextView(this.mContext);
        tv.setText(mStringIds[position]);
        tv.setTextSize(20);
        tv.setTextColor(Color.RED);
        tv.setGravity(Gravity.CENTER_HORIZONTAL);

        l.addView(iv);
        l.addView(tv);

        return l;
    }
    }
}
  • 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-26T11:30:36+00:00Added an answer on May 26, 2026 at 11:30 am

    use ViewHolder(so u will not inflating/creating view in every getView call(views are recycling)) pattern like in this example:

    http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

    EDIT:

           static class ViewHolder {
                TextView tv;
                ImageView iv;
            }
    
       public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
            if(convertView == null){
    
            LinearLayout l = new LinearLayout(this.mContext);
            l.setOrientation(LinearLayout.VERTICAL);
    
            ImageView iv = new ImageView(this.mContext);
    
            iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
    
            TextView tv = new TextView(this.mContext);
    
            tv.setTextSize(20);
            tv.setTextColor(Color.RED);
            tv.setGravity(Gravity.CENTER_HORIZONTAL);
    
            l.addView(iv);
            l.addView(tv);
            convertView = l;
            holder = new ViewHolder();
            holder.tv = tv;
            holder.iv = iv;
    
            } else {
            holder = (ViewHolder)convertView.getTag();
        }
        holder.tv.setText(mStringIds[position]);
        holder.iv.setImageResource(mImageIds[position]);
    
            return convertView;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.