Quantcast
Channel: Developer Feed - Snippet
Viewing all articles
Browse latest Browse all 178

How to create a Tabbed Screen in Android?

$
0
0
  1. publicclass BaniTabbedScreen extends TabActivity implements OnTabChangeListener {
  2.  
  3.  private TabHost tabHost;
  4.  
  5.  @Override
  6.   publicvoid onCreate(Bundle savedInstanceState){
  7.         super.onCreate(savedInstanceState);
  8.         setContentView(R.layout.tabbed_screen);
  9.  
  10.         /* TabHost will have Tabs */
  11.         tabHost =(TabHost) findViewById(android.R.id.tabhost);
  12.         tabHost.setOnTabChangedListener(this);
  13.  
  14.         /* TabSpec used to create a new tab.
  15.          * By using TabSpec only we can able to setContent to the  .
  16.         * By using TabSpec setIndicator() we can set name to tab. */
  17.  
  18.         /* tid1 is firstTabSpec Id. Its used to access outside. */
  19.         TabSpec audioTabSpec = tabHost.newTabSpec("audio");
  20.         TabSpec videoTabSpec = tabHost.newTabSpec("video");
  21.  
  22. /* TabSpec setIndicator() is used to set name for the tab. */
  23. * TabSpec setContent() is used to set content for a particular tab. */
  24.         audioTabSpec.setIndicator("Audio",  getResources().getDrawable(R.drawable.audio));
  25.                 videoTabSpec.setIndicator("Video", getResources().getDrawable(R.drawable.video));
  26.  
  27.         TextView audioContent =new TextView(this);
  28.         audioContent.setText("Listen to Gurbani Audio");
  29.          audioTabSpec.setContent(audioContent.getId());
  30.                  
  31.         TextView videoContent =new TextView(this);
  32.         videoContent.setText("Watch the Gurbani Video");
  33.         videoTabSpec.setContent(videoContent.getId());
  34.  
  35.         /* Add tabSpec to the TabHost to display. */
  36.         tabHost.addTab(audioTabSpec);
  37.         tabHost.addTab(videoTabSpec);
  38.  
  39.         }
  40. }

Viewing all articles
Browse latest Browse all 178

Trending Articles