A android.widget.Gallery - Example

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

Postby plusminus » Tue Jan 22, 2008 8:21 pm

Hello soniya,

you could use this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. int resID = ((Gallery) findViewById(R.id.gallery)).getSelectedItemId();
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

this will work if you alter the getItemId(..)-method in the ImageAdapter we created (make it return the ResourceID):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public long getItemId(int position) { return position; }
  2. // Change to
  3. public long getItemId(int position) { return myImageIds[position]; }
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


then use the code in the :src: [TinyTut] - Get ResourceName by its ID-Tutorial to determine the name 'behind' the resID.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby inter » Wed Feb 27, 2008 6:37 pm

Hi

I have some problem with capturing click event on Gallery .I see the sample using onItemClickListener at http://code.google.com/android/samples/ ... lery1.html
but it not work .
Someone can help me to solve it.Thanks
inter
Junior Developer
Junior Developer
 
Posts: 23
Joined: Thu Feb 21, 2008 10:13 am

Postby inter » Wed Feb 27, 2008 7:07 pm

Hii

As the posted above , i can't show alert or toast by using OnItemClickListener.help me
inter
Junior Developer
Junior Developer
 
Posts: 23
Joined: Thu Feb 21, 2008 10:13 am

Postby plusminus » Thu Feb 28, 2008 2:12 am

Hello inter,

you also have to say what does not work.

a. Nothing happens on Click
b. Exception is thrown
c. Emulator closes
d. spontaneous anal leakage :roll: :lol:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby inter » Thu Feb 28, 2008 11:14 am

Thank for reply quickly .

My program worked but I want when i click one image ,i want it show a message or notification on screen. I used OnItemClickListener to capture the event and show the message box in my code . But when it run .It's not show message box .
Why ??
Can you help me ??
Thanks
inter
Junior Developer
Junior Developer
 
Posts: 23
Joined: Thu Feb 21, 2008 10:13 am

Postby plusminus » Thu Feb 28, 2008 1:04 pm

Hello inter,

two ways work fine for me:
a.) show Toast, when user clicked the content (Applied to each ImageView itself!):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.             i.setOnClickListener(new OnClickListener(){
  2.  
  3.  
  4.  
  5.                         @Override
  6.  
  7.                         public void onClick(View arg0) {
  8.  
  9.                                 Toast.makeText(GalleryExample.this, "HALLO WELT", Toast.LENGTH_SHORT).show();
  10.  
  11.                         }
  12.  
  13.                
  14.  
  15.             });
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


b.)
Applied as OnItemClickListener to the Gallery:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         ((Gallery) findViewById(R.id.gallery)).setOnItemClickListener(new OnItemClickListener(){
  2.  
  3.                         @Override
  4.  
  5.                         public void onItemClick(AdapterView arg0, View arg1, int arg2,
  6.  
  7.                                         long arg3) {
  8.  
  9.                                 Toast.makeText(GalleryExample.this, "HELLO WORLD", Toast.LENGTH_SHORT).show();
  10.  
  11.                         }
  12.  
  13.         });
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby saty_32016 » Wed Mar 19, 2008 5:33 am

tum0rc0re wrote:Sample from SDK ;)Btw, cool widget, very useful :)



Hi ,
how can we take image from sdcard and put that into in a array?
saty_32016
Developer
Developer
 
Posts: 27
Joined: Mon Mar 10, 2008 4:47 am

Postby Stephen.Ada » Sun Apr 13, 2008 6:49 am

Hello +- , could u explain the usage and funtionality of the function getScale in ImageAdapter, i am a little confused about it...thx a lot!!
Stephen.Ada
Junior Developer
Junior Developer
 
Posts: 10
Joined: Mon Mar 10, 2008 3:18 pm

Postby brendanlim » Fri Aug 29, 2008 7:33 am

plusminus,

It seems like in the latest SDK that the images don't actually scale. I've tried using this exact tutorial but no matter what, all of the images are 150x150.

Do you know why this is or how this can be fixed?

Thanks so much!
brendanlim
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Aug 26, 2008 7:54 pm

Postby Artur79 » Fri Oct 03, 2008 3:18 pm

plusminus wrote:a.) show Toast, when user clicked the content (Applied to each ImageView itself!):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.             i.setOnClickListener(new OnClickListener(){
  2.  
  3.                         @Override
  4.                         public void onClick(View arg0) {
  5.                                 Toast.makeText(GalleryExample.this, "HALLO WELT", Toast.LENGTH_SHORT).show();
  6.                         }
  7.                
  8.             });
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


b.)


for some reason Eclipse cannot add for me a correct import when I'am using 'Source/Organize Imports' option, so need to add
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.view.View.OnClickListener;
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
Artur79
Developer
Developer
 
Posts: 26
Joined: Fri Sep 26, 2008 12:53 pm

Postby Artur79 » Wed Oct 08, 2008 4:57 pm

in this example method getScale() is never used. what for is it. or where I need to add a call ?
Artur79
Developer
Developer
 
Posts: 26
Joined: Fri Sep 26, 2008 12:53 pm

Postby plusminus » Wed Oct 08, 2008 6:07 pm

Artur79 wrote:in this example method getScale() is never used. what for is it. or where I need to add a call ?

It gets called by the System and returns howbig the images should be scale depending on the [font=Courier New]offset[/font] to the middle on. (The formula calculates smaller values, the bigger [font=Courier New]offset[/font] is)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby Artur79 » Thu Oct 09, 2008 10:24 am

thanks, I couldn't find where it is defined now, cuz it was in GalleryAdapter which is removed now
http://code.google.com/android/migratin ... ovals.html
Artur79
Developer
Developer
 
Posts: 26
Joined: Fri Sep 26, 2008 12:53 pm

Issue

Postby JavaAndroid » Fri Oct 17, 2008 2:15 am

Hi All,
I tried the above application. But while running it, i m getting NullPointerException at this line

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

moreover the error message is not very clear to debug. Can anyone identify where i m going wrong?

Thanks in Advance

Thanks
JavaAndroid
JavaAndroid
Freshman
Freshman
 
Posts: 7
Joined: Wed Oct 15, 2008 8:02 am

Re: Issue

Postby bino » Fri Oct 17, 2008 4:11 pm

JavaAndroid wrote:Hi All,
I tried the above application. But while running it, i m getting NullPointerException at this line

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

moreover the error message is not very clear to debug. Can anyone identify where i m going wrong?

Thanks in Advance

Thanks
JavaAndroid


It will be better if you show us your source code.
Maybe, you put the line: ((Gallery) findViewById(R.id.gallery)).setAdapter(new ImageAdapter(this));
before call: setContentView(R.layout.main);
if so, you should call setContentView(...) first, then you can call findViewById(...)

If it's not help, please show us your source code :wink:
User avatar
bino
Freshman
Freshman
 
Posts: 9
Joined: Mon Oct 13, 2008 7:15 am

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests