djdumbass

Android custom tabs using activities

Mar 30th, 2012
1,755
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.80 KB | None | 0 0
  1. /*
  2. I combined this custom tab layout tutorial with one that uses activities in the tabs instead of views. (http://mindstick.com/Articles/7e659092-3046-4461-97ba-b2b28616241e/?tab%20layout%20android%20application)
  3. To use activities, add another passable string variable to the setupTab function. this will be the class name of the activity you want to launch in that particular tab.
  4. Instead of passing new TabContentFactory() to our TabSpec, we pass intent to the class specified by the currently selected tab.
  5. Eclipse project source code. http://www.mediafire.com/?j1bs9xyij5ymn1k
  6. Extract zipfile.
  7. Open Eclipse. Go to File, import, existing projects into workspace. Choose the root of the folder you extracted.
  8.  
  9.  
  10. Random Note* If you refactor->rename anything, its a good idea to verify Eclipse renamed all related references, especially in the manifest. Then do Project - clean.
  11.  
  12.  
  13. */
  14. package com.joshclemm.android.tabswithactivity;
  15.  
  16.             //import android.app.Activity;
  17. /* added */ import android.app.TabActivity;
  18.             import android.content.Context;
  19. /* added */ import android.content.Intent;
  20.             import android.os.Bundle;
  21. /* added */ import android.preference.PreferenceManager;
  22.             import android.view.LayoutInflater;
  23.             import android.view.View;
  24.             import android.widget.TabHost;
  25.             import android.widget.TextView;
  26. // removed  import android.widget.TabHost.TabContentFactory;
  27.             import android.widget.TabHost.TabSpec;
  28.             import com.joshclemm.android.tabswithactivity.R;
  29.  
  30. // removed  public class CustomTabActivity extends TabActivity {
  31. /* added */ public class CustomTabActivity extends TabActivity {
  32.                 private TabHost mTabHost;
  33.  
  34.     private void setupTabHost() {
  35.         mTabHost = (TabHost) findViewById(android.R.id.tabhost);
  36.         mTabHost.setup();
  37.     }
  38.  
  39.     /** Called when the activity is first created. */
  40.     @Override
  41.     public void onCreate(Bundle savedInstanceState) {
  42.         super.onCreate(savedInstanceState);
  43.         // construct the tabhost
  44.         setContentView(R.layout.main);
  45.        
  46.         setupTabHost();
  47.         mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
  48.  
  49. // removed  setupTab(new TextView(this), "Tab 1");
  50. // removed      setupTab(new TextView(this), "Tab 2");
  51. // removed      setupTab(new TextView(this), "Tab 3");
  52. /* added */     setupTab(new TextView(this), "Search", "Search.class");
  53. /* added */     setupTab(new TextView(this), "Download", "Download.class");
  54. /* added */     setupTab(new TextView(this), "Explore", "Explore.class");
  55.  
  56.        
  57.        
  58.     }
  59.  
  60. // removed      private void setupTab(final View view, final String tag) {
  61. /* added */     private void setupTab(final View view, final String tag, final String className) {
  62.                         View tabview = createTabView(mTabHost.getContext(), tag);
  63. /* added */             TabHost tabHost = getTabHost();    
  64. /* added */             TabHost.TabSpec spec;  
  65. /* added */             Intent intent;
  66. /* added */             intent = new Intent().setClass(this, Search.class);
  67. /* added */             if (className.equals("Search.class")) {
  68. /* added */             intent = new Intent().setClass(this, Search.class);}
  69. /* added */             if (className.equals("Download.class")) {
  70. /* added */                 intent = new Intent().setClass(this, Download.class);}
  71. /* added */             if (className.equals("Explore.class")) {
  72. /* added */                 intent = new Intent().setClass(this, Explore.class);}
  73.  
  74. // removed  TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
  75. /* added */ TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(intent); {
  76. //removed       public View createTabContent(String tag) {return view;}
  77.                 };
  78.             mTabHost.addTab(setContent);
  79.     }
  80.  
  81.     private static View createTabView(final Context context, final String text) {
  82.         View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
  83.         TextView tv = (TextView) view.findViewById(R.id.tabsText);
  84.         tv.setText(text);
  85.         return view;
  86.     }
  87.  
  88. }
Add Comment
Please, Sign In to add comment