0

I'm trying to set up a onTabChangedListner() and I've looked around Stack Overflow and some other sites and it's pretty much the same thing everywhere so I copied that code. Here's my current code(this is all within on onCreate())

getTabHost().setOnTabChangedListner(new onTabChangeListner() {

    @Override
    public void onTabChanged(String tabId) {
        Toast.makeText(this, "A new tab has been selected", Toast.LENGHT_SHORT).show();
    }
});

I'm getting 3 errors:

  1. Unknown entity OnTabChangeListner()

  2. There is no applcable method to (com.mycompany.myapp.MainActivity.(anonymous), java.lang.String,int)

  3. Method android.widget.TabHost.setOnTabChangedListner(android.widget.TabHost.OnTabChangeListner) in 'android.widget.TabHost'can not be applied to (com.mycompany.myapp.MainActivity.(anonymous))

Any help would be appreciated!

Update: Here is my entire onCreate followed by a onTabChanged method:

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

    TabHost tabHost = getTabHost();

    TabSpec cat1spec = tabHost.newTabSpec("Category 1");
    cat1spec.setIndicator("Category 1");
    cat1spec.setContent(R.id.tab1c);

    TabSpec cat2spec = tabHost.newTabSpec("Category 2");
    cat2spec.setIndicator("Category 2");
    cat2spec.setContent(R.id.tab2c);
    TabSpec cat3spec = tabHost.newTabSpec("Favourites");
    cat3spec.setIndicator("Favourites");
    cat3spec.setContent(R.id.tab3c);

    tabHost.addTab(cat1spec);
    tabHost.addTab(cat2spec);
    tabHost.addTab(cat3spec);

    getActionBar().setHomeButtonEnabled(true);

    tabHost.setOnTabChangedListener(this);
}

@Override
public void onTabChanged(String tabId) {
    //Something will happends here
}
SweSnow
  • 17,504
  • 10
  • 36
  • 49

1 Answers1

0

The part of code which you have posted looks clean and I couldnt find anything from the error messages you posted. May be few more lines of code from your project could be bit helpful in finding the problem.

Please take a look at the below code, check whether you follow any of the pattern.

TabHost tabs = (TabHost) body_view.findViewById(R.id.media_tabs);
    tabs.setup();

    tabs.addTab(tabs.newTabSpec(ALBUMS_TAB));
    tabs.addTab(tabs.newTabSpec(ARTISTS_TAB));


tabs.setOnTabChangedListener(new OnTabChangeListener() {
      @Override
      public void onTabChanged(String tabId) {
        if (tabId.equals(ARTISTS_TAB)) {
          artistsFlipper.setDisplayedChild(0);
        } else if (tabId.equals(ALBUMS_TAB)) {
          albumsFlipper.setDisplayedChild(0);
        }
      }
 });

or

public class MyTabActivity extends TabActivity implements OnTabChangeListener {

    private TabHost tabHost;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /* Your onCreate code here */

        tabHost.setOnTabChangedListener(this);
    }

    /* ... */

    @Override
    public void onTabChanged(String tabId) {
        /* Your code to handle tab changes */
    }
}

The above solution is from here

Community
  • 1
  • 1
likeToCode
  • 852
  • 2
  • 10
  • 20
  • Please take a look at the below link, the example codes or implementation are very clear. http://stackoverflow.com/questions/8291191/how-to-use-ontabchangelistener – likeToCode Aug 13 '12 at 09:58
  • Do you have "implements OnTabChangeListener" clause in your class. I guess almost everything is same in your code with respect to the usual syntax. – likeToCode Aug 13 '12 at 10:04
  • I have implemented OnTabChangeListner now and it's giving me an error saying OnTabChangeListner is an unknown entity. – SweSnow Aug 13 '12 at 10:28
  • Actually the TabActivity class is deprecated and I did a quick implementation using the below link. I didnt get any error in the code and Iäm able to lauch the application in the emulator. http://developer.android.com/reference/android/app/TabActivity.html – likeToCode Aug 13 '12 at 12:13
  • Also this link would be helpful, http://www.vogella.com/articles/Android/article.html#fragments_tutorial – likeToCode Aug 13 '12 at 12:22
  • Apologis for providing many links, but this is the best one of all those http://android.codeandmagic.org/2011/07/android-tabs-with-fragments/ – likeToCode Aug 13 '12 at 12:44