0

I've a fully working tabhost/tabspec app (first working self-made app ever :D) see code beneath. Now, the following: when tab NORM is pressed I want to run a few lines of code. Is there something like an onclick for the tabhost/tabspec. Any help appreciated

public class AndroidTabLayoutActivity extends TabActivity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TabHost tabHost = getTabHost();

    // Tab for Dag
    TabSpec dagspec = tabHost.newTabSpec("Dag");
    dagspec.setIndicator("DagRooster", getResources().getDrawable(R.drawable.icon_dag_tab));
    Intent dagIntent = new Intent(this, DagActivity.class);
    dagspec.setContent(dagIntent);


    // Tab for Norm
    TabSpec normspec = tabHost.newTabSpec("Norm");
    // setting Title and Icon for the Tab
    normspec.setIndicator("Normaal", getResources().getDrawable(R.drawable.icon_norm_tab));
    Intent normIntent = new Intent(this, NormActivity.class);
    normspec.setContent(normIntent);


    // Tab for Instel
    TabSpec instelspec = tabHost.newTabSpec("Instel");
    instelspec.setIndicator("Info", getResources().getDrawable(R.drawable.icon_setting_tab));
    Intent instelIntent = new Intent(this, InstelActivity.class);
     instelspec.setContent(instelIntent);


    // Adding all TabSpec to TabHost
    tabHost.addTab(dagspec); // Adding photos tab
    tabHost.addTab(normspec); // Adding songs tab
    tabHost.addTab(instelspec); // Adding videos tab
    tabHost.setCurrentTab(2);

        }

}

Qwyrp
  • 57
  • 1
  • 8

2 Answers2

1

You just need to add TabHost to OnTabChangeListener() like this:

// Exit Application when press Exit tab
    tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {     
        @Override
        public void onTabChanged(String arg0) {
            if (getTabHost().getCurrentTabTag().equals("Exit")){
                finish();                   
            }
        }
    });
Peter O.
  • 32,158
  • 14
  • 82
  • 96
B.HoucinE
  • 31
  • 4
0

I guess you can use

tabHost.setOnTabChangedListener(new OnTabChangeListener() {
   @Override
  public void onTabChanged(String arg0) {
   Log.i("param1", "param2" + tabHost.getCurrentTab());
  }     
       });  

or

You may use the approach discussed in this SO discussion.

Community
  • 1
  • 1
kosa
  • 65,990
  • 13
  • 130
  • 167
  • When I add your code I get the following error: The method setOnTabChangedListener(TabHost.OnTabChangeListener) in the type TabHost is not applicable for the arguments (new OnTabChangeListener(){}) – Qwyrp Feb 10 '12 at 15:26