1

This is my code for tabs. Kindly tell me how to put listener on tab1 either through switch statement or by simple onclick listener. And this toast message is also not working

public class TabsActivity extends Activity implements OnClickListener  {

    TabHost th;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tabs);

    th = (TabHost)findViewById(R.id.tabhost);

    th.setup();
    TabSpec specs = th.newTabSpec("tag1");
    specs.setContent(R.id.tab1);
    specs.setIndicator("Home");
    th.addTab(specs);


    specs = th.newTabSpec("tag2");
    specs.setContent(R.id.tab2);
    specs.setIndicator("Goup");
    th.addTab(specs);

    specs = th.newTabSpec("tag3");
    specs.setContent(R.id.tab3);
    specs.setIndicator("Delete");
    th.addTab(specs);

}



@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch(arg0.getId()){
    case R.id.tab1:
        Toast.makeText(TabsActivity.this, "Action Item 0 selected!", Toast.LENGTH_LONG).show();
    break;
case R.id.tab2:

    TextView text= new TextView(TabsActivity.this);
    text.setText("u have created a new tab");

    break;

case R.id.tab3:
    break;

}
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_tabs, menu);
return true;
}
}
Trung Nguyen
  • 7,442
  • 2
  • 45
  • 87

1 Answers1

3

If you want to listener action click on a tab, you should use this:

th.setOnTabChangedListener(new OnTabChangeListener() {
    @Override
    public void onTabChanged(String tabId) {
        Toast.makeText(getApplicationContext(), "Click on tab: "+ tabId, Toast.LENGTH_SHORT).show();
    }
});
Renjith
  • 5,783
  • 9
  • 31
  • 42
VAdaihiep
  • 521
  • 9
  • 19