Reputation: 4287
I've seen applications that contain panel with tabs that don't lead user to another activities. I guess, this is where TabHost + Fragment elements are used.
Could you give your assumptions/caveats/links on implementing this functionality ?
Upvotes: 0
Views: 2495
Reputation: 19
You can extend TabActivity
, something like this:
public class NavigationBarActivity extends TabActivity (
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTabs();
}
private void setTabs(){
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
LayoutInflater inflater = getLayoutInflater();
// tab 1------------------------------
View tabView = inflater.inflate(R.layout.tab1, null, false);
ImageView tabImage = (ImageView)tabView.findViewById(R.id.tabImage);
TextView tabText = (TextView)tabView.findViewById(R.id.tabText);
// set icon
tabImage.setImageResource(R.drawable.tab1);
// set tab text
tabText.setText(R.string.tab1);
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Activity1.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("ta1");
spec.setIndicator(tabView);
spec.setContent(intent);
tabHost.addTab(spec);
// tab 2 -----------------------------------------
tabView = inflater.inflate(R.layout.tab2, null, false);
tabImage = (ImageView)tabView.findViewById(R.id.tabImage);
tabText = (TextView)tabView.findViewById(R.id.tabText);
// set icon
tabImage.setImageResource(R.drawable.tab2);
// set tab text
tabText.setText(R.string.tab2);
intent = new Intent().setClass(this, ChatListActivity.class);
spec = tabHost.newTabSpec("tab2");
spec.setIndicator(tabView);
spec.setContent(intent);
tabHost.addTab(spec);
tabHost.setOnTabChangedListener(this);
}
@Override
public void onTabChanged(String tabId) {
if(getTabHost().getCurrentTab() == 0) {
// WE are on tab1 ...
}
}
}
Upvotes: 0
Reputation: 12823
Use can make use of ViewPager.
First include the ViewPager in your layout (you can use the one in the compatibility package for old versions):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</RelativeLayout>
To load several Fragments into a ViewPager, you have to implement a FragmentPagerAdapter:
public class MyPagerAdapter extends FragmentPagerAdapter {
private Fragment fragment1;
private Fragment fragment2;
public ChartPagerAdapter(FragmentManager fm, Context context) {
super(fm);
fragment1 = new MyFragment();
fragment2 = new MyFragment();
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return fragment1;
case 1:
return fragment2;
}
}
@Override
public int getCount() {
return 2;
}
@Override
public String getPageTitle(int position) {
switch (position) {
case 0:
return "Title 1";
case 1:
return "Title 2";
}
}
}
Then you set this adapter to the ViewPager:
setContentView(R.layout.my_layout);
MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager(), MyActivity.this);
ViewPager pager = (ViewPager)findViewById(R.id.pager);
pager.setAdapter(adapter);
Upvotes: 2
Reputation: 4208
The advantage with using TabHost
is that you can place it anywhere. It can be implemented as a View
itself.
Here are 2 two good links I found:
Upvotes: 2