Here are the examples of the java api class android.widget.TabHost.TabSpec taken from open source projects.
1. TabTest#onCreate()
Project: codeexamples-android
File: TabTest.java
File: TabTest.java
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tab); TabHost host = (TabHost) findViewById(android.R.id.tabhost); TabSpec spec = host.newTabSpec("tab1"); spec.setContent(R.id.tab1); spec.setIndicator("Button1"); host.addTab(spec); spec = host.newTabSpec("tab2"); spec.setContent(R.id.tab2); spec.setIndicator("Next Button"); host.addTab(spec); spec = host.newTabSpec("tab3"); spec.setContent(R.id.tab3); spec.setIndicator("Just some text"); host.addTab(spec); }
2. TabsViewAttribute#constructTabSpec()
Project: AndroidBinding
File: TabsViewAttribute.java
File: TabsViewAttribute.java
private TabSpec constructTabSpec(Tab tab) { TabSpec spec = mTabHost.newTabSpec(tab.Tag.get()); if (tab.Icon.get() != null) { spec.setIndicator(tab.Label.get(), tab.Icon.get()); } else { spec.setIndicator(tab.Label.get()); } if (tab.Activity.get() != null) { Intent intent; try { intent = new Intent(getView().getContext(), Class.forName(tab.Activity.get())); spec.setContent(intent); } catch (ClassNotFoundException e) { e.printStackTrace(); } } else { spec.setContent(tab.ViewId.get()); } return spec; }
3. DetailActivity#onCreate()
Project: runnerup
File: DetailActivity.java
File: DetailActivity.java
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.detail); WidgetUtil.addLegacyOverflowButton(getWindow()); Intent intent = getIntent(); mID = intent.getLongExtra("ID", -1); String mode = intent.getStringExtra("mode"); mDB = DBHelper.getReadableDatabase(this); syncManager = new SyncManager(this); formatter = new Formatter(this); if (mode.contentEquals("save")) { this.mode = MODE_SAVE; } else if (mode.contentEquals("details")) { this.mode = MODE_DETAILS; } else { assert (false); } saveButton = (Button) findViewById(R.id.save_button); discardButton = (Button) findViewById(R.id.discard_button); resumeButton = (Button) findViewById(R.id.resume_button); uploadButton = (Button) findViewById(R.id.upload_button); activityTime = (TextView) findViewById(R.id.activity_time); activityDistance = (TextView) findViewById(R.id.activity_distance); activityPace = (TextView) findViewById(R.id.activity_pace); sport = (TitleSpinner) findViewById(R.id.summary_sport); notes = (EditText) findViewById(R.id.notes_text); map = (MapView) findViewById(R.id.mapview); saveButton.setOnClickListener(saveButtonClick); uploadButton.setOnClickListener(uploadButtonClick); if (this.mode == MODE_SAVE) { resumeButton.setOnClickListener(resumeButtonClick); discardButton.setOnClickListener(discardButtonClick); setEdit(true); } else if (this.mode == MODE_DETAILS) { resumeButton.setVisibility(View.GONE); discardButton.setVisibility(View.GONE); setEdit(false); } uploadButton.setVisibility(View.GONE); fillHeaderData(); requery(); loadRoute(); TabHost th = (TabHost) findViewById(R.id.tabhost); th.setup(); TabSpec tabSpec = th.newTabSpec("notes"); tabSpec.setIndicator(WidgetUtil.createHoloTabIndicator(this, getString(R.string.Notes))); tabSpec.setContent(R.id.tab_main); th.addTab(tabSpec); tabSpec = th.newTabSpec("laps"); tabSpec.setIndicator(WidgetUtil.createHoloTabIndicator(this, getString(R.string.Laps))); tabSpec.setContent(R.id.tab_lap); th.addTab(tabSpec); tabSpec = th.newTabSpec("map"); tabSpec.setIndicator(WidgetUtil.createHoloTabIndicator(this, getString(R.string.Map))); tabSpec.setContent(R.id.tab_map); th.addTab(tabSpec); tabSpec = th.newTabSpec("graph"); tabSpec.setIndicator(WidgetUtil.createHoloTabIndicator(this, getString(R.string.Graph))); tabSpec.setContent(R.id.tab_graph); th.addTab(tabSpec); tabSpec = th.newTabSpec("share"); tabSpec.setIndicator(WidgetUtil.createHoloTabIndicator(this, getString(R.string.Upload))); tabSpec.setContent(R.id.tab_upload); th.addTab(tabSpec); th.getTabWidget().setBackgroundColor(Color.DKGRAY); { ListView lv = (ListView) findViewById(R.id.laplist); LapListAdapter adapter = new LapListAdapter(); adapters.add(adapter); lv.setAdapter(adapter); } { ListView lv = (ListView) findViewById(R.id.report_list); ReportListAdapter adapter = new ReportListAdapter(); adapters.add(adapter); lv.setAdapter(adapter); } graphTab = (LinearLayout) findViewById(R.id.tab_graph); { graphView = new LineGraphView(this, getString(R.string.Pace)) { @Override protected String formatLabel(double value, boolean isValueX) { if (!isValueX) { return formatter.formatPace(Formatter.TXT_SHORT, value); } else return formatter.formatDistance(Formatter.TXT_SHORT, (long) value); } }; graphView2 = new LineGraphView(this, getString(R.string.Heart_rate)) { @Override protected String formatLabel(double value, boolean isValueX) { if (!isValueX) { return Integer.toString((int) Math.round(value)); } else { return formatter.formatDistance(Formatter.TXT_SHORT, (long) value); } } }; } hrzonesBarLayout = (LinearLayout) findViewById(R.id.hrzonesBarLayout); hrzonesBar = new HRZonesBar(this); }
4. PermissionsDialog#onCreate()
Project: astrid
File: PermissionsDialog.java
File: PermissionsDialog.java
/* * Layout the permission dialog */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.permissions_list); LayoutParams params = getWindow().getAttributes(); params.width = LayoutParams.FILL_PARENT; params.height = LayoutParams.FILL_PARENT; getWindow().setAttributes((android.view.WindowManager.LayoutParams) params); mPermissionDetails = (TextView) findViewById(R.id.permission_detail); mPermissionDetails.setMovementMethod(LinkMovementMethod.getInstance()); userPermissionsList = (ListView) findViewById(R.id.user_permissions_list); friendPermissionsList = (ListView) findViewById(R.id.friend_permissions_list); extendedPermissionsList = (ListView) findViewById(R.id.extended_permissions_list); userPermissionsAdapter = new PermissionsListAdapter(user_permissions); userPermissionsList.setAdapter(userPermissionsAdapter); friendPermissionsAdapter = new PermissionsListAdapter(friend_permissions); friendPermissionsList.setAdapter(friendPermissionsAdapter); extendedPermissionAdapter = new PermissionsListAdapter(extended_permissions); extendedPermissionsList.setAdapter(extendedPermissionAdapter); TabHost tabHost = (TabHost) findViewById(R.id.tabHost); tabHost.setup(); TabSpec spec1 = tabHost.newTabSpec("Tab 1"); spec1.setIndicator(activity.getString(R.string.user)); spec1.setContent(R.id.user_permissions_list); TabSpec spec2 = tabHost.newTabSpec("Tab 2"); spec2.setIndicator(activity.getString(R.string.friend)); spec2.setContent(R.id.friend_permissions_list); TabSpec spec3 = tabHost.newTabSpec("Tab 3"); spec3.setIndicator(activity.getString(R.string.extended)); spec3.setContent(R.id.extended_permissions_list); tabHost.addTab(spec1); tabHost.addTab(spec2); tabHost.addTab(spec3); tabHost.setCurrentTab(0); tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = TAB_HEIGHT; tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = TAB_HEIGHT; tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = TAB_HEIGHT; mGetPermissions = (Button) findViewById(R.id.get_permissions_button); mGetPermissions.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { /* * Source Tag: perms_tag Call authorize to get the new * permissions */ if (reqPermVector.isEmpty() && Utility.mFacebook.isSessionValid()) { Toast.makeText(activity.getBaseContext(), "No Permissions selected.", Toast.LENGTH_SHORT).show(); PermissionsDialog.this.dismiss(); } else { String[] permissions = reqPermVector.toArray(new String[0]); Utility.mFacebook.authorize(activity, permissions, new LoginDialogListener()); } } }); }
5. FieldsConnectionsDialog#onCreate()
Project: astrid
File: FieldsConnectionsDialog.java
File: FieldsConnectionsDialog.java
/* * Layout the dialog */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fields_connections_list); LayoutParams params = getWindow().getAttributes(); params.width = LayoutParams.FILL_PARENT; params.height = LayoutParams.FILL_PARENT; getWindow().setAttributes((android.view.WindowManager.LayoutParams) params); fieldsList = (ListView) findViewById(R.id.fields_list); connectionsList = (ListView) findViewById(R.id.connections_list); fieldsAdapter = new FieldsListAdapter(); if (this.fieldsArray == null) { fieldsList.setAdapter(new ArrayAdapter<String>(explorerActivity, android.R.layout.simple_list_item_1, new String[] { "No fields available" })); } else { fieldsList.setAdapter(fieldsAdapter); } connectionsAdapter = new ConnectionsListAdapter(); if (this.connectionsArray == null) { connectionsList.setAdapter(new ArrayAdapter<String>(explorerActivity, android.R.layout.simple_list_item_1, new String[] { "No connections available" })); } else { connectionsList.setAdapter(connectionsAdapter); } TabHost tabHost = (TabHost) findViewById(R.id.tabHost); tabHost.setup(); TabSpec spec1 = tabHost.newTabSpec("Tab 1"); spec1.setIndicator(explorerActivity.getString(R.string.fields)); spec1.setContent(R.id.fields_layout); TabSpec spec2 = tabHost.newTabSpec("Tab 2"); spec2.setIndicator(explorerActivity.getString(R.string.connections)); spec2.setContent(R.id.connections_list); tabHost.addTab(spec1); tabHost.addTab(spec2); tabHost.setCurrentTab(0); tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = TAB_HEIGHT; tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = TAB_HEIGHT; mGetFieldsButton = (Button) findViewById(R.id.get_fields_button); mGetFieldsButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { /* * Source Tag: */ FieldsConnectionsDialog.this.dismiss(); if (!fieldsVector.isEmpty()) { explorerActivity.getFields(fieldsVector); } else { Toast.makeText(explorerActivity.getBaseContext(), "No Fields selected.", Toast.LENGTH_SHORT).show(); } } }); connectionsList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) { FieldsConnectionsDialog.this.dismiss(); explorerActivity.getConnection(connectionsArray.get(position)); } }); }
6. DebugTabActivity#onCreate()
Project: BeeFramework_Android
File: DebugTabActivity.java
File: DebugTabActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.debug_home_tab); tabHost = getTabHost(); width = getWindowManager().getDefaultDisplay().getWidth() / 4; image = (ImageView) findViewById(R.id.tab_image); LayoutParams params = (LayoutParams) image.getLayoutParams(); params.width = width; image.setLayoutParams(params); TabSpec spec_tab1 = tabHost.newTabSpec("spec_tab1").setIndicator("spec_tab1").setContent(new Intent(DebugTabActivity.this, DebugMessageListActivity.class)); tabHost.addTab(spec_tab1); TabSpec spec_tab2 = tabHost.newTabSpec("spec_tab2").setIndicator("spec_tab2").setContent(new Intent(DebugTabActivity.this, ActivityLifeCycleActivity.class)); tabHost.addTab(spec_tab2); TabSpec spec_tab3 = tabHost.newTabSpec("spec_tab3").setIndicator("spec_tab3").setContent(new Intent(DebugTabActivity.this, CrashLogActivity.class)); tabHost.addTab(spec_tab3); TabSpec spec_tab4 = tabHost.newTabSpec("spec_tab4").setIndicator("spec_tab4").setContent(new Intent(DebugTabActivity.this, FloatViewSettingActivity.class)); tabHost.addTab(spec_tab4); RadioGroup group = (RadioGroup) this.findViewById(R.id.tab_group); group.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub switch(checkedId) { case R.id.tab_one: tabHost.setCurrentTabByTag("spec_tab1"); mTranslateAnimation = new TranslateAnimation(start, 0, 0, 0); mTranslateAnimation.setDuration(200); mTranslateAnimation.setFillEnabled(true); mTranslateAnimation.setFillAfter(true); image.startAnimation(mTranslateAnimation); start = 0; break; case R.id.tab_two: tabHost.setCurrentTabByTag("spec_tab2"); mTranslateAnimation = new TranslateAnimation(start, width, 0, 0); mTranslateAnimation.setDuration(200); mTranslateAnimation.setFillEnabled(true); mTranslateAnimation.setFillAfter(true); image.startAnimation(mTranslateAnimation); start = width; break; case R.id.tab_three: tabHost.setCurrentTabByTag("spec_tab3"); mTranslateAnimation = new TranslateAnimation(start, width * 2, 0, 0); mTranslateAnimation.setDuration(200); mTranslateAnimation.setFillEnabled(true); mTranslateAnimation.setFillAfter(true); image.startAnimation(mTranslateAnimation); start = width * 2; break; case R.id.tab_four: tabHost.setCurrentTabByTag("spec_tab4"); mTranslateAnimation = new TranslateAnimation(start, width * 3, 0, 0); mTranslateAnimation.setDuration(100); mTranslateAnimation.setFillEnabled(true); mTranslateAnimation.setFillAfter(true); image.startAnimation(mTranslateAnimation); start = width * 3; break; } } }); }
7. ShadowTabHost#newTabSpec()
Project: robolectric
File: ShadowTabHost.java
File: ShadowTabHost.java
@Implementation public android.widget.TabHost.TabSpec newTabSpec(java.lang.String tag) { TabSpec realTabSpec = Robolectric.newInstanceOf(TabHost.TabSpec.class); shadowOf(realTabSpec).setTag(tag); return realTabSpec; }
8. MainFrameActivity#addTab()
Project: pixate-freestyle-android
File: MainFrameActivity.java
File: MainFrameActivity.java
/** * Convenient method to add a tab to the tab widget at bottom. * * @param resId The resource id of image shown on indicator * @param content The class type of content */ private void addTab(int resId, Class<? extends Fragment> content) { ImageView indicator = new ImageView(this); indicator.setScaleType(ScaleType.CENTER_INSIDE); indicator.setBackgroundResource(R.drawable.tab_bg); indicator.setImageResource(resId); TabSpec spec = tabHost.newTabSpec(content.getSimpleName()).setIndicator(indicator); tabHost.addTab(spec, content, null); }