understanding Activity's setContentView

10,579

Answering my own question, I found what was wrong, when setContentView is called then the view attaches to the parent and that's why it does not appear in the tabhost. To remove the view from it's parent this code can be used:

ViewGroup vg = (ViewGroup)(myView.getParent());
vg.removeView(myView);
Share:
10,579

Related videos on Youtube

Alexander Voloshyn
Author by

Alexander Voloshyn

iOS/Android architect

Updated on April 26, 2022

Comments

  • Alexander Voloshyn
    Alexander Voloshyn about 2 years

    I need to create all the UI programmatically "on demand", this means I can't use any XML. This is the pseudo code of what I do:

    View v = new MyView();
    activity.setContentView(v);
    
    tabHost = new TabHost();
    ....
    tabHost.setup();
    TabSpec tabSpec = _tabHost.newTabSpec(page);
            tabSpec.setIndicator(title);
            tabSpec.setContent((TabContentFactory) this);
    
    activity.setContentView(tabHost);
    

    so when TabContentFactory is called I return the view which is the content view of the current activity. Basically what I do is taking current view and wrapping it in the tabhost. It half works, when I do this I'm able to see the tabbar, but only black view below it, if I click on other tab and then click back then I can see the view, everything works as intended.
    Now why I think it's related to setContentView, because when I do this:

       View v = new MyView();
    //    activity.setContentView(v); // we don't use it as current content view
    
    
    tabHost = new TabHost();
    ....
    tabHost.setup();
    TabSpec tabSpec = _tabHost.newTabSpec(page);
            tabSpec.setIndicator(title);
            tabSpec.setContent((TabContentFactory) this);
    

    Then everything is working perfectly. Any help appreciated, thanks!