The Android Developers TabWidget tutorial says the following:
"You can implement your tab content in one of two ways: use the tabs to swap Views within the same Activity, or use the tabs to change between entirely separate activities."
The tutorial goes on to demonstrate how you can use tabs with separate Activities. I have been unable to find an example of using tabs with different Views within the same Activity. I would rather not re-invent this particular wheel, so I am hoping someone here knows how this is done and can clue me in. Thanks!
Open the MainActivity class. Firstly, initialize ViewPager2 and TabLayout then set the adapter on ViewPager2. Then, create a TabLayoutMediator to link the TabLayout to the ViewPager2, and attach it.
Tabs are created using newTab() method of TabLayout class. The title and icon of Tabs are set through setText(int) and setIcon(int) methods of TabListener interface respectively. Tabs of layout are attached over TabLayout using the method addTab(Tab) method.
You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .
I think in the .setContent method of each tab you pass in the view you wish to use:
TabHost.TabSpec spec1 = tabs.newTabSpec("tag1"); spec1.setContent(R.id.AnalogClock01); spec1.setIndicator("Analog Clock");
Here's an example I found awhile back:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TabHost android:id="@+id/TabHost01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TabWidget android:id="@android:id/tabs" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="65px"> <AnalogClock android:id="@+id/AnalogClock01" android:layout_width="wrap_content" android:layout_height="wrap_content"></AnalogClock> <DigitalClock android:text="DigitalClock01" android:id="@+id/DigitalClock01" android:layout_width="wrap_content" android:layout_height="wrap_content"></DigitalClock> </FrameLayout> </TabHost> </LinearLayout>
And the Java code for this example is as follows:
import android.app.Activity; import android.os.Bundle; import android.widget.TabHost; public class tabexample extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TabHost tabs = (TabHost)findViewById(R.id.TabHost01); tabs.setup(); TabHost.TabSpec spec1 = tabs.newTabSpec("tag1"); spec1.setContent(R.id.AnalogClock01); spec1.setIndicator("Analog Clock"); tabs.addTab(spec1); TabHost.TabSpec spec2 = tabs.newTabSpec("tag2"); spec2.setContent(R.id.DigitalClock01); spec2.setIndicator("Digital Clock"); tabs.addTab(spec2); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With