Search code examples
androidandroid-layoutandroid-gallery

How to show a gallery in a tabbed screen?


I am trying to make a app with tabs showing listactivity and on the second tab a gallery. the first one seems working but how can i make the second one?I have used tutorials from android but those are not working.Someone help?

This for tab:

    host.addTab(host.newTabSpec("tab4")
            .setIndicator("Images")
            .setContent(new Intent(this,Images.class)));

the activity:

    public class Images extends Activity
{  @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

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

 public class ImageAdapter extends BaseAdapter {
  int mGalleryItemBackground;
 private Context mContext;

 private Integer[] mImageIds = {
    R.drawable.icon,
    R.drawable.icon,
    R.drawable.icon
  };

 public ImageAdapter(Context c) {
 mContext = c;
 TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
 mGalleryItemBackground = a.getResourceId(
        R.styleable.HelloGallery_android_galleryItemBackground, 0);
 a.recycle();
   }

  public int getCount() {
  return mImageIds.length;
   }

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

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

public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);

i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);

return i;
}
}
}

please help?


Solution

  • Check this. TabContentFactory can give you the solution.