Skip to main content

Multiple Android Activities in a TabActivity

Project Name:- Tab_Sample

Package Name:- com.android.tabsample
Target SDK:- Android 2.2 (API 8)

Create below 5 classes under the "com.android.tabsample" package.

ActivityStack.java


public class ActivityStackHome extends ActivityGroup {
    private Stack<String> stack;

    @Override
    public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (stack == null)
stack = new Stack<String>();
// start default activity
push("FirstStackActivity", new Intent(this, Tab_SampleActivity.class));
    }

    @Override
    public void finishFromChild(Activity child) {
pop();
    }

    @Override
    public void onBackPressed() {
pop();
    }

    public void push(String id, Intent intent) {
  Window window = getLocalActivityManager().startActivity(id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        if (window != null) {
            stack.push(id);
setContentView(window.getDecorView());
}
    }

    public void pop() {
  if (stack.size() == 1)
            finish();
        LocalActivityManager manager = getLocalActivityManager();
  manager.destroyActivity(stack.pop(), true);
if (stack.size() > 0) {
Intent lastIntent = manager.getActivity(stack.peek()).getIntent();
            Window newWindow = manager.startActivity(stack.peek(), lastIntent);
            setContentView(newWindow.getDecorView());
        }
    }
}


TabActivity.java


public class TabActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_screen);
TabHost tabHost = getTabHost();
Intent intent = new Intent().setClass(this, ActivityStack.class);
TabHost.TabSpec spec = tabHost.newTabSpec("tabId").setIndicator("Temp", getResources().getDrawable(R.drawable.home));
spec.setContent(intent);

tabHost.addTab(spec);

Intent intent1 = new Intent().setClass(this, ActivityStack.class);
TabHost.TabSpec spec1 = tabHost.newTabSpec("tabId").setIndicator("Temp", getResources().getDrawable(R.drawable.invoice));
spec1.setContent(intent1);
tabHost.addTab(spec1);

tabHost.setCurrentTab(0);
    }
}


FirstActivity.java


public class FirstActivity extends Activity {
  @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setText("Tab Sample Activity ");
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(getParent(), SecondActivity.class);
ActivityStack activityStack = (ActivityStack) getParent();
activityStack.push("SecondActivity", intent);
}
});
 
        setContentView(textView);
  }
}


SecondActivity.java


public class SecondActivity extends Activity {
    @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setText("First Stack Activity ");
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(getParent(), ThirdActivity.class);
ActivityStack activityStack = (ActivityStack) getParent();
activityStack.push("ThirdActivity", intent);
            }
        });
 
        setContentView(textView);
    }
}


ThirdActivity.java


public class ThirdActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
    }
}


Add Below XML files into your res/layout folder.

1) tab_screen.xml


<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="3dp" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@android:id/tabs"
            android:layout_weight="1" />

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" />
    </RelativeLayout>

</TabHost>


2) main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>


AndroidManifest.xml:-


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.tabsample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".FirstActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".TabActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ActivityStack"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SecondActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ThirdActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Download Full Source Code from below link.

Android Bottom Tab Example



Don’t forget to provide feedback or follow this blog, if you find this blog is useful.

Comments

Popular posts from this blog

Android Studio New IDE for Android Application Development

Google Launches Android Studio And New Features For Developer Console, Including Beta Releases And Staged Rollout during Google’s I/O developer conference, the company announced a group of tools for app developers, including a new developer suite called Android Studio. It’s an IDE based on  IntelliJ . This was a popular announcement, as the crowd “ooh’d” and “ahh’d” as screenshots were shown on stage. This tool has more options for Android Development, making the process faster and more productive. A “live layout” was shown that renders your app as you’re editing in realtime. Additionally, you can switch over to different layouts and screen sizes, such as 3.7 inch phone and 10-inch tablet. The team noted that this might be useful for internationalization, allowing you to quickly see what things look like without having to package up your app and install it on a device. The company says that it has “big plans” for Android Studio. DOWNLOAD ANDROID STUDIO V0...

Android Alarm Manager Example

AlarmManager Many a times we want some task to be performed at some later time in future. For Example: In SMS Scheduler we want a SMS to be send at some later time, or Task Reminder in which we want to be reminded  about a task at a particular time, to implement all these things we use AlramManager class. AlarmManager class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the  Intent  that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.                   The Alarm Manager holds a CPU wake lock as long as the ...

Android How to Send Email programmatically

In this tutorial, you will see how to send an email from your own android application. To do that, Android provides the  Intent . ACTION_SEND  , which means that your Activity want to send some data(in our case:email, cc, bcc, subject, attachements …) to another Activity(it may be an Activity in your own application, or in another application). The activity which will receive this sent data is not specified, it is up to the receiver of this action to ask the user where the data should be sent. That’s why you should give the user the choice of how to send data by wrapping it into a  Chooser   (through   createChooser ( Intent , CharSequence )  ). Send Email : 1 2 3 4 5 6 7 8 Intent intentEmail = new Intent ( Intent . ACTION_SEND ) ; intentEmail . putExtra ( Intent . EXTRA_EMAIL , new String [ ] { "your.email@gmail.com" } ) ; intentEmail . putExtra ( Intent . EXTRA_SUBJECT , "your subject" ) ; intentEmail...