0

I'm trying to create a custom TabHost. And I want to call the setTabSelectionListener() method of TabWidget.

But I have the following error:

TabWidget.OnTabSelectionChanged cannot be resolved to a type

Here is my test code:

public class CustomTabHost {
    public CustomTabHost(Activity activity) {       
        TabWidget tab_widget = (TabWidget) activity.findViewById(R.id.tab_widget);
        tab_widget.setTabSelectionListener(new TabWidget.OnTabSelectionChanged() {
            public void onTabSelectionChanged(int tabIndex, boolean clicked) {

            }
        });
    }
}

I'm using Eclipse and Android 2.2.

Thanks in advance!

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
Mathieu Mahé
  • 2,647
  • 3
  • 35
  • 50
  • I think there is no listener `setTabSelectionListener` associate with `TabWidget` see http://developer.android.com/reference/android/widget/TabWidget.html – Mohsin Naeem Jul 30 '12 at 15:57
  • the `setTabSelectionListener` method exists on the TabWidget (it's not the compiler error). It's a package private method. – Mathieu Mahé Jul 30 '12 at 16:06
  • It is your own written method? – Mohsin Naeem Jul 30 '12 at 16:10
  • No it's not : http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.6_r2/android/widget/TabWidget.java#TabWidget.OnTabSelectionChanged – Mathieu Mahé Jul 30 '12 at 16:12

1 Answers1

0

Instead of using the setTabSelectionListener why dont you use the .OnchangeTabListener as shown : How to use TabHost.OnTabChangeListener in android?

    public class ListFragment extends Fragment implements OnTabChangeListener{

View v;
TabHost tabHost;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

     v =  inflater.inflate(R.layout.main_menu_libraries, container, false);

      tabHost=(TabHost) v.findViewById(R.id.tabHost);
     tabHost.setup();

     TabSpec spec1=tabHost.newTabSpec("Tab 1");
     spec1.setContent(R.id.tab1);
     spec1.setIndicator("About",getResources().getDrawable(R.drawable.android));

     TabSpec spec2=tabHost.newTabSpec("Tab 2");
     spec2.setContent(R.id.tab2);
     spec2.setIndicator("Blog",getResources().getDrawable(R.drawable.android));

     tabHost.addTab(spec1);
     tabHost.addTab(spec2);

     tabHost.setOnTabChangedListener(this);


     return v;
}

@Override
public void onTabChanged(String arg0) {

    if (tabHost.getCurrentTab()==0)
            {
        //your code here

            }

           if (tabHost.getCurrentTab()==1)
           {
        //your code here
            }

Also in the part where it says //your code here ... try calling a new class which extends Fragment and inflate the particular fragment with the menu !

Community
  • 1
  • 1
Android2390
  • 1,150
  • 12
  • 21
  • I need to override the default behavior of a tabHost : when I click on the last tab, I have to display a lateral menu (like the menu of facebook), and not changing of activity! – Mathieu Mahé Jul 30 '12 at 16:04
  • I dont think there is an option setTabSelectionListener for TabWidgets. The TabHost.OnTabChangeListener works the same way ,on the change of a tab you can inflate the particular page/fragment with whatever view you want to fill it with – Android2390 Jul 30 '12 at 16:10
  • The problem is I can't create a TabHost because there is no activity for this last tab. The last tab is, in fact, only a button to show the menu. But the design integrated it in the TabWidget of the TabHost – Mathieu Mahé Jul 30 '12 at 16:16
  • I have created a TabHost without making it an activity as well..It extends The Fragment class and implements the OnTabChangeListener. I get your point that on the click of the last button you want to display a menu. So why don't you try creating a fragment on the click of that tab and then inflating the fragment with the menu you want. Yes the TabHost is integrated within the TabWidget in the xml file. Sorry if i can't help you much :) – Android2390 Jul 30 '12 at 16:21
  • I just start Android so I'm not at ease with fragments yet. But I'll try what you just said. Thank you! – Mathieu Mahé Jul 30 '12 at 16:25
  • i've edited my answer,maybe it will be of some help to you. Best of luck ! – Android2390 Jul 30 '12 at 16:32