0

How to detect onClick event on the tab when i extend SherlockFragmentActivity


  • I mean say i click Rating Tab .... Log msg Rating tab is clicked = 1
  • when i click rating tab again .... Log msg Rating tab is clicked = 2

Note:: I checked several solutions on stackoverflow but none of them answer when i use sherlockFragmentActivity


MainActivitySherlock.java

public class MainActivitySherlock extends SherlockFragmentActivity {
    // Declare Variables
    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set the view from main_sorting.xml
        setContentView(R.layout.main_sorting);

        // Locate android.R.id.tabhost in main_sorting.xml
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);

        // Create the tabs in main_sorting.xml
        mTabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent);

        // Create Rating 
        mTabHost.addTab(mTabHost.newTabSpec("rating").setIndicator("Rating"), 
                FragmentRating.class, null);

        // Create Price
        mTabHost.addTab(mTabHost.newTabSpec("Price").setIndicator("Price"),
                FragmentPrice.class, null);

        // Create Distance
        mTabHost.addTab(mTabHost.newTabSpec("Distance").setIndicator("Distance"),
                FragmentDistance.class, null);

    }

    //Split ActionBar Buttons
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getSupportMenuInflater();
        menuInflater.inflate(R.menu.actionbar_sort_menu, menu);

        return super.onCreateOptionsMenu(menu);
    }
}

How to achieve this !

Devrath
  • 42,072
  • 54
  • 195
  • 297
  • I think `TabHost.setOnTabChangedListener` is what you want. Examples : http://stackoverflow.com/questions/1062476/onclicklistener-on-tabs-not-working – Ye Lin Aung Apr 19 '14 at 12:01
  • @ Ye Lin Aung ..... Your link helped http://stackoverflow.com/a/2204533/1083093 .... using this i am able to detect the onclick events, thanks :) – Devrath Apr 19 '14 at 12:09
  • 1
    I shall post it as an answer and can you please accept it ? so, the rest can reference it in the future. – Ye Lin Aung Apr 19 '14 at 12:14

1 Answers1

1

You can try by setting setOnTabChangedListener to your TabHost. In your case,

mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
   @Override
   public void onTabChanged(String arg0) {
       Log.i("Tab number", "" + tabHost.getCurrentTab());
   });
}
Ye Lin Aung
  • 11,234
  • 8
  • 45
  • 51