A android.widget.Gallery - Example

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Re: A android.widget.Gallery - Example

Postby Efefes » Sat Dec 11, 2010 8:47 am

Thanks, I got everything working now.
Next I'd like to look into making the gallery loop
Efefes
Freshman
Freshman
 
Posts: 6
Joined: Thu Dec 09, 2010 2:18 pm

Top

Re: A android.widget.Gallery - Example

Postby byteadventurer » Fri Jan 28, 2011 11:13 pm

Hello Devs!

I have been searching on how to do this but unable to figure it out.

I have done the tutorial and added an ImageView and TextView to update with information when the Gallery Item is clicked. This all works very well, but the issue is that I would like to update the ImageView and TextView when the Gallery has Scrolled and the center item changed with out the need for a click.

Is there a way to do this? Very similar to the stock Wallpaper selection on older Android devices, also like LauncherPro Wallpaper Gallery Selector.

To further clarify, I'm looking to get the new position of the centered gallery item after a scroll.

Thanks for any suggestions!
byteadventurer
Freshman
Freshman
 
Posts: 2
Joined: Sat Nov 20, 2010 10:49 pm

Re: A android.widget.Gallery - Example

Postby mithat » Fri Feb 18, 2011 3:53 am

plusminus wrote:
android.widget.Gallery - Example


What you learn: You will learn how to use the Gallery-Widget (android.widget.Gallery) to display a series of images (/res/drawable/s).

:?: Problems/Questions: Write them right below...

Difficulty: 1 of 5 :)

What it will look like:
Image


Description:
0.) Lets start with the XML-File which will result in the Screenshot shown above. It is very simple, just a TextView followed by a Gallery(with id: "gallery"):
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent"
  6.    >
  7. <TextView  
  8.    android:layout_width="fill_parent"
  9.    android:layout_height="wrap_content"
  10.    android:text="GalleryExample - by anddev.org"
  11.    />
  12. <Gallery id="@+id/gallery"
  13.        android:layout_width="fill_parent"
  14.        android:layout_height="wrap_content"
  15.        android:gravity="bottom"
  16. />
  17. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


1.) Now to the EntryPoint of all Activities, the onCreate(...) method. It loads the previously defined main.xml and applies an Adapter (ImageAdapter) to the Gallery.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class GalleryExample extends Activity {
  2.     /** Called when the activity is first created. */
  3.     @Override
  4.     public void onCreate(Bundle icicle) {
  5.         super.onCreate(icicle);
  6.         setContentView(R.layout.main);
  7.  
  8.         /* Find the gallery defined in the main.xml
  9.          * Apply a new (custom) ImageAdapter to it. */
  10.         ((Gallery) findViewById(R.id.gallery))
  11.                         .setAdapter(new ImageAdapter(this));
  12.     }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


2.) The basic setup of the ImageAdapter
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     public class ImageAdapter extends BaseAdapter {
  2.         /** The parent context */
  3.         private Context myContext;
  4.  
  5.         /** All images to be displayed.
  6.          * Put some images to project-folder:
  7.          * '/res/drawable/uvw.xyz' .*/
  8.         private int[] myImageIds = {
  9.                         R.drawable.image_1,
  10.                         R.drawable.image_2,
  11.                         R.drawable.image_3,
  12.                         R.drawable.image_4
  13.         };
  14.         /** Simple Constructor saving the 'parent' context. */
  15.         public ImageAdapter(Context c) { this.myContext = c; }
  16.  
  17.         /** Returns the amount of images we have defined. */
  18.         public int getCount() { return this.myImageIds.length; }
  19.  
  20.         /* Use the array-Positions as unique IDs */
  21.         public Object getItem(int position) { return position; }
  22.         public long getItemId(int position) { return position; }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


3.) The 'important' methods of the ImageAdapter

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         /** Returns a new ImageView to
  2.          * be displayed, depending on
  3.          * the position passed. */
  4.         public View getView(int position, View convertView, ViewGroup parent) {
  5.             ImageView i = new ImageView(this.myContext);
  6.  
  7.             i.setImageResource(this.myImageIds[position]);
  8.             /* Image should be scaled as width/height are set. */
  9.             i.setScaleType(ImageView.ScaleType.FIT_XY);
  10.             /* Set the Width/Height of the ImageView. */
  11.             i.setLayoutParams(new Gallery.LayoutParams(150, 150));
  12.             return i;
  13.         }
  14.  
  15.         /** Returns the size (0.0f to 1.0f) of the views
  16.          * depending on the 'offset' to the center. */
  17.         public float getScale(boolean focused, int offset) {
  18.                 /* Formula: 1 / (2 ^ offset) */
  19.             return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
  20.         }
  21.     }
  22. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Thats it ;)


Regards,
plusminus


Hi plusminus!
I wish to current item (image) in galllery item automatically. I have used setSelection attribute (int) + 1 thread to do this. It runs but is not running smoothly as each item by hand scroll.
Help me, have any ideas or attributes for each item in the Scroll auto gallery, such as using fingers to scroll is not?
Thanks very much and would respond :D
mithat
Junior Developer
Junior Developer
 
Posts: 14
Joined: Thu Jul 29, 2010 5:05 pm

Re: A android.widget.Gallery - Example

Postby cul » Fri Mar 11, 2011 8:01 am

Hey plusminus, Hi.. Thnx for your wonderful tutorials it helped me lot many times! Juz now tried your remote images galley tutorial. I want to know wheather tutorial for remote images full screen sliding animation available? I'm stuck on this since long. If not will you help regarding that? Thanx in advance you are a pro!
cul
Freshman
Freshman
 
Posts: 2
Joined: Thu Jan 13, 2011 1:55 pm

Re:

Postby at004 » Sat Mar 26, 2011 1:19 am

pskink wrote:final version: added custom animation attributes (used in xml layouts) and horizontal offset attribute. also simplified zoom out animation

have fun



hey all thx alot for this thread, its amazing, i was wondering if anyone managed to be able to load photos from a specific folder on the sdcard into that view. i have tried alot but failed to do so

thank you
at004
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Mar 26, 2011 1:13 am

Re: A android.widget.Gallery - Example

Postby sebjeri » Thu Mar 31, 2011 3:38 am

Hi, I'm a starter, looking for a .zip version of this image gallery with a small narration (audio file), which will be played when clicked on every image. Any help will be greately appreciated. All image and audio should have same file names in the drawable folder. Thanks - Jeri
sebjeri
Freshman
Freshman
 
Posts: 2
Joined: Thu Mar 31, 2011 3:20 am

Top

Re: A android.widget.Gallery - Example

Postby james.liu » Wed Apr 20, 2011 9:22 am

Hi, guys. There is open source loop gallery: Android Infinite Loop Gallery
james.liu
Freshman
Freshman
 
Posts: 5
Joined: Wed Apr 20, 2011 9:09 am

Re: A android.widget.Gallery - Example

Postby Zeba Momin » Thu May 05, 2011 8:13 am

Hi,
I've implemented Gallery in my application in the exact same manner mentioned above.
My requirement is that I need to know which image is been currently shown in the gallery .
I tried using the 'position' parameter' in getView() for this purpose, but the values returned are very uncertain and not consecutive.
Please help... How can i get the position of the currently displayed image?
Thank You.
Zeba Momin
Developer
Developer
 
Posts: 49
Joined: Thu Sep 17, 2009 6:53 am

Re: A android.widget.Gallery - Example

Postby Anbarasan » Sat May 07, 2011 2:10 pm

Hi Guys,
i want to show only one image for one fling in a gallery view..can anyone pls help me out..
Anbarasan
Once Poster
Once Poster
 
Posts: 1
Joined: Sat May 07, 2011 2:05 pm

Re: A android.widget.Gallery - Example

Postby mithat » Wed Oct 12, 2011 11:18 am

Hi everybody!
help me, gallery same this image:
Image

please way !
thanks
mithat
Junior Developer
Junior Developer
 
Posts: 14
Joined: Thu Jul 29, 2010 5:05 pm

Re: A android.widget.Gallery - Example

Postby dualcnhq » Mon Feb 06, 2012 9:30 am

hi guys,

is there any way to set the gallery in left side on load? and handle onScreenOrientation?
dualcnhq
Freshman
Freshman
 
Posts: 7
Joined: Wed Nov 16, 2011 9:55 am

Re: A android.widget.Gallery - Example

Postby Christelle » Wed Aug 08, 2012 12:50 pm

Thank you!! I really helped me :)
Christelle
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Aug 08, 2012 12:46 pm

Re: A android.widget.Gallery - Example

Postby r.j. » Fri Jan 18, 2013 7:46 am

Hi plusminus,

I wonder how this is working, yes it is working but its showing me as a default android gallery, not output you shown in your post. I am also unable to find importance of getScale() method because I cant see this from where it is called.
Basically I am looking for exact functionality which you shown in your output, thanks.
r.j.
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Jan 18, 2013 7:38 am

Top
Previous

Return to Novice Tutorials

Who is online

Users browsing this forum: Google Feedfetcher and 3 guests