Zookey
Zookey

Reputation: 2707

Open new Fragment (Tab) on Button click

I am using ViewPager with ActionBar Tabs and I easily can swipe between tabs(fragments), and I also can open another tab onClick of that tab.

But, I want to on button click from one tab to open another tab(fragment).

Take a look on the picture below. enter image description here

Upvotes: 1

Views: 2523

Answers (2)

Simulant
Simulant

Reputation: 20102

After creating your Tab, store the tab in a variable so that it is accessable in your Buttons onClick method. Then call the ActionBar.Tab.select() method inside your onClick method of your button.

Use something like this: (I could not check everything, but it should give you the idea)

ActionBar actionBar = getActivity().getActionBar();
final Tab tab = actionBar.newTab();
Button button = new Button(getActivity());
button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        tab.select();   
    }
}

Upvotes: 1

Zookey
Zookey

Reputation: 2707

Here is how I solved this problem:

private ActionBar actionBar;

Then I put this in onCreate()

actionBar = getActivity().getActionBar();

And then I created button on click listener like this:

btnLocationReviews.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                actionBar.setSelectedNavigationItem(3);


            }

Upvotes: 1

Related Questions