Android 1.5 programming using TabHost and buttons
I’m currently trying out the Android 1.5 SDK and have seen several examples on TabHost.
What I want to do is use a different button on each tab to accomplish its task.
I tried
OnClickListiner() and onClick() are being used. I think this is used by all developers, but every time I press the button I get an empty exception on LogCat. I also have every XML layout, so I call Tab tab.add(… setContent(R.id.firstTabLayout))
firstTabLayout = layout for Button and TextView.
What is the best way to make a button/TextView work under TabHost?
Solution
I’m not entirely sure what your problem is, but this is how I set up tabbed activities earlier
The layout .xml
<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">
<LinearLayout android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@android:id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:id="@+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@android:id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:id="@+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@android:id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
Tab.java
public class InitialActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
TabHost th = getTabHost();
th.addTab(th.newTabSpec("tab_1").setIndicator("Tab1").setContent(R.id.tab1));
th.addTab(th.newTabSpec("tab_2").setIndicator("Tab2").setContent(R.id.tab2));
th.addTab(th.newTabSpec("tab_3").setIndicator("Tab3").setContent(R.id.tab3));
}
}
You can then use findViewById()
on any View in the tab, or if there is a shared name between them, you can do findViewById(R.id.tab1).findViewById(R.id.text).