5

I have infinite gallery based on this example :

http://blog.blundell-apps.com/infinite-scrolling-gallery/ ,

It runs nicely, now I want to have the Gallery still scroll through the images and under each image there should be a text caption.

I searched net with no result, would you please help me in coding that, just beginner in development.

==================================================================================

NEW Update :

upload photo explane what i need exactly which i get it with normal gallery (applied text to each image and able to customize text too as shown down image ,and each image has diffrenet text than others , but still not succeed to do it with infinite gallery :

enter image description here

PLEASE ANY HELP AND ADVICE .

THANKS ALOT.

Android Stack
  • 4,214
  • 6
  • 27
  • 49
  • 1
    Welcome to Stack Overflow :). Your question is a bit vague at the moment -- if you tell us what you've tried and where you got stuck, you're more likely to get a useful answer. – Andrew Aylett Mar 21 '12 at 10:07
  • @Blundell would you please check this post related to one of your blog code please , any advice will be appreciated : http://stackoverflow.com/questions/13496413/what-is-the-best-alternative-to-infinite-scrolling-gallery-view-with-text-descri – Android Stack Nov 21 '12 at 15:38

2 Answers2

2

I went through Blundell's tutorial and thanks to him I know how to make an Infinitelyscrolling gallery :)

To answer the question, about how to add a text caption below each of the images , I made same small changes to Blundell's nice tut and used some of his suggestions in the above answer and I think I got a nice way of doing the task.

The code below doesnt inflate or use gallery_item.xml at all, so it will increase the performance significantly compared to the way when you are inflating it every time.

Trimmed down code of classes from Blundell's tutorial ( because in the question, you are using only resources and not sdcard).

public class InfiniteScrollingGalleryActivity extends Activity {

public class GalleryItem{
      int imageId;
      String caption;
       public int getImageId() {
        return imageId;     }

    public String getCaption() {
        return caption;
    }
        public GalleryItem(int i,String s) {
        imageId=i;
        caption=s;  }   
}

int[] resourceImages = {R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
        R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     GalleryItem[] item = new GalleryItem[6];
        //initialising all items, change member variables according to needs
    for(int i=0;i<6;i++){
        item[i] = new GalleryItem(resourceImages[i], "pic no" +(i+1));          }                
    setContentView(R.layout.activity_main);
     InfiniteGallery galleryOne = (InfiniteGallery) findViewById(R.id.galleryOne);
    galleryOne.setResourceGallery(item);
     }  }

Here I have added the GalleryItem class array and passed it.

Also added the below code in InfiniteGalley class.

public void setResourceGallery(GalleryItem[] item) {
        setAdapter(new InfiniteGalleryResourceAdapter(getContext(), item));
        setSelection((getCount() / 2));
    }

below code's getView() is where the good things happen :

 public class InfiniteGalleryResourceAdapter extends BaseAdapter {

    /** The context your gallery is running in (usually the activity) */
    private Context mContext;   
 GalleryItem[] myItems; 

    public InfiniteGalleryResourceAdapter(Context context, GalleryItem[] item) {
        this.mContext = context;
            myItems=item;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // convertView is always null in android.widget.Gallery
        TextView t = new TextView(mContext);
        try {           
            int itemPos = (position % myItems.length);
            t.setText(myItems[itemPos].getCaption());
            Drawable d = mContext.getResources().getDrawable(myItems[itemPos].getImageId());
            ((BitmapDrawable) d).setAntiAlias(true); // Make sure we set anti-aliasing otherwise we get jaggies (non-smooth lines)

            //d.setBounds(0,0,60,60); //use this to change dimens of drawable,if needed
            t.setCompoundDrawablesWithIntrinsicBounds(null, d, null, null);

        } catch (OutOfMemoryError e) {
            // a 'just in case' scenario
            Log.e("InfiniteGalleryResourceAdapter", "Out of memory creating imageview. Using empty view.", e);
        }

        return t;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

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

    @Override
    public long getItemId(int position) {
        return position;
    }
    /** The width of each child image */
    private static final int G_ITEM_WIDTH = 120;
    /** The height of each child image */
    private static final int G_ITEM_HEIGHT = 80;
    private int imageWidth;
    private int imageHeight;
}

In getView(), I am just creating a textView and assigning the drawable to it using the handy t.setCompoundDrawablesWithIntrinsicBounds(null, d, null, null); . So it excludes the need of inflating layouts which is a heavy operation.

Below is the output image:

enter image description here

Akhil
  • 12,955
  • 6
  • 31
  • 39
  • REALLY THANKS FOR YOUR ANSWER , BUT WHAT REALLY I WANT TO SHOW DESCRIPTION DOWN EACH IMAGE NOT ONLY PIC NO RELATED TO POSITION AS I MEAN UNDER FIRST PIC SHOW EX: ( PIC TOOK IN MY HOME ) , SECOND PIC ( MY NICE PIC IN PARK) ,SOMETHING LIKE THIS , I HAVE TO SPACIFY different description FOE EACH IMAGE, THANKS ALOT " NOW IM IN WORK I CANT CHECK THE CODE WHEN I BACK HOME ILL DO . – Android Stack Apr 05 '12 at 09:28
  • Instead of text I set for position, just give a different string, for each gallery Item of array. That shouldn't be a problem. It works exactly according to what you asked in the question. – Akhil Apr 05 '12 at 09:30
  • please would you tell me how ( Instead of text I set for position, just give a different string, for each gallery Item of array. ) so that i can get different description for each image , thanks – Android Stack Apr 05 '12 at 23:46
  • Tell me from where are you getting the strings (if the answer works for the above question, you can award the bounty) – Akhil Apr 06 '12 at 06:23
  • @Akhil i dont have that experiance in java as you or Blundell thats why i asked ,i tried and i will try more till reach to my target, when i solve it ,sure i will tell you ,im still learning my dear , thanks again – Android Stack Apr 06 '12 at 15:33
  • @AndroidStack you are welcome to post any issues you are facing.. I will be glad to help. – Akhil Apr 06 '12 at 16:20
  • where are storing the descriptions? if you can add its code, or it would be better to start a new question – Akhil Apr 06 '12 at 16:32
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/9802/discussion-between-akhil-and-android-stack) – Akhil Apr 07 '12 at 06:17
  • 1
    @Akhil i accept and gave you the bounty really you deserve it , thanks alot man – Android Stack Apr 10 '12 at 10:45
  • @AndroidStack : thank you very much :) . We will chat some time friend. – Akhil Apr 10 '12 at 18:28
  • @Akhil would you please check mu new post , thnaks: http://stackoverflow.com/questions/13668588/infinite-scrolling-image-viewpager – Android Stack Dec 05 '12 at 10:33
0

In the adapter you can see the method: getView, you can see this method returns an ImageView, so now you want the getView method to return an imageview and textview...

U can do this in a few ways, here how you can do it with a LayoutInflater

View v = getLayoutInflater().inflate(R.layout.gallery_item, null);
((ImageView) v.findViewById(R.id.img)).setImageResource(imageIds[position]);
((TextView) v.findViewById(R.id.caption)).setText(captions[position]);

So in your res/layout folder you should have an 'gallery_item' layout that contains an ImageView (img) and a TextView (caption)

i.e.

gallery_item.xml

 <LinearLayout>
      <ImageView ... />
      <TextView ... />
 </LinearLayout>

Hope this was helpfull!

EDIT

so as the above example shows you would need two arrays, one of imageIds and one of textCaptions. To keep your Adapter nice and clean it's screaming for you to make an object.

 public class GalleryItem {
       int imageId;
       String caption

       // Constructor

       // getters and setters

 }

You could then pass an Array or List of your GalleryItems to the Adapter (replacing the setAdapter method). i.e:

 GalleryItem[] items = new GalleryItem[];

Then in your getView method as outlined above you would extract each object:

 GalleryItem item = items[position];
 View v = getLayoutInflater().inflate(R.layout.gallery_item, null);

((ImageView) v.findViewById(R.id.img)).setImageResource(item.getImageId());
((TextView) v.findViewById(R.id.caption)).setText(item.getCaption());

Hope thats clear

Blundell
  • 69,653
  • 29
  • 191
  • 215
Ferdau
  • 1,505
  • 1
  • 11
  • 20