Android : change the style of the tabhost tabbar just like actionbar tabbar - android

I want to change the style of the tabhost tabbar ,same like actionbar tababar, I have tried to show in Figure.How can i do that ? PLease Help me thanks in advance.
I want to change the style from (1) to (2).
My code is below.
Class
package com.android.timeline;
#SuppressLint({ "SimpleDateFormat", "NewApi" })
public class MainActivity extends FragmentActivity implements ActionBar.TabListener
{
public int width;
private ActionBar actionBar;
private TextView actionBarTitle;
private TabPagerAdapter TabAdapter;
ArrayList<Fragment> fragmentlist = new ArrayList<Fragment>();
private ViewPager Tab;
private String[] tabs = { "About", "Watch Next", "Related" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentlist.add(new AboutDetail());
fragmentlist.add(new WatchNextDetail());
fragmentlist.add(new RelatedDetail());
currentAboutDetail = fragmentlist.get(0);
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager) findViewById(R.id.pager);
Tab.setOffscreenPageLimit(2);
pagerchangeListener();
}
private void pagerchangeListener() {
Tab.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
Tab.setAdapter(TabAdapter);
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("mMyCurrentPosition", Tab.getCurrentItem());
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mMyCurrentPosition = savedInstanceState.getInt("mMyCurrentPosition");
// where mMyCurrentPosition should be a public value in your activity.
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
}
public class TabPagerAdapter extends FragmentStatePagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int i) {
Log.e("LOG", "Position || " + i);
return fragmentlist.get(i);
}
#Override
public int getCount() {
return fragmentlist.size(); // No of Tabs
}
#Override
public void destroyItem(View container, int position, Object object) {
}
}
#Override
public void onTabSelected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
Tab.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</TabHost>
</RelativeLayout>

Try like below. Please go through FragmentTabHost and ViewPager with FragmentPagerAdapter
main.xml
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+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="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="#+id/tabFrameLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
MainActivity.java
public class MainActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTabHost = (FragmentTabHost) findViewById(R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.tabFrameLayout);
mTabHost.addTab(
mTabHost.newTabSpec("tab1").setIndicator("Tab 1",
getResources().getDrawable(android.R.drawable.star_on)),
FragmentTab1.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator("Tab 2",
getResources().getDrawable(android.R.drawable.star_on)),
FragmentTab2.class, null);
}
}

for this task refer this link http://wptrafficanalyzer.in/blog/implement-swiping-between-tabs-with-viewpager-in-action-bar-using-sherlock-library/

Related

Changing tab text and background color on selection

I want to change the tab color and text when it get selected i have tried lots in both xml and android nothing working pls help i am extending with the Fragment and using tabpageradapter heres my code
public class BusTab extends FragmentActivity implements OnTabChangeListener {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bus_tab);
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager)findViewById(R.id.pager);
Tab.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position); }
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
//Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener(){
#Override
public void onTabReselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}};
//Add New Tab
actionBar.addTab(actionBar.newTab().setText("BUSNO").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("ROUTE").setTabListener(tabListener));
}
#Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
}
}
Use tab_indicator for each tab as
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#color/white"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_tab_txt"
style="?android:attr/tabWidgetStyle"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="#string/live"
android:textColor="#drawable/tab_text_color"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_gravity="bottom"
android:background="#drawable/tab_underline_selector" />
</LinearLayout>
<View
android:layout_width="1dp"
android:id="#+id/tabs_divider_view"
android:layout_height="30dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#color/grey_background" />
</LinearLayout>
and drawable tab_text_color as
<?xml version="1.0" encoding="utf-8"?>
<item android:state_selected="true" android:color="#color/dark_blue"/>
<item android:state_pressed="true" android:color="#color/dark_blue"/>
<item android:color="#color/dark_gray" />
and tab_underline_selector as
<?xml version="1.0" encoding="utf-8"?>
<item android:drawable="#color/dark_blue" android:state_selected="true"/>
<item android:drawable="#color/transparent"/>
and in your Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.entries_tabs, container, false);
HorseRacesNowMainActivity.setHeaders(this);
mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
View tabIndicatorToday = LayoutInflater.from(getActivity()).inflate(R.layout.tab_indicator, mTabHost.getTabWidget(), false);
((TextView) tabIndicatorToday.findViewById(R.id.tv_tab_txt)).setText(getResources().getString(R.string.today));
mTabHost.addTab(mTabHost.newTabSpec(getResources().getString(R.string.today)).setIndicator(tabIndicatorToday), EntriesTodayFragment.class, null);
View tabIndicatorLive = LayoutInflater.from(getActivity()).inflate(R.layout.tab_indicator, mTabHost.getTabWidget(), false);
((TextView) tabIndicatorLive.findViewById(R.id.tv_tab_txt)).setText(getResources().getString(R.string.live));
mTabHost.addTab(mTabHost.newTabSpec(getResources().getString(R.string.live)).setIndicator(tabIndicatorLive), EntriesLiveFragment.class, null);
View tabIndicatorTrack = LayoutInflater.from(getActivity()).inflate(R.layout.tab_indicator, mTabHost.getTabWidget(), false);
((TextView) tabIndicatorTrack.findViewById(R.id.tv_tab_txt)).setText(getResources().getString(R.string.by_track));
tabIndicatorTrack.findViewById(R.id.tabs_divider_view).setVisibility(View.INVISIBLE);
mTabHost.addTab(mTabHost.newTabSpec(getResources().getString(R.string.by_track)).setIndicator(tabIndicatorTrack), EntriesTrackFragment.class, null);
return view;
}

