How can I get last selected Action Bar Tab from which I'm navigating to next Tab?.
what I've tried :
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
adapter = new FragmentPagerAdapter(getSupportFragmentManager(),
title);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(adapter);
viewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
/* (non-Javadoc)
* #see android.support.v4.view.ViewPager.SimpleOnPageChangeListener#onPageSelected(int)
*/
#Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < adapter.getCount(); i++) {
ActionBar.Tab tab = actionBar.newTab();
tab.setText(adapter.getPageTitle(i));
tab.setTabListener(this);
actionBar.addTab(tab);
}
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
// here How Can I get previous selected tab from which I am navigating
}
So in onTabSelected() method How can I know from which Tab I'm navigated to this Tab.Is there is any way?
You can create a field called
int fLastTab = -1;
This field you can update in the function onTabSelected
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
// Here you can check the value of fLastTab,
// if fLastTab == -1 there wasn't any last selected tab
// and if it has another value you have the last selected tab
// at the end of this function you will update the value of fLastTab
// Doing somenthing with fLastTab....
fLastTab = tab.getPosition();
}
There is another way, you can override the function
public void onTabUnselected(Tab tab, FragmentTransaction ft){
// Doing somenthing with the last selected action bar tab (tab.position())...
}
where you have the position of the tab who exits the selected state. onTabUnselected
Related
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 trying to bind the ActionBar Tabs to a ViewPager. At the very beginning I created two separate projects for ActionBar Tabs and ViewPager, and they were working fine. When trying to bind the to each other, according to the code below, the ViewPager comply to the TabListener, in other sense, when I touch an ActionBar Tab the ViewPager change accordingly and display the respective View.For an example, I have three Tabs, when touching the Tab number two, the ViewPager displays the respective page number two. and so on.
But the ActionBar Tabs do not obey the ViewPager, in other sense, when swiping the screen to move to the next page of the ViewPager, the ViewPager shows the respective View BUT the ActionBar Tab does not change its current selected state according to the current ViewPager's selected View. For an example, when swiping to to the ViewPage number three, the ViewPager displays its respected View which is number three, BUT, the current selected ActionBar Tab does not change to be number three. I could be accessing ViewPage three while the highlighted Tab is number one.
I hope I explained the problem clearly.
MainActivity
private ViewPager mViewPager;
private MyTabsPagerAdapter mPagerAdapter;
private ActionBar mActionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Fragment> mFragList = new ArrayList<Fragment>();
mFragList.add(new Fragment01());
mFragList.add(new Fragment02());
mFragList.add(new Fragment03());
mViewPager = (ViewPager) findViewById(R.id.pager);
mActionBar = getActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mPagerAdapter = new MyTabsPagerAdapter(getSupportFragmentManager(), mFragList);
mViewPager.setAdapter(mPagerAdapter);
for(int i=0; i<mFragList.size(); i++) {
mActionBar.addTab(mActionBar.newTab().setText("Fragment0"+(i+1)).setTabListener(this));
}
}
#Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
mViewPager.setCurrentItem(arg0.getPosition());
//mActionBar.setSelectedNavigationItem(arg0.getPosition());
}
#Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
}
MyTabsPagerAdapter
public class MyTabsPagerAdapter extends FragmentPagerAdapter {
List<Fragment> mFragList;
public MyTabsPagerAdapter(FragmentManager fm, List<Fragment> mFragList) {
super(fm);
// TODO Auto-generated constructor stub
this.mFragList = mFragList;
}
#Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return this.mFragList.get(arg0);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return this.mFragList.size();
}
}
You need to set a listener for when the viewpager changes for example,
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
This waits for the viewpager to change and when it does, it sets the actionbar position equal to the position you moved the viewpager to.
I want to change the tab colour in the ViewPager. I search but didn't find any good solution for it. I want to change the tab colour from default that is from black to white. Is their any way to do this programatically for i have to modify style.xml. I don't know how to do as i am new to android. plz somebody help
public class HomeActivity extends FragmentActivity implements ActionBar.TabListener
{
public ViewPager viewPager;
private PageAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Calculate EMI", "EMI Schedule"};
String TabFragmentSchedule;
public void setTabFragmentSchedule(String t){
TabFragmentSchedule = t;
}
public String getTabFragmentSchedule(){
return TabFragmentSchedule;
}
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#0F7BFF"));
actionBar.setBackgroundDrawable(colorDrawable);
mAdapter = new PageAdapter(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) {
}
});
}
//method that is called to swipe viewpage on button click from calculation fragment
public void switchToFragmentSchedule(){
// viewPager.setCurrentItem(1);
viewPager.setCurrentItem(1, false);
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
You should style your theme in your xml files. You can find all the information for this task here.
If you need to change it programmatically, you can use
mActionBar.setBackgroundDrawable(new ColorDrawable(0xffffffff));
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayShowTitleEnabled(true);
As a beginner, you might also find this styling tool very helpful.
I developed an android applicaion with acion bar that has 3 tabs with 3 difrrent fragments and I tried to pass a parameter from parent activity to fragments, I used the solution given in THIS qusetion and every thing is right, the only problem is that, now the activity sends this parameter to fragments just once at the start f applicaion, I need to get this parameter each time the user select the tabs, I think I should do this opration on onTabSelected method but have no idea how to do that, any suggestion?
Try this !!
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());
Log.d("tab selected",""+tab.getPosition());
if(tab.getPosition==0)
{
Fragment fragA=new FrgmentA();
fragA.getData("data");
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
Log.d("tab Unselected",""+tab.getPosition());
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
Log.d("tab Reselected",""+tab.getPosition());
}
};
// Create first Tab
tab = mActionBar.newTab().setText("Profile").setTabListener(tabListener);
mActionBar.addTab(tab);
// Create second Tab
tab = mActionBar.newTab().setText("Picture").setTabListener(tabListener);
mActionBar.addTab(tab);
// Create third Tab
tab = mActionBar.newTab().setText("Help").setTabListener(tabListener);
mActionBar.addTab(tab);
FragmentA extends Fragment{
public FragmentA(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view =inflater.inflate(R.layout.fragment_image, container, false);
return view;
}
public void getData(String s)
{ // your implementation
}
}
hey please pass the value when the fragment instance is created ,please take a look on below codes
I just passwed an list once the fragment is loaded via viewpager
private void setupViewPager(ViewPager viewPager, List<AccountDetail> listAc) {
Homepage.ViewPagerAdapter adapter = new Homepage.ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new AllFragment("", listAc), "All");
adapter.addFrag(new AllFragment("1", listAc), "Credit");
adapter.addFrag(new AllFragment("2", listAc), "Debit");
viewPager.setAdapter(adapter);
}
And then write a constructor in your fragment be like this
public AFragment(String text, List<AccountDetail> manufactist) {
mText = text;
manufactureListAcoonut = manufactist;
}
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);