Vinny K
Vinny K

Reputation: 194

Use One Common Button across Fragments with ActionBarSherlock

I used ActionBarSherlock to get some Holo Themed Tabs and an ActionBar on my app and created a Fragment to handle the behavior on each tab. I wanted the tabs and a button along the bottom to "sandwich" the fragments do at the bottom of the screen there would be a button that would have one click listener across both fragments.

In my Activity I created the tabs like this.

public class InviteFriendsActivity extends SherlockFragmentActivity implements ActionBar.TabListener
{
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);

    ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab tab1 = bar.newTab();
    tab1.setText("Tab 1");
    tab1.setTabListener(this);  

    ActionBar.Tab tab2 = bar.newTab();
    tab2.setText("Tab 2");
    tab2.setTabListener(this);  

    bar.addTab(tab1);
    bar.addTab(tab2);
  }
}

And then I created the onTabSelected

 public void onTabSelected(Tab tab, FragmentTransaction ft)
{
    if (tab.getPosition() == 0)
    {
        Fragment1 frag = new Fragment1();
        ft.replace(android.R.id.content, frag);
    }
    else if (tab.getPosition() == 1)
    {
        Fragment2 frag = new Fragment2();
        ft.replace(android.R.id.content, frag);
    }
}

I have no problem getting the tabs to display or change, but I can't seem to figure out how to get a button the would line the bottom of the screen and stay static while inside this activity and still allow me to switch between the two fragments.

Upvotes: 1

Views: 891

Answers (1)

Leandros
Leandros

Reputation: 16825

You want a button which is displayed through every fragment and each tab?
This can easily done by using a fragmentcontainer which displays your fragments. For example use a layout like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        android:name="com.example.yourfragmentcontainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:text="Button" />

</RelativeLayout>

For help how to setup a ActonBar using a fragmentcontainer take a look at this tutorial: http://arvid-g.de/12/android-4-actionbar-with-tabs-example

Upvotes: 2

Related Questions