setCurrentTab Android
i have 4 tabs on my main screen,
main ( set to current ) , Call, Email, Web
When a user clicks on any of tab call, email or web, it starts making a call, or go to compose a email, or opens up the browser respectfully.
Problem is, i want just three tabs (Call, Email, Web) and i Dont want any tab to be selected by default, means they should only become active when a user Touch them..(a call or any service cant be main at all)
All java coding, XML file, and Manifest code is given below,
XML File (tab_activity_layout)
<?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">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"></FrameLayout>
</RelativeLayout>
</LinearLayout>
</TabHost>
Java Coding (MainTabActivity)
package com.NVT.android;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
public class MainTabActivity extends TabActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_activity_layout);
Resources res = getResources(); // Resource object to get Drawables
开发者_StackOverflow社区 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, Main.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("main").setIndicator("Main",
res.getDrawable(R.drawable.ic_tab_artists_grey))
.setContent(intent);
tabHost.addTab(spec);
TabHost host=getTabHost();
host.addTab(host.newTabSpec("one")
.setIndicator("Call")
.setContent(new Intent(this, CallService.class)));
host.addTab(host.newTabSpec("two")
.setIndicator("Email")
.setContent(new Intent(this, EmailService.class)));
host.addTab(host.newTabSpec("three")
.setIndicator("Web")
.setContent(new Intent(this, WebService.class)));
}
}
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.NVT.android"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Main"
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=".MainTabActivity"
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=".Courses">
</activity>
<activity android:name=".CampusMap">
</activity>
<activity android:name=".GettingHere">
</activity>
<activity android:name=".ILoveNescot">
</activity>
<activity android:name=".FurtherEducationCourses">
</activity>
<activity android:name=".HigherEducationCourses">
</activity>
<activity android:name=".EmployersTrainingCourses">
</activity>
<activity android:name=".WebService">
</activity>
<activity android:name=".CallService">
</activity>
<activity android:name=".EmailService">
</activity>
</application>
<uses-sdk android:minSdkVersion="9" />
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
You say you want the tabs to act like buttons - so why don't you just use buttons instead?
When they are pressed, just change the contents of the area above based on the button pressed.
A TabHost requires that one of the tabs is always visible so while it may be possible to do what you want with a TabHost, and playing with the visibility, your code will be simpler and easier to maintain and debug if you just use buttons instead.
EDIT:
I've not tested this, and have written it from memory, but I think it should give a starting point.
layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<FrameLayout
android:id="@android:id/contentpane"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
</FrameLayout>
<LinearLayout
abdroid:orientation="horizontal"
android:gravity="bottom"
android:layout_width="fill_parent">
<Button android:id="@android:id/callbutton"
android:text="Call"/>
<Button android:id="@android:id/emailbutton"
android:text="Email"/>
<Button android:id="@android:id/webbutton"
android:text="Web"/>
</LinearLayout>
</LinearLayout>
In your onClick()
for the buttons, set the relevant content view for the contentpane:
FrameLayout fl = ( FrameLayout ) findViewById ( R.id.contentpane );
fl.setContentView ( R.id.callservice ); // Or create it programmatically, and use the layout here
host.setCurrentTabByTag(tag);
host.setCurrentTab(index);
use on of the above method
you said "i Dont want any tab to be selected by default", but it's anti-behaviour of a tab component. The default character of a tab layout is to be select at least one tab. Maybe you must redesign your UI, think other UI in the world as, for example, Firefox...
Greetings
Intent i = new Intent(this, SomeActivity.class);
tabHost.addTab(tabHost.newTabSpec("invisible_tab").setIndicator("").setContent(i));
// hide the tab
tabHost.getTabWidget().getChildTabViewAt(0).setVisibility(View.GONE);
精彩评论