Adding tabs again to tab fragment activity in android

I have an app which has navigation type fixed tabs + swipe. And I have created two tabs using two fragments, fragment 1 and fragment 2. It's working fine. Now again I need to create 3 tabs for the fragment 1. How can this be acheived??
This is my MainActivity:
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;// Tab titles
private String[] tabs = { "Flights","My Bookings" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
This is my TabsPagerAdapter class:
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int index) {
// TODO Auto-generated method stub
switch (index) {
case 0:
return new Flight();
case 1:
return new Hotel();
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
}
This is my mainactivity.xml file
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
Then there are two fragment activities Flight and Hotel in my app. Now Again I need to add tabs to the fragment activity flight. How can this be acheived?? Can someone please help me out...
Edit: This is an image from net. This is what I exactly want..
Add another Fragment Class Like this:
#Override
public Fragment getItem(int index) {
// TODO Auto-generated method stub
switch (index) {
case 0:
return new Flight();
case 1:
return new Hotel();
case 2:
return new Thirdfragment();
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
And Dont forget to add third tab in MainActivity class
private String[] tabs = { "Flights","My Bookings",ThirdFragment };
your layout of flight fragment must be changed to it:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"
>
<TabHost
android:id="#+id/TabHost01"
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"
android:layout_weight="0" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<anylayout
android:id="#+id/tab_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</anylayout>
<anylayout
android:id="#+id/tab_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="3dp" >
<anylayout
android:id="#+id/tab_3"
android:layout_width="match_parent"
android:layout_height="match_parent">
</anylayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
and in onCreatView :
mTabHost = (TabHost) v.findViewById(R.id.TabHost01);
mTabHost.setup();
mTabHost.addTab(mTabHost.newTabSpec("tab_1")
.setIndicator("View1_Child1")
.setContent(R.id.tab_1));
mTabHost.addTab(mTabHost.newTabSpec("tab_2")
.setIndicator("View2_Child2")
.setContent(R.id.tab_2));

ViewPager in android Null pointer exception

ViewPager gives null pointer exception.
I am trying to add tab and view pager inside fragment
Below is my code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState)
{
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.activity_main_fragment_layout);
mViewPager = (ViewPager) mTabHost.findViewById(R.id.tabHostWithViewPager);
for (int i = 0; i < 5; ++i) {
mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Fragment B"), FragmentB.class,
null);
}
FragmentPagerAdapter fragmentPagerAdapter = new
FragmentPageAdapter(getActivity().getSupportFragmentManager());
mViewPager.setAdapter(fragmentPagerAdapter);
mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageScrollStateChanged(int arg0) {
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageSelected(int arg0) {
TabWidget widget = mTabHost.getTabWidget();
int oldFocusability = widget.getDescendantFocusability();
widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
mTabHost.setCurrentTab(arg0);
widget.setDescendantFocusability(oldFocusability);
// actionBar.setSelectedNavigationItem(arg0);
}
});
return mTabHost;
}
activity_main_fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<android.support.v4.view.ViewPager
android:id="#+id/tabHostWithViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</TabHost>
</LinearLayout>
FragmentPagerAdapter.java
public class FragmentPageAdapter extends FragmentPagerAdapter {
public FragmentPageAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public int getCount() {
return 5;
}
#Override
public Fragment getItem(int arg0) {
return new FragmentA();
}
}
Please help me to solve this issue.I can add tab in fragment but gets null pointer exception when trying to implement viewpager.Viewpager gives null pointer exception
Please find out the logcat

Fragments overlapping using ListFragment and FragmentTabHost

My FragmentTabHost and ListView works fine until I clicked an item in ListView then change Tab.That is when the Fragments overlap.
Here is the image after clicking an item in ListView then changing from Tab 1(where the ListView resides) to Tab 2(another fragment is called).
http://i904.photobucket.com/albums/ac243/fookywooky/OverlappingFragments_zps2b60e9ab.png
Here is my main activity
public class MainActivity extends FragmentActivity {
private FragmentTabHost hostTab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hostTab = (FragmentTabHost)findViewById(android.R.id.tabhost);
//hostTab = (TabHost) findViewById(R.id.tabhost);
hostTab.setup(this,getSupportFragmentManager(), android.R.id.tabcontent);
hostTab.addTab(
hostTab.newTabSpec("tab1").setIndicator("Tab 1",
getResources().getDrawable(android.R.drawable.star_on)),
FragmentTab.class, null);
hostTab.addTab(
hostTab.newTabSpec("tab2").setIndicator("Tab 2",
getResources().getDrawable(android.R.drawable.star_on)),
FragmentTab1.class, null);
hostTab.addTab(
hostTab.newTabSpec("tab3").setIndicator("Tab 3",
getResources().getDrawable(android.R.drawable.star_on)),
FragmentTab2.class, null);
}
And here is my fragment for Tab 1(with ListView)
public class FragmentTab extends ListFragment implements OnItemClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] values = new String[] { "Ang na Ang na", "Sulakihin", "Somaskidot", "Moko Moko"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
switch(position){
case 0:
// Create new fragment and transaction
Fragment newFragment = new FragmentInListView();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(android.R.id.tabcontent, newFragment);
transaction.addToBackStack(null);
transaction.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
// Commit the transaction
transaction.commit();
break;
default:
}
}
}
I just added 1 fragment for 1 item in the ListView for testing. Still no fragments for Items 2,3 and 4.
Here is my code for the Tab 2.
public class FragmentTab1 extends Fragment {
FragmentActivity main;
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_layout1,
container, false);
TextView tv= (TextView) view.findViewById(R.id.text);
tv.setText("Fragment 2");
return view;
}
}
And here is my activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/replace"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.app.FragmentTabHost
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"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
And here is the xml file for the list view fragment.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:id="#+id/tanga" >
<ListView
android:id="#+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
and lastly, here is the xml for Tab 2
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:id="#+id/bobo">
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:text="#string/hello_world"
android:textAppearance="?android:attr/textAppearanceMedium" />
Just feel free to ask if you want anymore code to be posted. I will be grateful for any help or comments. Please help. Thanks in advance.

Set Default Tab for Fragment/ViewPager setup

How do you set a default tab when the FragmentActivity is first created? I have 5 tabs and want it to open to tab 2, not 1. It seems like it should be much easier than it is!
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_layout);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment f = null;
switch (position) {
case 0: {
f = new MasterFrag();
Bundle args = new Bundle();
f.setArguments(args);
break;
}
// case 1 - 3 edited out; i'd like default view "case 1".
default:
throw new IllegalArgumentException("not this many fragments: "
+ position);
}
return f;
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.mastercattab1).toUpperCase();
// case 1- 3 edited out
}
return null;
}
}
And my XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</TabHost>
</LinearLayout>
Have you tried calling mViewPager.setCurrentItem(tab.getPosition()); at the end of onCreate()?

Categories

Resources