SIGN UP MEMBER LOGIN:    
ARTICLE

Working with Tab Layout in Mono For Android

Posted by Manish Tewatia Articles | Android Programming October 04, 2011
Tab Layout is used to wrap multiple views in a single window and navigate thought them with a Tab Container. Let's learn in this article how to create and work with the Tab Layout in Mono for Android.
Reader Level:

When you want to wrap multiple views in a single window and navigate thought them with a Tab Container, you have to use Tab Layouts. The container object for TabLayout is Android.Widget.TabHost. When the user selects a tab, this object sends a message to the parent container, TabHost, to tell it to switch the displayed page.

TabHost is used to navigate through multiple views within the same activity.

tabs in android

syntex:

    [Android.Runtime.Register("android/widget/TabWidget")]
    public class TabWidget : LinearLayout, Android.Views.View.IOnFocusChangeListener

The Activity of Tab Layout goes like:

Activity
-TabHost
  -TabWidget
    -FrameLayout
      -TabsContent

  • First we create TabHost
  • Than create TabWidget
  • Than Create FrameLayout
  • Last creates the Content for the Tabs

Let's look at an example in which you'll create a tabbed UI that uses a separate a Activity for each tab:

Step 1 : Start a new Mono for Android Application in Visual Studio 2010 named AndroidTabLayout.

Step 2 : Add the images to your project Resources/Drawable/ like I add android.png and, and set the Build Action of image to AndroidResource in the property window.

image property in android

Step 3 : Open the Resources/Layout/Main.axml file and edit the code like below code:

Main.axml file:
 
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabh"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
  <LinearLayout
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
    <TabWidget
        android:id="@android:id/alltabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <FrameLayout
        android:id="@android:id/tabcontents"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">    
      <ImageView
        android:id="@+id/imageview1"
        android:src="@drawable/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
      <TextView
          android:id="@+id/textview1"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:textSize="15px"
          android:text="ANDROID" />
      <TextView
          android:id="@+id/textview2"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:textSize="15px"
          android:text="Android is a software stack for mobile devices that includes an operating system,
                             middleware and key applications. The Android SDK provides the tools and APIs
                             necessary to begin developing applications on the Android platform using the Java
                             programming language.
"
/>     
    </FrameLayout>
  </
LinearLayout>
</
TabHost>

In the above code we first create a TabHost than, as it required, the TabWidget and a FrameLayout both live somewhere within it. To position the TabWidget and FrameLayout vertically, a LinearLayout is used, and inside the FrameLayout we create one ImageView and two TextViews to customize the tabs.

Step 4 : Now open the Activity1.cs and use the following code:

Activity1.cs file

using
System;
 
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
 
namespace AndroidTabLayout
{
    [Activity(Label = "AndroidTabLayout", MainLauncher = true)]
    public class Activity1 :
TabActivity
    {
 
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
 
           
// Set our view from the "main" layout resource 
            SetContentView(Resource.Layout.Main);
 
            TabHost.TabSpec con;
 
            con = TabHost.NewTabSpec("tab_test1").SetIndicator("IMAGE").SetContent(Resource.Id.imageview1);
            TabHost.AddTab(con);
 
 
            con = TabHost.NewTabSpec("tab_test2").SetIndicator("NAME").SetContent(Resource.Id.textview1);
            TabHost.AddTab(con);
 
            con = TabHost.NewTabSpec("tab_test3").SetIndicator("DESCRIPTION").SetContent(Resource.Id.textview2);
            TabHost.AddTab(con);
 
            TabHost.CurrentTab = 0; 
        }      
    }
}

In the above code you see the first we change the base class from Activity to TabActivity, By default, the base class will be set to an Activity. After that we use two methods, SetIndicator() to set the text for the tab button, and SetContent() to define which View we want to associate with the tab and reveal when pressed.

Step 5 : Run your application.

In this application you see how all the tabs are categorized for different types of information, when you click on a particular tab the related information of tab will be displayed in content area.

Your application look like: 

By default, the content of the first Tab will be displayed:

android tab layout

Now click on the second Tab, the name will display:

tab layout in android

Now click on the third Tab, the description will display:

android tab

Thank You............

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor