views:

27

answers:

1

Hi everyone,

I'm trying to create a gallery in my Android project, but can't seem to make it work as it should.

Here is what I am trying to do:

Basically, I want a gallery that displays only one picture, but still has the same controls (sliding left and right). I'm getting the pictures asynchronously from the internet.

Where am I at that?

Well, for now I'm just trying to display the gallery... I'll see the "one picture" step after.

What is my problem?

Well... My gallery doesn't show on my layout. At all.

What seems to be the problem?

Log information give me that the height and the widht of my gallery object are... 0.

Speaking about code...

Layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:background="@drawable/fond">

  <ImageView android:background="@drawable/banniere" android:layout_height="wrap_content" android:layout_width="fill_parent"/>

  <Gallery
    android:id="@+id/gallery"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>



</LinearLayout>

Activity

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.galleryfullscreen);

    Gallery gal = (Gallery) findViewById(R.id.gallery);

    ArrayList<Photo> photos = (ArrayList<Photo>) this.getIntent().getExtras().getSerializable("Photos");
    int pos = this.getIntent().getExtras().getInt("Index");

    Log.i("Season",photos.toString());

    gal.setAdapter(new PhotoAdapter(this, photos, false));
    gal.setSelection(pos);

    Log.d("Bottom", String.valueOf(gal.getBottom()));
    Log.d("Right", String.valueOf(gal.getRight()));
    Log.d("Width", String.valueOf(gal.getWidth()));
    Log.d("Height", String.valueOf(gal.getHeight()));       
}

Extras are correctly set.

Adapter

public class PhotoAdapter extends BaseAdapter {

private ArrayList<PhotoView> views;
private ArrayList<Photo> season;

public PhotoAdapter(Context c, ArrayList<Photo> images, boolean mini) {

    season = images;
    views = new ArrayList<PhotoView>();
    for (int i = 0; i < images.size() ; i++)
        if (mini)
        views.add(new PhotoView(c,images.get(i).getUrlMini(),false));
        else{

            views.add(new PhotoView(c,images.get(i).getUrlBig(),true));
        }

}

@Override
public int getCount() {
    return views.size();
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    return views.get(position);
}

public ArrayList<Photo> getSeason() {
    return season;
}

}

This adaptor is working fine for other objects (such as gridview)

And the PhotoView...

public class PhotoView extends ImageView {

Bitmap image;

public PhotoView(Context context, String url, boolean Gallery) {

    super(context);
    if (!Gallery){
    this.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT,GridView.LayoutParams.WRAP_CONTENT));
    this.setScaleType(ImageView.ScaleType.FIT_CENTER);
    this.setImageDrawable((getResources().getDrawable(R.drawable.blk)));
    new DownloadImageTask().execute(url);
    }
    else{
        this.setLayoutParams(new Gallery.LayoutParams(100,100));
        this.setScaleType(ImageView.ScaleType.FIT_CENTER);
        this.setBackgroundDrawable(((getResources().getDrawable(R.drawable.blk))));
        this.setImageDrawable((getResources().getDrawable(R.drawable.blk)));
    }

}

public Bitmap getImage(){
    return image;
}

public void setImage(Bitmap img){
    image = img;
    this.setImageBitmap(image);
}

The same, it's working fine for a grid view. With the download task, that is not set up for the Gallery yet. I'm at least trying to display a ressource before setting this up, so I'm sure the problem doesn't come from here.

Sooo... why are my logs showing "0" for gallery width and height? LayoutParams are set for the object (I tried both XML and code), and for the views inside. The items are created, and the "getView" method is called. I tried seeting up LayoutParams here, doesn't change anything.

I also tried adding the xmlns:android="http://schemas.android.com/apk/res/android" part in the layout for the gallery, doesn't change a thing.

I really don't know what I'm missing. I tried to adapt many tutorials on gallery objects, but still... don't know! If you have any idea of what I should do/try, please...

Thanks all!

Cheers

A: 

Looking at your XML file I can see what could be problem. Your LinearLayout is set to add views in an horizontal matter (that is default) and your first view has its layout_width set to fill_parent.

Set orientation to vertical in your LinearLayout.

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" 
  android:orientation="vertical"
  android:background="@drawable/fond">
gyller
Well, that did the trick.. knew I was missing something! Thanks a lot
Ours