Gridview of pictures?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Gridview of pictures?

Postby barney » Sat Jun 27, 2009 4:54 am

Hi,

Sorry I'm new to android development. Have searched for the answer to this but can't figure it out. I want to make a grid view of a collection of jpeg pictures. so far i have only figured out how to use it for an array of string with the setAdapter method. All i want is to lay out a grid of images. if someone could just point in me in the right direction, the help would be greatly appreciated.

Thanks in advance,

b
barney
Junior Developer
Junior Developer
 
Posts: 14
Joined: Tue Apr 28, 2009 5:40 am

Top

Postby nbadal » Sun Jun 28, 2009 3:57 am

How about this structure? (I don't feel like writing the whole thing in xml)

Code: Select all
[LinearLayout (vertical)]
    [LinearLayout (Fill_Parent width, Horizontal, weight=1)]
        [Image (Fill_Parent width, weight 1)]
        [Image (Fill_Parent width, weight 1)]
        [Image (Fill_Parent width, weight 1)]
    [/LinearLayout]
    [LinearLayout (Fill_Parent width, Horizontal, weight=1)]
        [Image (Fill_Parent width, weight 1)]
        [Image (Fill_Parent width, weight 1)]
        [Image (Fill_Parent width, weight 1)]
    [/LinearLayout]
    [LinearLayout (Fill_Parent width, Horizontal, weight=1)]
        [Image (Fill_Parent width, weight 1)]
        [Image (Fill_Parent width, weight 1)]
        [Image (Fill_Parent width, weight 1)]
    [/LinearLayout]
[/LinearLayout]
nbadal
Developer
Developer
 
Posts: 30
Joined: Tue May 26, 2009 3:55 pm

Postby barney » Sun Jun 28, 2009 4:05 am

nbadal,

Thanks, that would work. I considered it, but figured the api had some other way with gridview. also, if more pictures were added to the list, how would this layout change? if anyone knows something like that, let me know. by the way nbadal, are you a mavs fan?
barney
Junior Developer
Junior Developer
 
Posts: 14
Joined: Tue Apr 28, 2009 5:40 am

Postby nbadal » Sun Jun 28, 2009 4:31 am

To answer your on-topic (first) question, if you wanted to add more to it, rows would be extra LinearLayouts, and columns would be another ImageView in each row.

To answer the second question: No, sorry. My username is actually not an abbreviation for NBA Dallas, but for my name, N. Badal (That is a really strange coincidence. Now when ever I see my username, I'm going to think NBA DAL lol)
nbadal
Developer
Developer
 
Posts: 30
Joined: Tue May 26, 2009 3:55 pm

Postby barney » Sun Jun 28, 2009 4:43 am

nbadal, thanks for the help. one question is how can i enable the user to add pictures to the layout after runtime? or will the layout have to be set before i compile?
barney
Junior Developer
Junior Developer
 
Posts: 14
Joined: Tue Apr 28, 2009 5:40 am

Postby nbadal » Sun Jun 28, 2009 7:34 pm

Well, if you wanted to have a set number of columns, it would be pretty easy. You would need to keep either a running total of all the images, or have an int variable that would count how many pictures are in the bottom most row.

Let's say you want four pictures in each row, you could check to see if it is divisible by four before adding a picture to an existing row, and if it was divisible by four, you would create a new LinearLayout with a new ImageView inside.

Also, if you wanted a dynamic grid of pictures, you would need to decide on a fixed height for each picture, otherwise the pictures would keep getting smaller and smaller each time you added a new row. So for example you might change the structure to:

Code: Select all
[LinearLayout (vertical)]
    [LinearLayout (Fill_Parent width, Horizontal, height=100px)]
        [Image (width=100px, height=Fill_Parent)]
        [Image (width=100px, height=Fill_Parent)]
        [Image (width=100px, height=Fill_Parent)]
    [/LinearLayout]
    [LinearLayout (Fill_Parent width, Horizontal, height=100px)]
        [Image (width=100px, height=Fill_Parent)]
        [Image (width=100px, height=Fill_Parent)]
        [Image (width=100px, height=Fill_Parent)]
    [/LinearLayout]
    [LinearLayout (Fill_Parent width, Horizontal, height=100px)]
        [Image (width=100px, height=Fill_Parent)]
        [Image (width=100px, height=Fill_Parent)]
        [Image (width=100px, height=Fill_Parent)]
    [/LinearLayout]
[/LinearLayout]
nbadal
Developer
Developer
 
Posts: 30
Joined: Tue May 26, 2009 3:55 pm

Top

Postby barney » Sun Jun 28, 2009 11:54 pm

nbadal, thanks again. your advice helped me out. what got me confused was that i didn't realize LinearLayout was a java class. i thought the layout had to be statically defined in the xml file. now i can mess around with it and figure it out
barney
Junior Developer
Junior Developer
 
Posts: 14
Joined: Tue Apr 28, 2009 5:40 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests