Wednesday, 7 November 2018

Tabbed Activity in Android

Hi All,
Use Tabbed Activity for good user experience.



package com.example.tabdemo;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {
TabHost tabhost;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              tabhost=(TabHost)findViewById(android.R.id.tabhost);
             
              TabSpec tab1=tabhost.newTabSpec("first activity");
              TabSpec tab2=tabhost.newTabSpec("second activity");
              TabSpec tab3=tabhost.newTabSpec("second activity");
             
              tab1.setIndicator("contact");
              tab1.setContent(new Intent(this,one.class));
             
              tab2.setIndicator("home");
              tab2.setContent(new Intent(this,two.class));
             
              tab3.setIndicator("list");
              tab3.setContent(new Intent(this,three.class));
             
              tabhost.addTab(tab1);
              tabhost.addTab(tab2);
              tabhost.addTab(tab3);
             
       }

       @Override
       public boolean onCreateOptionsMenu(Menu menu) {
              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              return true;
       }

       @Override
       public boolean onOptionsItemSelected(MenuItem item) {
              // Handle action bar item clicks here. The action bar will
              // automatically handle clicks on the Home/Up button, so long
              // as you specify a parent activity in AndroidManifest.xml.
              int id = item.getItemId();
              if (id == R.id.action_settings) {
                     return true;
              }
              return super.onOptionsItemSelected(item);
       }
}


------------------------------------------------------------------------------------------------------------------------------------------

Layout file


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.tabdemo.MainActivity"
    android:orientation="horizontal">

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

        <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" >
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                            </FrameLayout>
        </LinearLayout>
    </TabHost>

</LinearLayout>

No comments:

Post a Comment