user1995307
user1995307

Reputation: 747

How to add content to a tab in Action Bar

I have some tabs in the Action Bar as in the code below. But I want to add contents like a few buttons and an EditText to each tab. I am not sure how to do this. Can anybody tell me where should I place this. I will be great full to you for any help. Thanks in advance.

public class MLkeyboardActivity extends FragmentActivity implements
    ActionBar.TabListener {

/**
 * The serialization (saved instance state) Bundle key representing the
 * current tab position.
 */
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mlkeyboard);

    // Set up the action bar to show tabs.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // For each of the sections in the app, add a tab to the action bar.
    actionBar.addTab(actionBar.newTab().setText(R.string.title_section1)
            .setTabListener(this));
    actionBar.addTab(actionBar.newTab().setText(R.string.title_section2)
            .setTabListener(this));
    actionBar.addTab(actionBar.newTab().setText(R.string.title_section3)
            .setTabListener(this));
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Restore the previously serialized current tab position.
    if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
        getActionBar().setSelectedNavigationItem(
                savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    // Serialize the current tab position.
    outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
            .getSelectedNavigationIndex());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_mlkeyboard, menu);
    return true;
}


@Override
public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, show the tab contents in the
    // container view.

    Fragment fragment = new DummySectionFragment();
    Bundle args = new Bundle();
    args.putInt(DummySectionFragment.ARG_SECTION_NUMBER,
            tab.getPosition() + 1);
    fragment.setArguments(args);
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.container, fragment).commit();
}

@Override
public void onTabUnselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

/**
 * A dummy fragment representing a section of the app, but that simply
 * displays dummy text.
 */
public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Create a new TextView and set its text to the fragment's section
        // number argument value.
        TextView textView = new TextView(getActivity());
        textView.setGravity(Gravity.CENTER);
        textView.setText(Integer.toString(getArguments().getInt(
                ARG_SECTION_NUMBER)));
        return textView;
    }
}

}

Upvotes: 0

Views: 3512

Answers (3)

user1995307
user1995307

Reputation: 747

This tutorial solved my problem. What we have to do is create number of fragments (say FragmentA,B,C )corresponding to each tab and call those fragments using each tabs.The code is avilable for download in the link below http://android-er.blogspot.in/2012/06/create-actionbar-in-tab-navigation-mode.html

public class AndroidNavigationTabsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    Tab tabA = actionBar.newTab();
    tabA.setText("Tab A");
    tabA.setTabListener(new TabListener<MyFragmentA>(this, "Tag A", MyFragmentA.class));
    actionBar.addTab(tabA);

    Tab tabB = actionBar.newTab();
    tabB.setText("Tab B");
    tabB.setTabListener(new TabListener<MyFragmentB>(this, "Tag B", MyFragmentB.class));
    actionBar.addTab(tabB);

    Tab tabC = actionBar.newTab();
    tabC.setText("Tab C");
    tabC.setTabListener(new TabListener<MyFragmentC>(this, "Tag C", MyFragmentC.class));
    actionBar.addTab(tabC);

    if (savedInstanceState != null) {
        int savedIndex = savedInstanceState.getInt("SAVED_INDEX");
        getActionBar().setSelectedNavigationItem(savedIndex);
    }

}

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006674

But I want to add contents like a few buttons and an EditText to each tab. I am not sure how to do this.

Put them in the fragment that you are loading in onTabSelected().

Or, modify onTabSelected() to do something else, instead of replacing a fragment, to affect your desired changes to your UI.

Upvotes: 1

lokoko
lokoko

Reputation: 5803

You could refer to this :

http://developer.android.com/reference/android/app/ActionBar.html

Also, you could create an OptionCommand and setAction on top of that.

Upvotes: 0

Related Questions