GithubHelp home page GithubHelp logo

shawwinbin / tabwidgetsample Goto Github PK

View Code? Open in Web Editor NEW

This project forked from learnncode/tabwidgetsample

0.0 0.0 0.0 768 KB

This is a simple tutorial on how to use tabs with fragment in android application.

License: Apache License 2.0

tabwidgetsample's Introduction

TabWidgetSample

Demo on how to use tabs with fragment in android .

This is a simple tutorial on how to use tabs with fragment in android application.

Final output
Final Outout


In this application we are using fragment from android-support-v4.jar, you are required to add android-support-v4.jar into your app.

1] Create a class lets say "BaseActivity" as we are using fragments to show tabs. BaseActivity class will extends "android.support.v4.app.FragmentActivity" class.

Here is xml file for BaseActivity class

<android.support.v4.app.FragmentTabHost
       android:id="@android:id/tabhost"
       android:layout_width="match_parent"
       android:layout_height="match_parent" >
       <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:orientation="vertical" >
           <TabWidget
               android:id="@android:id/tabs"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:orientation="horizontal" />
           <FrameLayout
               android:id="@android:id/tabcontent"
               android:layout_width="match_parent"
               android:layout_height="match_parent" />
       </LinearLayout>
   </android.support.v4.app.FragmentTabHost>

2] BaseActivity class

In onCreate() method first setup tabhost instance and then add tabs

mTabHost  = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").
setIndicator(getResources().getString(R.string.tab_one)),
TabOneFrgment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").
setIndicator(getResources().getString(R.string.tab_two)),
TabTwoFrgment.class, null);

Here we are adding tabs in tabhost using addTab method

addTab method takes 3 parameter:

a] TabSpec instance : you can define tab indicator, can assign a tag for this tab etc.

b] activity which you want to show when user clicks on this tab.

c] bundle instance

setIndicator(): here you can set text and or icon which will displayed on tab.

You can also add icon with tabs

              Ex : mTabHost.addTab(mTabHost.newTabSpec("tab2").
                     setIndicator(getResources().getString(R.string.tab_two),                    
                     getResources().getDrawable(R.drawable.ic_launcher)),
                    TabTwoFrgment.class, null);

Tag a tab

By default tabs are shown in the same order as they were added in tabhost.

You can set a tab as current tab by using setCurrentTabByTag method.

Ex : mTabHost.setCurrentTabByTag("tag2");

You can also show a tab by using tags :

Ex : TabOneFrgment tabOneFrgment = (TabOneFrgment)  fragmentManager.findFragmentByTag("tab1");
fragmentTransaction.show(tabOneFrgment);
fragmentTransaction.commit();

How to navigate tabs???

Tab navigation has 2 aspects:

a] create a new fragment (add a fragment)

b] show already added fragment.

It is easy to create a new fragment on tab click but the trickiest part is to retain fragment state while changing tabs.

So in the sample app we are retaining fragment state whenever user changes a tab.

For this you need to add setOnTabChangedListener to the tab host.

Ex : mTabHost.setOnTabChangedListener(new OnTabChangeListener() {.....});

Inside setOnTabChangedListener we need check whether the intended fragment is already created (please refer fragment life cycle).

If fragment is already created we just need to show that fragment else we will have to create a new fragment and hide current fragment.

mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
TabOneFrgment tabOneFrgment = (TabOneFrgment) fragmentManager.findFragmentByTag("tab1");
TabTwoFrgment tabTwoFrgment = (TabTwoFrgment) fragmentManager.findFragmentByTag("tab2");
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if(tabId.equalsIgnoreCase("tab1")){
if(tabOneFrgment != null){
if(tabTwoFrgment != null){
fragmentTransaction.hide(tabTwoFrgment);
}
fragmentTransaction.show(tabOneFrgment);
}
}else{
if(tabTwoFrgment != null){
if(tabOneFrgment != null){
fragmentTransaction.hide(tabOneFrgment);
       }
fragmentTransaction.show(tabTwoFrgment);
}
}
fragmentTransaction.commit();
}
});

Happy Coding Happy Learning :)

tabwidgetsample's People

Contributors

learnncode avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.