How to change the selected tab in a tabLayout programmatically - android

I have a tab layout with a view pager and 5 different tabs, if the user is not registered only one option is available, so I want to disable the click on the other tabs. What I did is override the onTabSelected to change the current item in the viewPager.
#Override
public void onTabSelected(TabLayout.Tab tab) {
if (User.current != null) {
viewPager.setCurrentItem(tab.getPosition());
} else {
viewPager.setCurrentItem(0);
}
}
It works perfectly but it has one problem, the tab indicator change to the selected tab, so I want to keep the tab indicator on the first tab.

Related

tab indicator not moving when swiping but moves on selection

I have a main activity with 3 tabs,when i swipe the tabbed indicator doesn't move but the layout changes while selecting a tab the indicator moves and the layout changes too.
Make sure you have a OnPageChangeListener attached to your ViewPager
mViewPager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
mTabs.getTabAt(position).select();
}
});
You can also do like
tabLayout.setupWithViewPager(viewPager);

Swipe Views with Tabs: Modification required

I have implemented Swipe Views with Tabs from the Android's official website: http://developer.android.com/training/implementing-navigation/lateral.html
I want to make two modifications to this code:
The Tab bar on the top shows three different tabs. On top left; the previous tab, on top right; the next tab and on top mid; the current/selected tab. I want to hide the previous and the next tabs. I want it to show only the current/selected tab bar.
In this code, each tab has only one fragment. I want Tab 2 to have 3 fragments lined horizontally. What I want is, if I swipe on from the top on the Tabs, I will go directly to the next/previous Tabs. But if I swipe on the screen, below the Tabs, I should be able to swipe three times for tab two before I reach tab 3. To elaborate: I need horizontally scroll-able List-views in each fragment.
Is this the right direction for this? Any help would be appreciated.
I didn't understand your first point, but about grouping several fragments under the same tab, you can do it by adding your own listeners for tabs pressed and for viewPager swipe, instead of letting the view Pager implement that logic. Here is an example:
//setup tabs
mTabLayout = (TabLayout) findViewById(R.id.tabs);
mTabLayout.addTab(mTabLayout.newTab("Prev"));
mTabLayout.addTab(mTabLayout.newTab("Current"));
mTabLayout.addTab(mTabLayout.newTab("Next"));
//Listen for pressed tabs
mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
if (mDissableTabListener) {
return;
}
if (tab.getPosition() == 0) {
//go to first position
mViewPager.setCurrentItem(0);
} else if (tab.getPosition() == 1){
//i.e: go to your first tab at the center
,ViewPager.setCurrentItem(1);
} else if (tab.getPosition() == 2) {
//go to last position
if (mViewPager.getCurrentItem() == mViewPager.getAdapter().getCount() -1) {
//we are in the last tab, so move to first position
mViewPager.setCurrentItem(0);
}
}
}
...
});
//Now use a OnViewPagerListener to change selected tab when there is a swipe
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//avoid the TabSelectedMethod to be called (it would change the viewPager current item)
mDissableTabListener = true;
if (position == mViewPager.getAdapter().getCount() -1){
//When swipe to last
mTabLayout.getTabAt(2).select();
} else if (position == 0){
//select first tab
mTabLayout.getTabAt(0).select();
} else {
//select center tab
mTabLayout.getTabAt(1).select();
}
mDissableTabListener = false;
}
...
});

disabling tab navigation through action bar if tab validation fails

I am running an app with 3 tabs that support both swiping and action bar for tab navigation. i set up a validation check so that it when tab 2 is selected, if certain requirements arent met, it returns to tab 1.
It works well with swiping (if swiping from tab1 to tab2, it displays the error message and returns to tab 1) but with the action bar, if an action bar button is pressed for tab2, the error message is displayed and tab1's view displayed but the action bar button remains on the tab 2.
I had tried the following script but with no luck of changing the active tab button back to the first tab. This is especially a problem since data is supposed to be saved to sqlite when whenever a new tab is selected.
public void onTabSelected(Tab tab, FragmentTransaction ft) {
check = GlobalApp.data().value;
if(tab.getTag() == "Product")
{
if(check == "Select Client")
{
actionBar.setSelectedNavigationItem(0);
viewPager.setCurrentItem(0);
alert.showAlertDialog(Invoice2.this,
"Error",
"Client name not selected", true);
}
else
{
viewPager.setCurrentItem(tab.getPosition());
}
}
else if ((tab.getTag() == "Confirm"))
{
viewPager.setCurrentItem(tab.getPosition());
}
First bind the tabs to the ViewPager:
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
//Do the criteria check here; AFTER setting current item.
}
then Viewpager to the tabs:
viewPager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
getActionBar().setSelectedNavigationItem(position);
//Do the criteria check here; AFTER setting current item.
}
});
After that you can perform check for your criteria after commented lines. Regardless of you changing the tab or page, your tabs and your pages should change synchronously. For more info check this Android Developers training page

TabActivity OnTabChangedListener - how to get the previous selected tab

I have a tab where when the user click on it i want a dialog to appear and keep him on the current tab (without switching to the selected tab).
I have the code of the dialog and the tab listener working but how do i keep the curren tab?
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
if(tabId.equals("SomeThing") && !(AppSettings.getIsFullVersion()))
{
callFullVersionDialog("Sorry, SomeThing is only available on full version");
// finish();
}
}});
You can always do this,
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in the ViewPager.
// code to show your Dialog box here.
mViewPager.setCurrentItem(position of your current tab here);
}
if each tab is a seperate fragment put your fragment position in there (0,1,2,3...).

How do I make all tabs in a TabHost unselected

I need to show tab content on occasion, otherwise the area must be filled with "non-tabhost" data.However, tabs should be visible and when user clicks any of those tabs "non-tabhost" must be hidden and appropriate tab content must become visible.
It's something connected to a fake tab creation ?
Give an example of creating TabHost with tabs unselected.
Thanks.
What I usually do is, add an extra Tab and use setVisibility(View.GONE) to hide it. THis will just hide the tab button from the user, and the Tab will still be there, in the "background" and you can programmatically select it, by using tabHost.setCurrentTab(0). I also usually keep this tab as the first one.
1.copy the code where you want to tabs make unselected
tabLayout.setSelectedTabIndicatorColor(Color.WHITE);
tabLayout.setTabTextColors(Color.BLACK, Color.BLACK);
2.Override on Tabselected Listener and paste the following code
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override`enter code here
public void onTabSelected(TabLayout.Tab tab) {
tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#EB1C23"));
tabLayout.setTabTextColors(Color.BLACK, Color.RED);
viewPager.setCurrentItem(position);
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#EB1C23"));
tabLayout.setTabTextColors(Color.BLACK, Color.RED);
viewPager.setCurrentItem(position);
}
});

Categories

Resources