Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to dynamically change inflated tab content?

I am having trouble setting the content on a tab that I've inflated from an XML file.

I add the tab to my TabHost ('tabs') dynamically by doing the following:

        TabSpec passSpec = tabs.newTabSpec("Pass Tab"); 
        passSpec.setIndicator("Passengers", getResources().getDrawable(R.drawable.tab_message));

        passSpec.setContent(new TabHost.TabContentFactory() { 
            public View createTabContent(String tag) { 
                View layout = mInflater.inflate(R.layout.tab_content_passengers, null);                 
                return(layout); 
            } 
        });
        tabs.addTab(passSpec);

This works fine...what I'm having trouble with is changing the content on that tab later on. Is there any way to accomplish this without re-inflating all of the tabs with brand new layouts?

I am trying the following and nothing happens:

    mInflater = LayoutInflater.from(this);
    View layout = mInflater.inflate(R.layout.tab_content_passengers, null);
    TextView t = (TextView) layout.findViewById(R.id.testText);
    t.setText("Hello world??");
like image 229
TomBomb Avatar asked Oct 10 '22 16:10

TomBomb


1 Answers

You can keep a reference to the layout variable (maybe in a map or something) and then programmatically modify it later on like this:

tabMap.get(tabId).findViewById(R.id.testText).setText("The text is changed now!");

As long as you do it on the UI thread the changes should be reflected immediately.

like image 108
Gyan aka Gary Buyn Avatar answered Oct 14 '22 03:10

Gyan aka Gary Buyn