Currency Exchange isn't working in Android - android

I have run into an obstacle when doing my Android application. My issue is that I cannot create an onTabListener for my tabs. I have three tabs with different names. The problem is that they all have the same tab count. In my code I ran this:
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
int n = getActionBar().getTabCount();
Toast.makeText(getApplicationContext(), "You have selected: " + n, Toast.LENGTH_LONG).show();
}
This makes a toast saying the value of the tab (for example: 0, 1, 2, 3), but all of the tabs have the same indicator. I cannot make an onTabListener if I have all my tabs have the same indicator. Here is my full code:
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
int n = getActionBar().getTabCount();
Toast.makeText(getApplicationContext(), "You have selected: " + n, Toast.LENGTH_LONG).show();
}
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(tabTitle[i])
.setTabListener(tabListener));
}
What is the issue? Is there any way I can create an onTabListener using the tab's title? Any help to this problem is greatly appreciated.

all you are doing is getting the number of tabs you have...
you need to do tab.getPosition() to get what one selected
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
int n = tab.getPosition();
Toast.makeText(getApplicationContext(), "You have selected: " + n, Toast.LENGTH_LONG).show();
}

Related

how to get last selected action bar tab?

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

Creating Swipe Views with tabs in Android 5.0

In making the swipe tabbed layout as below prior to Android 5.0:
Simply required the code as:
#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));
}
}
But given the methods for navigation on the ActionBar are deprecated, How can I implement this type of view to be compatible withe Android 5.0 and previous versions?
You need to update to Toolbar here is the documentation.
http://android-developers.blogspot.mx/2014/10/appcompat-v21-material-design-for-pre.html

How to add tabs in the action bar

I added some tab buttons in the action bar of my app. But I don't know how to add the coding to the each tab button. I want to open a new layouts from each tab button. I can't find the ids for these buttons.(I'm a beginner for android) Thank you!
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
};
for (int i = 0; i < 3; i++) {
actionBar.addTab(
actionBar.newTab()
.setText("Tab " + (i + 1))
.setTabListener(tabListener));
}
I got tabs by using this this code in the onCreate method.
/** Creating Android Tab */
Tab tab = actionabar.newTab().setText("My Tabs").setIcon(R.drawable.myfriends).setTabListener(tabListener);
Tab tab = actionabar.newTab().setText("Tab1").setTabListener(tabListener);
actionabar.addTab(tab);
tab = actionabar.newTab().setText("Tab2").setTabListener(tabListener);
actionabar.addTab(tab);
tab = actionabar.newTab().setText("Tab3").setTabListener(tabListener);
actionabar.addTab(tab);
Use switch case to shift to tabs
switch (position) {
case 0:
tabLayout = R.layout.tab1;
break;
case 1:
tabLayout = R.layout.tab2;
break;
case 2:
tabLayout = R.layout.tab3;
break;
}

ActionBarSherlock - selecting tab by clicking on other tabs

I have an example with 3 tabs and one button.
public class MainActivity extends SherlockActivity implements TabListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
setContentView(R.layout.activity_main);
addTab("1", 0, false);
addTab("2", 1, false);
addTab("3", 2, false);
Button cmdClick = (Button) findViewById(R.id.cmdClick);
cmdClick.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
getSupportActionBar().setSelectedNavigationItem(0);
}
});
}
private void addTab(String tabTitle, int position, boolean setSelected) {
ActionBar.Tab tab = getSupportActionBar().newTab();
tab.setText(tabTitle);
tab.setTabListener(this);
getSupportActionBar().addTab(tab, position, setSelected);
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Log.d("example", "Selected " + tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
Log.d("example", "Unselected " + tab.getPosition());
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Log.d("example", "Reselected " + tab.getPosition());
}
}
When I click on the button it automatically selects first tab. I would like to automatically select first tab whenever I click on the second or on the third tab. I tried like this
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Log.d("example", "Selected " + tab.getPosition());
getSupportActionBar().setSelectedNavigationItem(0);
}
but it doesn't work. Any ideas?
Thanks.
Edit:
Maybe this example doesn't have any sense but this is just a simplified example of what I'm trying to do. I would like to have 2 tabs by default, one with title "1" and second one with title "+". When user selects "+" tab I would like to create new tab with title "2" (between tabs "1" and "+") and to automatically select tab "2".
First of all I'd like to discourage this kind of behavior. This will be VERY confusing for your users and frankly doesn't make any sense.
That said I tried hacking it but it doesn't seem like it's that easy to do. I did following:
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if(tab.getPosition() == 0) {
int tabs = mActivity.getSupportActionBar().getTabCount();
if(tabs > 3) {
ActionBar.Tab test = mActivity.getSupportActionBar().getTabAt(2);
test.select();
return;
}
}
Edit:
For the behavior your describing I think it would be much easier to implement it by creating your own tabs. You could use radio buttons and/or buttons to implement it easily.
Now this causes the tab indicator to still point at the old tab but the fragment inside is updated to the selected one =/ so not quite there...

Android TabWidget without TabHost with custom actions

Is possible to use TabWidget without TabHost? I wanna something like Tabs navigation for ActionBar failback for older phones.
So I only want to show user tabs and listen on click actions, where I get active tab ID. Nothing more.
I know in common situations TabsNavigatin for actionBar is just for navigate through Fragments. But I easily avoid Fragments. :
class mTabListener implements ActionBar.TabListener {
private Screen screen;
public mTabListener(Screen screen) {
this.screen = screen;
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
screen.onTabReselected(tab, ft);
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
screen.onTabSelected(tab, ft);
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
screen.onTabUnselected(tab, ft);
}
}
public abstract class Screen extends Activity {
protected void addTab(String title, int what, boolean selected) {
if (Global.API < 11)
return;
ActionBar bar = getActionBar();
Tab tab = bar.newTab()
.setTag(new Integer(what))
.setTabListener(new mTabListener(this))
.setText(title);
bar.addTab(tab, selected);
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Integer what = (Integer)tab.getTag();
tabSelected(what);
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
public void tabSelected(int what) {
}
}
But I can't find how to add tab buttons to TabWidget;
No. Tab widget without tab host is like using pushbutton for the tabs. You will have do the manipulation of tab navigation.
Here is the solution, TabWidget without tabHost
https://github.com/muratonnet/android-SingleTabWidget

Categories

Resources