I am having a SherlockActionbar with Tab Using view-Pager
Current Output:: (Clearly you can see three tabs on top)
My Code::
MainActivity.java
public class MainActivity extends SherlockFragmentActivity {
// Declare Variables
ActionBar mActionBar;
ViewPager mPager;
Tab tab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
// Activate Navigation Mode Tabs
mActionBar = getSupportActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Locate ViewPager in activity_main.xml
mPager = (ViewPager) findViewById(R.id.pager);
// Activate Fragment Manager
FragmentManager fm = getSupportFragmentManager();
// Capture ViewPager page swipes
ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
super.onPageSelected(position);
// Find the ViewPager Position
mActionBar.setSelectedNavigationItem(position);
}
};
mPager.setOnPageChangeListener(ViewPagerListener);
// Locate the adapter class called ViewPagerAdapter.java
ViewPagerAdapter viewpageradapter = new ViewPagerAdapter(fm);
// Set the View Pager Adapter into ViewPager
mPager.setAdapter(viewpageradapter);
// Capture tab button clicks
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Pass the position on tab click to ViewPager
mPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
};
// Create first Tab
tab = mActionBar.newTab().setText("Tab1").setTabListener(tabListener);
mActionBar.addTab(tab);
// Create second Tab
tab = mActionBar.newTab().setText("Tab2").setTabListener(tabListener);
mActionBar.addTab(tab);
// Create third Tab
tab = mActionBar.newTab().setText("Tab3").setTabListener(tabListener);
mActionBar.addTab(tab);
}
}
activity_main.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="fill_parent"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
ViewPagerAdapter.java
public class ViewPagerAdapter extends FragmentPagerAdapter {
// Declare the number of ViewPager pages
final int PAGE_COUNT = 3;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int arg0) {
switch (arg0) {
// Open FragmentTab1.java
case 0:
FragmentTab1 fragmenttab1 = new FragmentTab1();
return fragmenttab1;
// Open FragmentTab2.java
case 1:
FragmentTab2 fragmenttab2 = new FragmentTab2();
return fragmenttab2;
// Open FragmentTab3.java
case 2:
FragmentTab3 fragmenttab3 = new FragmentTab3();
return fragmenttab3;
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return PAGE_COUNT;
}
}
How can i achieve this Or is this possible::(I need three tabs at top and three tabs at bottom)
If this is possible what changes should i need to make for my code ?
If I'm getting this right you want to have the same tabs in top and bottom simultaneously both tied to the contents in between?
If so, I would suggest extending tabhost activity from sherlock library and change the layout so that the tabs are in both in top and bottom using the same variable.
Hope this helps.
Related
I have a tab swiped layout which includes four tabs each of which have their own layout and fragment, in my main activity layout a viewpager takes part for changing tabs. specific view(tab) loads when app starts up and the others will be loaded when the tab changes. my problem is when I nevigate to the third or forth tab (not the second tab) a new instance of it's corresponding fragment will be created instead of loading previously created fragment (the expected behavior that only occur for the second tab).When I navigate to the second tab (SecondPageFragment()) and again come back to first tab it works correct and doesn't create a new IndexFragment, it load the previously created fragment instead so every thing is fine in this case but when I go to the third or forth tab and then come back to first tab it creates an new instance of IndexFragment.
what's the possible reason for that and why it only happens for third,forth etc tab and only second tab works correct and as expected ????
here is my codes that switch the tabs and their corresponding fragments
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new ForthPageFragment();
case 1:
return new ThirdPageFragment();
case 2:
return new SecondPageFragment();
case 3:
return new IndexFragment();
default :
break;
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 4;
}
}
and here is my main activity code :
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "صفحه چهارم","صفحه سوم","صفحه دوم","صفحه اصلی" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager)findViewById(R.id.tabpager);
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));
}
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) {
}
});
viewPager.setCurrentItem(3);
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
}
viewPager.setOffscreenPageLimit(3);
i have swipe tabs with 3 tab i first tab will have web view with Facebook other got text only , now if i swipe from first tab to the second then come back to the first its good but if i swipe to the 3rd tab when i go back to the first tab the web view will reload again , how can i stop that
this is my code
home.java
public class MainActivity extends FragmentActivity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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("Android").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("iOS").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Windows").setTabListener(tabListener));
}
}
ViewPager has a method setOffscreenPageLimit() that determines how many pages to retain when they go off screen.
try increasing the page limit. For example:
Tab.setOffscreenPageLimit(3);
I am developing an app which is using ActionBar.NAVIGATION_MODE_TABS and a separate FragmentPagerAdapter class in order to swipe through 5 different screens. On these screens there are buttons you can press to load a new activity, and then go back to the MainActivity when done. My problem is, every time you go back and reload the MainActivity it defaults back to the first tab (0).
I have tried many different solutions to try and get the previously selected tab to load when the activity is reloaded including SharedPreferences, saving instance state and trying to force the FragmentPagerAdapter class to load a certain fragment when called - but something keeps overriding it and it always goes back to the first tab (0) without fail. I would be really grateful for any possible solutions to this.
Code as follows:
public class MainActivity extends FragmentActivity implements TabListener {
ActionBar tabBar;
ViewPager viewPager;
#Override
protected void onCreate(Bundle currenttab) {
super.onCreate(currenttab);
setContentView(R.layout.activity_main);
// Sets up view pager
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setCurrentItem(0);
viewPager.setAdapter(new MainAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int currenttab) {
tabBar.setSelectedNavigationItem(currenttab);
}
#Override
public void onPageScrolled(int currenttab, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int currenttab) {
}
});
// Adds tabs to action bar
tabBar = getActionBar();
tabBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = tabBar.newTab();
tab1.setIcon(R.drawable.action);
tab1.setTabListener(this);
ActionBar.Tab tab2 = tabBar.newTab();
tab2.setIcon(R.drawable.jobs);
tab2.setTabListener(this);
ActionBar.Tab tab3 = tabBar.newTab();
tab3.setIcon(R.drawable.historic);
tab3.setTabListener(this);
ActionBar.Tab tab4 = tabBar.newTab();
tab4.setIcon(R.drawable.breeds);
tab4.setTabListener(this);
ActionBar.Tab tab5 = tabBar.newTab();
tab5.setIcon(R.drawable.special);
tab5.setTabListener(this);
tabBar.addTab(tab1);
tabBar.addTab(tab2);
tabBar.addTab(tab3);
tabBar.addTab(tab4);
tabBar.addTab(tab5);
}
// Action bar methods
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
// Class to deal with swipe function
class MainAdapter extends FragmentPagerAdapter {
public MainAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int currenttab) {
// TODO Auto-generated method stub
Fragment fragment = null;
if (currenttab == 0) {
fragment = new FragmentAction();
}
if (currenttab == 1) {
fragment = new FragmentJobs();
}
if (currenttab == 2) {
fragment = new FragmentHistory();
}
if (currenttab == 3) {
fragment = new FragmentBreeds();
}
if (currenttab == 4) {
fragment = new FragmentSpecial();
}
return fragment;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 5;
}
}
I managed to resolve this by making my back buttons behavior mimic the built in behavior of calling onBackPressed();. The problem was going back using my previous code was creating a new instance of my MainActivity thus defaulting it to the first tab.
I have 3 fragments... with their 3 navigation tabs.
The problem is that if i'm on the first tab, and i click the third.. when I click another time the first, it reloads all the fragment. If I click the second tab (in the middle) and I click the first, it doesn't reload.
My objective is that I don't want to refresh my fragments never!
How can I do it?
My code is:
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
// Return Items
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new Retos();
case 1:
return new Amics();
case 2:
return new Ranking();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
And this :
public class Perfil extends ActionBarActivity implements ActionBar.TabListener{
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private SearchView mSearchView;
private TextView mStatusView;
// Tab titles
private String[] tabs = { "TAB1", "TAB2", "TAB3" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.perfil);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getSupportActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
/** Creating fragment1 Tab */
Tab tab = actionBar.newTab()
.setText("TAB1")
.setTabListener(this);
actionBar.addTab(tab);
/** Creating fragment2 Tab */
tab = actionBar.newTab()
.setText("TAB2")
.setTabListener(this);
//.setIcon(R.drawable.ic_action_group);
actionBar.addTab(tab);
/** Creating fragment3 Tab */
tab = actionBar.newTab()
.setText("TAB3")
.setTabListener(this);
actionBar.addTab(tab);
/**
* 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) {
}
}
viewPager.setOffscreenPageLimit(C_FRAGMENTS_TO_KEEP_IN_MEMORY);
C_FRAGMENTS_TO_KEEP_IN_MEMORY is the number of tabs at right and left of the current selected tab to keep in memory. So in your case should be 2.
Please be sure that you are NOT creating a new fragment instance each time you call the getItem method in you viewpager adapter.
Check the #Rarw answer in this page.
Try setting your ViewPager offScreenPageLimit to cover the number of fragments you are trying to load. OffScreenPageLimist keeps fragments alive in an idle state when they are not visible. I don't see that value set in your code and based on what you're describing, that fragments keep recycling, it sounds to me like you're using the default state of 1, which will only retain one of the fragments off screen and not both.
Some caveats, this approach only really works if you know in advance how many fragments you will need since if you're dynamically adding and removing fragments its hard to know how many if any to retain.
UPDATE
This is likely why you're pages are refreshing:
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new Retos();
case 1:
return new Amics();
case 2:
return new Ranking();
}
return null;
}
You keep returning a new fragment each time you switch id. What you should do something like this:
case 0:
if(mRetrosFragment == null)
mRetrosFragment = new Retros();
return mRetrosFragment;
This way you stop recreating the fragment every time the tab changes and instead retain that instance.
This question already has answers here:
how to show text in lower case in android?
(6 answers)
Closed 3 months ago.
I created a tab application using Sherlock ActionBar Tab Views. I like to change the Tab title to lowercase. see screen shot
I want to change TAB1 to tab1
Any solution is greatly appreciated
my code as follow
public class MainActivity extends SherlockFragmentActivity {
// Declare Variables
ActionBar mActionBar;
ViewPager mPager;
Tab tab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
// Activate Navigation Mode Tabs
mActionBar = getSupportActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Locate ViewPager in activity_main.xml
mPager = (ViewPager) findViewById(R.id.pager);
// Activate Fragment Manager
FragmentManager fm = getSupportFragmentManager();
// Capture ViewPager page swipes
ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
super.onPageSelected(position);
// Find the ViewPager Position
mActionBar.setSelectedNavigationItem(position);
}
};
mPager.setOnPageChangeListener(ViewPagerListener);
// Locate the adapter class called ViewPagerAdapter.java
ViewPagerAdapter viewpageradapter = new ViewPagerAdapter(fm);
// Set the View Pager Adapter into ViewPager
mPager.setAdapter(viewpageradapter);
// Capture tab button clicks
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Pass the position on tab click to ViewPager
mPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
};
// Create first Tab
tab = mActionBar.newTab().setText("Tab1").setTabListener(tabListener);
mActionBar.addTab(tab);
// Create second Tab
tab = mActionBar.newTab().setText("Tab2").setTabListener(tabListener);
mActionBar.addTab(tab);
// Create third Tab
tab = mActionBar.newTab().setText("Tab3").setTabListener(tabListener);
mActionBar.addTab(tab);
}
thanks
Please add this in TabLayout tag of xml file
app:tabTextAppearance="#android:style/TextAppearance.Widget.TabWidget"
this will help you :)
try this cod
text-transform: capitalize;
Try This :-
tab = mActionBar.newTab().setText("Tab1").setTabListener(tabListener);
mActionBar.addTab(tab);
TextView x1 = (TextView) mTabHost.getTabWidget().getChildAt(0)
.findViewById(android.R.id.title);
x1.setAllCaps(false);
// Create second Tab
tab = mActionBar.newTab().setText("Tab2").setTabListener(tabListener);
mActionBar.addTab(tab);
TextView x2 = (TextView) mTabHost.getTabWidget().getChildAt(0)
.findViewById(android.R.id.title);
x2.setAllCaps(false);
// Create third Tab
tab = mActionBar.newTab().setText("Tab3").setTabListener(tabListener);
mActionBar.addTab(tab);
TextView x3 = (TextView) mTabHost.getTabWidget().getChildAt(0)
.findViewById(android.R.id.title);
x3.setAllCaps(false);