0

I write a method to generate gallery at run time.

private Gallery createGallery(ImageAdapter imageAdapter) {

    Gallery sampleGallery = new Gallery(getApplicationContext());
    sampleGallery.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    sampleGallery.setGravity(Gravity.FILL_VERTICAL);
    sampleGallery.setSpacing(5);
    sampleGallery.setAdapter(imageAdapter);
    sampleGallery.setSelection(1);
    return sampleGallery;

}

Then I create a galley and try to set it to a Tab Layout.

final TabHost mTabHost = (TabHost) findViewById(R.id.tabHost);
    mTabHost.setup();
    Gallery tabGallery = createGallery(brkingNewAdapter); // my image adapter
    tabGallery.setId(R.id.gallery_1);
    mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1")
            .setContent(R.id.gallery_1));

R.id.gallery_1 is defined as mentions in here.

I get a exception as follows.

Could not create tab content because could not find view with id 2130968576

Any help on this?

Thank you in advance.

Community
  • 1
  • 1
saji159
  • 328
  • 7
  • 14

1 Answers1

1

You should take a look at TabContentFactory.

The solution for you would be to implement TabContentFactory and return the Gallery instance in the createTabContent method and use setContent(TabHost.TabContentFactory contentFactory) method in addTab.

class GalleryContentFactory implements TabContentFactory{
    private ImageAdapter imageAdapter;
    public GalleryContentFactory(ImageAdapter imageAdapter){
        this.imageAdapter = imageAdapter;
    }
    public View createTabContent(String tag){
        Gallery sampleGallery = new Gallery(getApplicationContext());
        sampleGallery.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
        sampleGallery.setGravity(Gravity.FILL_VERTICAL);
        sampleGallery.setSpacing(5);
        sampleGallery.setAdapter(imageAdapter);
        sampleGallery.setSelection(1);
        return sampleGallery;
    }
}

and for adding to the tab:

GalleryContentFactory galleryFactory = new GalleryContentFactory(brkingNewAdapter);
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2")
            .setContent(galleryFactory));
saji159
  • 328
  • 7
  • 14
Rajesh
  • 15,724
  • 7
  • 46
  • 95