Home > Uncategorized > How to implement Tabbar in Android

How to implement Tabbar in Android

Here you will learn step by step procedure to implement tabbar in Android through xml and java code :-

1. main.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>
<Button
android:id=”@+id/click”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Click”
/>

<ImageView
android:id=”@+id/image”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:src=”@drawable/icon”
/>
</LinearLayout>

2. tab_main.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<TabHost xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:id=”@android:id/tabhost”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
<LinearLayout
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:padding=”5dp”>
<TabWidget
android:id=”@android:id/tabs”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content” />
<FrameLayout
android:id=”@android:id/tabcontent”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:padding=”5dp” />
</LinearLayout>
</TabHost>

3. MainActivity.java
package com.sstech.camera;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TabHost;

public class MainActivity extends TabActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_main);

Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab

// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, DummyActivity.class);

// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec(“timesheet”).setIndicator(“Dashboard”,
res.getDrawable(R.drawable.icon))
.setContent(intent);
tabHost.addTab(spec);

// Do the same for the other tabs
intent = new Intent().setClass(this, DummyActivity.class);
spec = tabHost.newTabSpec(“summary”).setIndicator(“Candidate Details”,
res.getDrawable(R.drawable.icon))
.setContent(intent);
tabHost.addTab(spec);

intent = new Intent().setClass(this, DummyActivity.class);
spec = tabHost.newTabSpec(“logs”).setIndicator(“DeleteCandidate”,
res.getDrawable(R.drawable.icon))
.setContent(intent);
tabHost.addTab(spec);

spec = tabHost.newTabSpec(“timesheet”).setIndicator(“Set Question”,
res.getDrawable(R.drawable.icon))
.setContent(intent);
tabHost.addTab(spec);

// Do the same for the other tabs
intent = new Intent().setClass(this, DummyActivity.class);
spec = tabHost.newTabSpec(“summary”).setIndicator(“View Question”,
res.getDrawable(R.drawable.icon))
.setContent(intent);
tabHost.addTab(spec);

intent = new Intent().setClass(this, DummyActivity.class);
spec = tabHost.newTabSpec(“logs”).setIndicator(“Edit Profile”,
res.getDrawable(R.drawable.icon))
.setContent(intent);
tabHost.addTab(spec);

intent = new Intent().setClass(this, DummyActivity.class);
spec = tabHost.newTabSpec(“logs”).setIndicator(“Change Password”,
res.getDrawable(R.drawable.icon))
.setContent(intent);
tabHost.addTab(spec);

tabHost.setCurrentTab(0);
} catch (NotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*intent = new Intent().setClass(this, MainActivity.class);
spec = tabHost.newTabSpec(“songs”).setIndicator(“Songs”,
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);*/
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// do something on back.
Intent intent = new Intent(this,DummyActivity.class);
startActivityForResult(intent, 0);

}

return super.onKeyDown(keyCode, event);
}
}
4. DummyActivity.java
package com.sstech.camera;

import android.app.Activity;
import android.os.Bundle;

public class DummyActivity extends Activity{

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

5. AndroidManifest.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.sstech.camera”
android:versionCode=”1″
android:versionName=”1.0″>
<application android:icon=”@drawable/icon” android:label=”@string/app_name”>
<activity android:name=”.MainActivity”
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=”.DummyActivity”></activity>
</application>
<uses-sdk android:minSdkVersion=”7″ />

<uses-permission android:name=”android.permission.CAMERA” />
<uses-feature android:name=”android.hardware.camera” />
<uses-feature android:name=”android.hardware.camera.autofocus” />
</manifest>

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: