android - how to add some Fragment into TabHost? -
i wrote app contain tabhost. each tab contain linearlayout - , want add different exist fragment created each of tab
how ?
private void setnewtab(context context, tabhost tabhost, string tag, string title, int contentid ){ tabhost.tabspec tabspec = tabhost.newtabspec(tag); tabspec.setindicator(title); fragment newfragment = new actionfragment (); tabspec.setindicator(newfragment.getview()); tabspec.setcontent(contentid); tabhost.addtab(tabspec); // after line exception } <tabhost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" > <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <tabwidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" > </tabwidget> <framelayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" > <linearlayout android:id="@+id/tab1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_blue_light"> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="tab 1 content" /> </linearlayout> <linearlayout android:id="@+id/tab2" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_red_dark"> <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="tab 2 content" /> </linearlayout> <linearlayout android:id="@+id/tab3" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_green_light"> <textview android:id="@+id/textview3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="tab 3 content" /> </linearlayout> </framelayout> </linearlayout>
use viewpager
fragmentstatepageradapter
if many tabs. if have 2 3 tabs use pageradapter
.
check out below link, explained how why use viewpager
tablayout
.
https://guides.codepath.com/android/google-play-style-tabs-using-tablayout
tablayout
viewpager
goes in scenario.
Comments
Post a Comment