Tuesday, November 13, 2012

Tab menu in android


main.xml
<?xml version=”1.0″ encoding=”utf-8″?> <TabHost xmlns:android=”http://schemas.android.com/apk/res/android” android:id=”@+id/my_tabhost” android:layout_width=”fill_parent” android:layout_height=”fill_parent”> <TabWidget android:id=”@android:id/tabs” android:layout_width=”fill_parent” android:layout_height=”65px”/>
<FrameLayout android:id=”@android:id/tabcontent” android:layout_width=”fill_parent” android:layout_height=”200px” android:paddingTop=”65px”> <LinearLayout android:id=”@+id/content” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”fill_parent”> </LinearLayout> </FrameLayout> </TabHost>
tabViewTest2 .java
package com.android.mytabviewtest;
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.TabHost; import android.widget.TabHost.TabSpec;
public class tabViewTest2 extends Activity {
private int ADD_NEW_TAB = Menu.FIRST;
@Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); TabHost tabs = (TabHost) this.findViewById(R.id.my_tabhost); tabs.setup(); TabSpec tspec1 = tabs.newTabSpec(”First Tab”); tspec1.setIndicator(”One”); tspec1.setContent(R.id.content); tabs.addTab(tspec1); TabSpec tspec2 = tabs.newTabSpec(”Second Tab”); tspec2.setIndicator(”Two”); tspec2.setContent(R.id.content); tabs.addTab(tspec2); TabSpec tspec3 = tabs.newTabSpec(”Third Tab”); tspec3.setIndicator(”Three”); tspec3.setContent(R.id.content); tabs.addTab(tspec3); }
@Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu);
menu.add(0, ADD_NEW_TAB, 0, “New Tabs”); return true; }
//Dynamically delete tabs, then add one again //Problem with SDK 1.1 returns null pointer exception @Override public boolean onOptionsItemSelected(MenuItem item) {
TabHost tabs = (TabHost) this.findViewById(R.id.my_tabhost); tabs.clearAllTabs(); tabs.setup(); TabSpec tspec1 = tabs.newTabSpec(”New Tab”); tspec1.setIndicator(”NEWTAB”, this.getResources().getDrawable(R.drawable.icon)); tspec1.setContent(R.id.content); tabs.addTab(tspec1);
return super.onOptionsItemSelected(item); } }

No comments:

Post a Comment