Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 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)
- 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.
- Instead of passing new TabContentFactory() to our TabSpec, we pass intent to the class specified by the currently selected tab.
- Eclipse project source code. http://www.mediafire.com/?j1bs9xyij5ymn1k
- Extract zipfile.
- Open Eclipse. Go to File, import, existing projects into workspace. Choose the root of the folder you extracted.
- 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.
- */
- package com.joshclemm.android.tabswithactivity;
- //import android.app.Activity;
- /* added */ import android.app.TabActivity;
- import android.content.Context;
- /* added */ import android.content.Intent;
- import android.os.Bundle;
- /* added */ import android.preference.PreferenceManager;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.TabHost;
- import android.widget.TextView;
- // removed import android.widget.TabHost.TabContentFactory;
- import android.widget.TabHost.TabSpec;
- import com.joshclemm.android.tabswithactivity.R;
- // removed public class CustomTabActivity extends TabActivity {
- /* added */ public class CustomTabActivity extends TabActivity {
- private TabHost mTabHost;
- private void setupTabHost() {
- mTabHost = (TabHost) findViewById(android.R.id.tabhost);
- mTabHost.setup();
- }
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // construct the tabhost
- setContentView(R.layout.main);
- setupTabHost();
- mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
- // removed setupTab(new TextView(this), "Tab 1");
- // removed setupTab(new TextView(this), "Tab 2");
- // removed setupTab(new TextView(this), "Tab 3");
- /* added */ setupTab(new TextView(this), "Search", "Search.class");
- /* added */ setupTab(new TextView(this), "Download", "Download.class");
- /* added */ setupTab(new TextView(this), "Explore", "Explore.class");
- }
- // removed private void setupTab(final View view, final String tag) {
- /* added */ private void setupTab(final View view, final String tag, final String className) {
- View tabview = createTabView(mTabHost.getContext(), tag);
- /* added */ TabHost tabHost = getTabHost();
- /* added */ TabHost.TabSpec spec;
- /* added */ Intent intent;
- /* added */ intent = new Intent().setClass(this, Search.class);
- /* added */ if (className.equals("Search.class")) {
- /* added */ intent = new Intent().setClass(this, Search.class);}
- /* added */ if (className.equals("Download.class")) {
- /* added */ intent = new Intent().setClass(this, Download.class);}
- /* added */ if (className.equals("Explore.class")) {
- /* added */ intent = new Intent().setClass(this, Explore.class);}
- // removed TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
- /* added */ TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(intent); {
- //removed public View createTabContent(String tag) {return view;}
- };
- mTabHost.addTab(setContent);
- }
- private static View createTabView(final Context context, final String text) {
- View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
- TextView tv = (TextView) view.findViewById(R.id.tabsText);
- tv.setText(text);
- return view;
- }
- }
Add Comment
Please, Sign In to add comment