Tutorial :Using Views in a tabbed application



Question:

Alright, so I have an application that has a tabbed interface. What I would like to know is how I can change the currently active View in the tab, and then when I'm done with that View, go back the the original View, exactly as it was.


Solution:1

You may also want to use a ViewFlipper, defined in xml.

<ViewFlipper android:id="@+id/ScreenSwitch"      android:layout_width="fill_parent"      android:layout_height="fill_parent">      <element A>      <element B>      <element C>  </ViewFlipper>  

Usage is simple, just find the ViewFlipper object throught findViewById() and request changing:

viewFlipper.setDisplayedChild( idOfViewToSet );  

It's then completely up to you, how do you build "tabs".


Solution:2

For creating the view, what you probably want is something like this:

import android.view.View;  import android.widget.TabHost;    public class MyViewController implements TabHost.TabContentFactory  {      private View myRootView;        @Override      public View createTabContent(String tag)      {          if (myRootView == null)          {              // TODO: Create your view here by...              //       1) Constructing it yourself              //       2) Inflating a layout.xml with LayoutInflater (better)              myRootView = new View(context);          }          return myRootView;      }  }  

As for changing tabs programmatically, it is as simple as calling setCurrentTab() on your TabHost object.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »