Set Default Tab for Fragment/ViewPager setup - android

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()?

Related

Android - PageViewer View stuck on screen

i'm trying to get this pageviewer with fragments to work however i'm stumbled on a problem where the view stays on screen on swipe. Ideally, i'd want it to only be on a particular layout yet i'm not too sure why it "carries over" to other screens (it also stays on screen during the transition between fragments).
Any feedback will be appreciated!
Link to Imgur Screenshots
Main:
private void initUI()
{
// ---------Page Viewer with Fragments -----------
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getSupportActionBar();
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));
}
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 onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
Adapter:
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Main Frag
return new Fragment1();
case 1:
// Second Frag
return new Fragment2();
case 2:
// Third Frag
return new Fragment3();
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
Fragment classes are basically this:
public class Fragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main, container, false);
return rootView;
}
}
Main Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
The EditText is in the main layout, the same layout that the ViewPager is in. It should be inflated within the tab. You'll need to create a separate layout for each tab.
Main layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
layout_fragment_1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Then you need to inflate the new layout in fragment 1.
View rootView = inflater.inflate(R.layout.layout_fragment_1, container, false);
return rootView;

How show ListView in ActionBarTab?

I created ActionBarTab and i need add ListView in witch tab that ListViews have difference data.
But i cant show my listView in Tabs .
MainActivity.xml :
#SuppressLint("NewApi")
public class MainActivity extends FragmentActivity implements ActionBar.TabListener{
static String [] Years;
static ArrayAdapter<String> adapter;
static ListView Lists;
public static Context context;
public static Activity activity;
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
ViewPager mViewPager;
private String[] tabs = { "tab 1", "tab 2", "tab 3","tab 4","tab 5" };
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
final ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab().setText(tabs[i])
.setTabListener(this));
}
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
switch (i) {
case 0:
Years = context.getResources().getStringArray(R.array.YEAR);
Lists = (ListView) activity.findViewById(R.id.list);
adapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1, Years);
Lists.setAdapter(adapter);
case 1:
case 2:
case 3:
case 4:
default:
return null;
}
}
#Override
public int getCount() {
return 5;
}
#Override
public CharSequence getPageTitle(int position) {
return "Section " + (position + 1);
}
}
}
NOTICE : In switch case 0 : I can't show ListView .
activity_main.xml :
<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" >
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</android.support.v4.view.ViewPager>
simple_list_item_1.xml :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="6dip"
android:textAppearance="?android:attr/textAppearanceLarge" />
This is my string-array in string.xml, I fill my listView from here :
<string-array name="YEAR">
<item>1992</item>
<item>1993</item>
<item>1994</item>
<item>1995</item>
<item>1996</item>
<item>1997</item>
<item>1998</item>
<item>1999</item>
</string-array>
OK . create a fragment_section_listview.xml :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</FrameLayout>
Notice : Delete ListView from activity_main.xml change it to :
<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" />
Then create a class in your MainActivity :
public static class LstView extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_listview, container, false);
Years = context.getResources().getStringArray(R.array.YEAR);
Lists = (ListView) rootView.findViewById(R.id.list);
adapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1, Years);
Lists.setAdapter(adapter);
return rootView;
}
}
Then write this in your switch case:
switch (i) {
case 0:
return new LstView();
Other Code .....

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));

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

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/

How to add more items in each tab of TabWidget?

I have written a simple tab program using TabHost and TabWidget but I am able to add only one item to each tab.. I want to add a textview, an imageview and a button but I am unable to.. This is my xml code:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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="98dp" >
<TextView
android:id="#+id/tab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Tab1"
android:textColor="#ffffff"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Tab2"
android:textColor="#ffffff"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Tab3"
android:textColor="#ffffff"
android:textSize="16sp"
android:textStyle="bold" />
</FrameLayout>
</LinearLayout>
</TabHost>
How do I add an imageview along with the existing textview to Tab1?
you can use tab in action bar, like this:
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
private static final int NUM_PAGES = 3;
in onCreate()
final ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabReselected(Tab tab,
android.app.FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab,
android.app.FragmentTransaction ft) {
mPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab,
android.app.FragmentTransaction ft) {
}
};
for (int i = 0; i < mPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar
.newTab()
.setText(mPagerAdapter.getPageTitle(i))
.setIcon(
((ScreenSlidePagerAdapter) mPagerAdapter)
.getPageIcon(i))
.setTabListener(tabListener));
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return Fragment.instantiate(MainActivity.this,
Fragment_sports.class.getName());
case 1:
return Fragment.instantiate(MainActivity.this,
Fragment_casino.class.getName());
case 2:
return Fragment.instantiate(MainActivity.this,
Fragment_live_betting.class.getName());
default:
break;
}
return null;
}
#Override
public int getCount() {
return NUM_PAGES;
}
#Override
public CharSequence getPageTitle(int position) {
String tabLabel = null;
switch (position) {
case 0:
tabLabel = " Sports";
break;
case 1:
tabLabel = "Casino";
break;
case 2:
tabLabel = "Live Betting";
break;
}
return tabLabel;
}
public int getPageIcon(int position) {
int id = 0;
switch (position) {
case 0:
id = R.drawable.icon_all_sports_d;
break;
case 1:
id = R.drawable.icon_favourites_d;
break;
case 2:
id = R.drawable.icon_live_d;
break;
default:
break;
}
return id;
}
}
and your main_activity.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.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>

Categories

Resources