How to call tab Onclick and OnTabChange for same tab
Asked Answered
B

3

11

In my project, I have two tabs and a button. For two tabs,I have two activities and button calling different activity. the thing is I am showing result of button on first tab. i.e tab0 is active on tab0Event and on button click event too. And am able to change the tab events using tabHost.setOnTabChangedListener, but now what i further want is, say suppose i click on button so now button view is displaying(tab0 is active) but again if i click on tab0, tab0 activity should be displayed.

I tried many solutions for clicking on tab, one is

getTabWidget().getChildAt(getTabHost().getCurrentTab()).setOnClickListener
      (new View.OnClickListener() {

      @Override public void onClick(View v) {
      System.out.println(getTabHost().getCurrentTab());

      } });

But when i used this code with tabChnageListner, tab change not working and i got very unexpected results. Would you please suggest solution for my problem.

Thanks.

code that is working for tab changed is as: (working fine for tab change, need to add Tab Onclick in it)

public class TabLayoutUsingTabChangeEventActivity extends TabActivity {

        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);                
            final TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
            final TabHost.TabSpec sp1 = tabHost.newTabSpec("TAB1");
            TabHost.TabSpec sp2 = tabHost.newTabSpec("TAB2");

            //Creating First Tab
            Intent intent1 = new Intent(this, Tab1Activity.class);
            sp1.setIndicator("TAB1").setContent(intent1);
            tabHost.addTab(sp1);

            //Creating Second Tab 
            Intent intent2 = new Intent(this, Tab2Activity.class);
            sp2.setIndicator("TAB2").setContent(intent2);
            tabHost.addTab(sp2);               

            //Tab Changed Event
            tabHost.setOnTabChangedListener(new OnTabChangeListener(){
                 @Override
                 public void onTabChanged(String tabId) {
                     Log.i("TabId :", tabId);
                     if(tabId.equals("TAB2")){
                     Log.i("TAB1", "TAB1 Changed");
                     Intent intent1 = new Intent().setClass(getApplicationContext(), Tab1Activity.class);
                     sp1.setIndicator("TAB1").setContent(intent1);
                     tabHost.setCurrentTab(0);
                     }
                  }
            });

            Button addNewButton = (Button)findViewById(R.id.add_new_ticket_btn);
            addNewButton.setOnClickListener(new OnClickListener(){
                  @Override
                  public void onClick(View v) {
                     Intent in = new Intent().setClass(getApplicationContext(), AddNewTicketActivity.class);
                     sp1.setContent(in);
                     tabHost.setCurrentTab(0);
                     //startActivity(in);
                 }
            });               
      }
}
Bottali answered 29/11, 2011 at 13:7 Comment(1)
please update your requirement clearly. its not clear.Cantone
P
7

You can implement this listener:

    host.setOnTabChangedListener(new OnTabChangeListener()
    {
        @Override
        public void onTabChanged(String tabId)
        {
            if (getTabHost().getCurrentTabTag().equals("tab0"))
            {
                host.getCurrentTabView().setOnTouchListener(
                        new OnTouchListener()
                        {
                            @Override
                            public boolean onTouch(View v, MotionEvent event)
                            {
                                if (event.getAction() == MotionEvent.ACTION_DOWN)
                                {
                                    if (getTabHost().getCurrentTabTag().equals("tab0")
                                    {
                                        getTabHost().setCurrentTabByTag("tab1");
                                        getTabHost().setCurrentTabByTag("tab0");
                                    }
                                    return false;
                                }
                                return false;
                            }
                        });
            }
        }
    });

Also when you add tabs set tag for every tab:

host.addTab(host.newTabSpec("tab0"));
host.addTab(host.newTabSpec("tab1"));
Pullman answered 9/1, 2013 at 20:19 Comment(1)
Definitely this is the right answer!!. It works perfect and have no problems with the setOnClickListener and setOnTabChangedListener. Thanks for this post!!Fireproofing
T
2

You can used Fragment to layout

Teacart answered 10/1, 2013 at 3:25 Comment(0)
W
1

You cant. Views in TabWidget already have onClickListener and it is required fo normal workflow. If you remove it you will break TabWidget's logic.

When you set your onClickListener you remove previuos onClickListener and you break TabWidget logic.

Usually in such cases compound click listener is created (a click listener which handles click event and calls another click listener). But not in your case because there is no way to get old click listener because View doesn't have getOnClickListener method.

This is TabWidget source code. All related values are private... so we can't fix anithing from this side.

The only way to achieve your goal is a hack with Reflections because they allows to read private vars. So before set View's new onlick listener you should get old one (using Reflections), then create compound onClickListener which will do your event handling code and then call old onClickListener. Set compound click listener to the View.

Wrong answered 8/1, 2013 at 21:16 Comment(1)
I have one more idea. You can use custom views (MyCustomView) as TabWidget childs. Then you can make MyCustomView's setOnclickListener method work non standart way: do not remove old click listener but "summ up" all click listeners into one compound listener. So when your view get clicked it will call all click listeners have been set and not the last one.Wrong

© 2022 - 2024 — McMap. All rights reserved.