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
Related
I currently have Tab Based app and the tabs are on bottom. Is there any way to put them up?
Add Tabs To Action Bar
To create tabs using ActionBar, you need to enable
NAVIGATION_MODE_TABS, then create several instances of ActionBar.Tab
and supply an implementation of the ActionBar.TabListener interface
for each one. For example, in your activity's onCreate() method, you
can use code similar to this:
#Override
public void onCreate(Bundle savedInstanceState) {
final ActionBar actionBar = getActionBar();
// Specify that tabs should be displayed in the action bar.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create a tab listener that is called when the user changes tabs.
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// show the given tab
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// hide the given tab
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// probably ignore this event
}
};
// Add 3 tabs, specifying the tab's text and TabListener
for (int i = 0; i < 3; i++) {
actionBar.addTab(actionBar.newTab().setText("Tab " + (i + 1)).setTabListener(tabListener));
}
}
How you handle the ActionBar.TabListener callbacks to change tabs
depends on how you've constructed your content. But if you're using
fragments for each tab with ViewPager as shown above, the following
section shows how to switch between pages when the user selects a tab
and also update the selected tab when the user swipes between pages.
From Docs.
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.
I build app that provide custom add and remove tabs from TABFragment. now i want to give id of every tab which i created custom. and get the id of where i am exactly. I also used tab.getPosition but it give me where am i exactly. For example i created five page. now when i am 4th tab and then i want to 5th tab id 5th page progress under background. Please give me best suggestion and any regards.Thank you
my Add button code.
btNewtab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
actionBar.addTab(actionBar.newTab().setText("New Tabs")
.setTabListener(HomeActivity.this)); //Adding new tab
COUNT_TAB+=1; //Add tab for counting
mAdapter.notifyDataSetChanged();
}
});
You can identify the individual tabs by Tag easily. just set the tag for each tab and perform action on your basis. i.e
COUNT_TAB+=1;//put this line before so that you can use it as tag
actionBar.addTab(actionBar.newTab().setText("New Tabs")
.setTabListener(HomeActivity.this).setTag(COUNT_TAB));
//If you want to set the selected tab initially different from default then set true or false to indicate which tab should be selected. you can change the position of the added tab also by setting the position. Below is the code i.e.
actionBar.addTab(actionBar.newTab().setText("TAB1").setTag("tab1")
.setTabListener(this),0,false);
actionBar.addTab(actionBar.newTab().setText("TAB2").setTag("tab2")
.setTabListener(this),1,true);
actionBar.addTab(actionBar.newTab().setText("TAB3").setTag("tab3")
.setTabListener(this),2,false);
//To handle the click event of the tabs you have to override the onTabSelected() method
i have put some sample code hope it will help you.
#Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
if (tab.getTag().equals("tab1")) {
// When the given tab is selected, show the tab contents in the
// container view.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt("myvalues", 1);
fragment.setArguments(args);
getFragmentManager().beginTransaction()
.replace(R.id.container, fragment).commit();
} else if (tab.getTag().equals("tab2")) {
Toast.makeText(this, "two clicked", Toast.LENGTH_LONG).show();
} else if (tab.getTag().equals("tab3")) {
Toast.makeText(this, "three clicked", Toast.LENGTH_LONG).show();
}
}
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...).
I have an activity that uses ActionBarSherlock to show an action bar with navigation tabs. I need one of those tabs to open another activity (taking over the whole screen) when it is tapped. I built a custom TabListener to achieve this, but it fires when the tab contents are initially rendered, not just when it's manually selected.
I can't see a way to catch a traditional onClick event, so I assume I need to handle this in the TabListener and somehow distinguish the initial selection from a later manual selection, but how? I could use a counter each time the tab is selected and only start the secondary activity if the counter is above zero, but that looks like an ugly hack. I was hoping there was a way to handle a "click" as opposed to a "selection" as the latter includes both the initial display of the views, as well as later manual clicks.
I attach the listener to the action bar navigation tab like this:
tab.setTabListener(new TabDiseaseSelectorListener(this));
And then the listener looks like this:
public class TabDiseaseSelectorListener implements ActionBar.TabListener {
private final Activity mActivity;
public TabDiseaseSelectorListener(Activity activity) {
mActivity = activity;
}
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction unused) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setClass(mActivity, DiseaseSelector.class);
startActivity(intent);
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction unused) {
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
I tried using onTabReselected() but that only fires if the tab is already selected and the user taps it again.