Need help - Tab control

Put your problem here if it does not fit any of the other categories.

Need help - Tab control

Postby tcsakthimai » Fri Feb 18, 2011 11:22 am

Hi,

I need to redirect from one activity to another activity while clicking tab control.
I used customized tab.

My Java code:


Code: Select all
private TabHost mTabHost;


private void setupTabHost() {
      mTabHost = (TabHost) findViewById(android.R.id.tabhost);
      mTabHost.setup();
   }
public void onCreate(Bundle icicle) {
   super.onCreate(icicle);
   requestWindowFeature(Window.FEATURE_NO_TITLE);
   setContentView(R.layout.myblog);
                setupTabHost();
   mTabHost.getTabWidget().setDividerDrawable(R.drawable.tabline);
   setupTab(new TextView(this), "myblog");
   setupTab(new TextView(this), "buddies");
   setupTab(new TextView(this), "Subscriptions");
   setupTab(new TextView(this), "Readers");
   Bottom_Tab b=new Bottom_Tab(R.layout.myblog);
   }
   private void setupTab(final View view, final String tag) {
      View tabview = createTabView(mTabHost.getContext(), tag);

      TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
         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.tab_bg, null);
      
      ImageButton tv = (ImageButton) view.findViewById(R.id.tabsText);
      
      if(text == "myblog")         
                        tv.setBackgroundResource(R.drawable.myblog_ontab);
      
      if(text == "buddies")
         
         tv.setBackgroundResource(R.drawable.budd_tab);
      
      if(text == "Subscriptions")
         tv.setBackgroundResource(R.drawable.subscri_tab);
      
      if(text == "Readers")
         tv.setBackgroundResource(R.drawable.reader_tab);
           
      return view;
   }



My Xml code:
Code: Select all
tab_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/tabsLayout" android:layout_width="fill_parent" android:layout_height="wrap_content">
<ImageButton android:id="@+id/tabsText" android:layout_height="wrap_content" android:layout_width="fill_parent"/>   
</LinearLayout>


Code: Select all
myblog.xml


<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@android:id/tabhost" android:layout_width="fill_parent"
      android:layout_height="wrap_content">
      <LinearLayout android:orientation="vertical"
         android:layout_width="fill_parent" android:layout_height="wrap_content">         
         <TabWidget android:id="@android:id/tabs"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_marginLeft="0px" android:layout_marginRight="0px " />
         
         <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent" android:layout_height="fill_parent" />
      </LinearLayout>
</TabHost>




Here I used one image button and load different images using 'if' condition.

Is there any possible way to write click events in this code?

Thanks in advance.
tcsakthimai
Developer
Developer
 
Posts: 40
Joined: Wed Jan 19, 2011 2:21 pm

Top

Re: Need help - Tab control

Postby blundell » Thu Feb 24, 2011 11:28 pm

You mean like this:

Code: Select all

public class your class implements OnTabChangedListener {

public void onCreate(Bundle icicle) {
mTabHost.setOnTabChangedListener(this);
}
}

[/code]

Code: Select all
@Override
        public void onTabChanged(String tabId) {
                // if tab four pressed send to SearchPage.class
                if (mTabHost.getCurrentTab() == 3) {
                        Intent i = new Intent(mContext, SearchPage.class);
                        startActivity(i);
                        mTabHost.setCurrentTab(0);
                }
        }
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Need help - Tab control

Postby strider2023 » Mon Feb 28, 2011 11:08 am

Or do you mean something like this...

Code: Select all
private TabHost loadingTab;
   
   /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loading_tab);
       
        loadingTab = (TabHost)findViewById(android.R.id.tabhost);
       
        TabSpec preLoadingTabSpec = loadingTab.newTabSpec("preLoadingTab");
        TabSpec loadingTabSpec = loadingTab.newTabSpec("loadingTab"); 
        TabSpec postLoadingTabSpec = loadingTab.newTabSpec("postLoadingTab");
               
        //Without Tab Icons
        preLoadingTabSpec.setIndicator("Pre-Loading").setContent(new Intent(this,PreLoadingService.class));
        loadingTabSpec.setIndicator("Loading").setContent(new Intent(this,CurrentLoadingService.class));
        postLoadingTabSpec.setIndicator("Post-Loading").setContent(new Intent(this,PostLoadingService.class));
       
        loadingTab.addTab(preLoadingTabSpec);
        loadingTab.addTab(loadingTabSpec);
        loadingTab.addTab(postLoadingTabSpec);
       
        loadingTab.getTabWidget().setCurrentTab(0);
    }
strider2023
Senior Developer
Senior Developer
 
Posts: 126
Joined: Tue Mar 02, 2010 6:34 am
Location: Chennai, India

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 21 guests