Tomasz Kryński
Tomasz Kryński

Reputation: 541

How to set view as a content of TabSpec?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TabHost tabHost = new TabHost(this);
    tabHost.setId(2);
    TabWidget tabWidget = new TabWidget(this);
    tabWidget.setId(android.R.id.tabs);
    tabHost.addView(tabWidget, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

    FrameLayout tabcontentFrame = new FrameLayout(this);
    tabcontentFrame.setId(android.R.id.tabcontent);

    tabHost.addView(tabcontentFrame, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);


    tabHost.setup();

    LinearLayout lay = new LinearLayout(this);
    lay.setId(1);

...

    TabSpec spec = tabHost.newTabSpec(" ");

    spec.setContent(lay.getId()); 
    spec.setIndicator(" ");
    tabHost.addTab(spec);

    //mGame.addView(lay);
    setContentView(tabHost);
}

I want to add a view(LinearLayout) to TabSpec, but I'm getting error in line: spec.setContent(lay.getId()); Error: Could not create tab content because could not find view with id 1.

I don't want to use any xml. How to make this view visible when adding to TabSpec

///EDIT

Actually I found a problem. LinearLayout lay should be added firstly to tabcontentFrame, but it still not working and I'm getting next RunTimeException, with cause Resources$NotFoundException with message Unable to start activity ComponentInfo{com.example.konsolatrx/konsolatrx.TrxConsole}: android.content.res.Resources$NotFoundException: Resource ID #0x0

The error is in line tabHost.addTab(spec);

///SOLUTION

Ok, i found answer.

Problem is in line:

TabHost tabHost = new TabHost(this);

Should be:

TabHost tabHost = new TabHost(this, null);

Attribute set must be specified, but I don't know why. Maybe someone can explain. NKN code is good, but is not solution of this problem, just other way to make that I want.

Upvotes: 1

Views: 1336

Answers (1)

nKn
nKn

Reputation: 13761

I use something like this:

// Being th the TabHost
final TabSpec setContent = th.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
  public View createTabContent(String tag) {
    final View ll_view = LayoutInflater.from(globvars.getContext()).inflate(R.layout.tabs_content, null);
    final TextView view = (TextView) ll_view.findViewById(R.id.tabsContent);

    // do all the stuff you need to do to your view

    return ll_view;
  }
});


th.addTab(setContent);

Upvotes: 2

Related Questions