0

I just took the sample code shared by developer android site. I wanted to create a tabbed menu along with the option menu in the action bar. I want my activity to have the navigation tabs along with the action bar menu. I am not getting the action bar menu. But i have seen the applications which have navigation tabs as well as the action bar menu items as well. Please if someone helps me that would be of great help.

MainActivity Class

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
AppSectionsPagerAdapter mAppSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will display the three primary sections of the app, one at a
 * time.
 */
ViewPager mViewPager;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Create the adapter that will return a fragment for each of the three primary sections
    // of the app.
    mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

    // Set up the action bar.
    final ActionBar actionBar = getActionBar();

    // Specify that the Home/Up button should not be enabled, since there is no hierarchical
    // parent.
    actionBar.setHomeButtonEnabled(true);

    // Specify that we will be displaying tabs in the action bar.
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    // Set up the ViewPager, attaching the adapter and setting up a listener for when the
    // user swipes between sections.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mAppSectionsPagerAdapter);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            // When swiping between different app sections, select the corresponding tab.
            // We can also use ActionBar.Tab#select() to do this if we have a reference to the
            // Tab.
            actionBar.setSelectedNavigationItem(position);
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by the adapter.
        // Also specify this Activity object, which implements the TabListener interface, as the
        // listener for when this tab is selected.
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mAppSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {


    getMenuInflater().inflate(R.menu.template_actvity, menu);
    Toast.makeText(getApplicationContext(), "Indie onCreateOptionsMenu", Toast.LENGTH_SHORT).show();

    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    return super.onOptionsItemSelected(item);
}

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

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

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

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
 * sections of the app.
 */
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

    public AppSectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        switch (i) {
            case 0:
                // The first section of the app is the most interesting -- it offers
                // a launchpad into the other demonstrations in this example application.
                return new LaunchpadSectionFragment();

            default:
                // The other sections of the app are dummy placeholders.
                Fragment fragment = new DummySectionFragment();
                Bundle args = new Bundle();
                args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
                fragment.setArguments(args);
                return fragment;
        }
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "Section " + (position + 1);
    }
}

/**
 * A fragment that launches other parts of the demo application.
 */
public static class LaunchpadSectionFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_section_launchpad, container, false);

        // Demonstration of a collection-browsing activity.
        rootView.findViewById(R.id.demo_collection_button)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(getActivity(), CollectionDemoActivity.class);
                        startActivity(intent);
                    }
                });

        // Demonstration of navigating to external activities.
        rootView.findViewById(R.id.demo_external_activity)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        // Create an intent that asks the user to pick a photo, but using
                        // FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET, ensures that relaunching
                        // the application from the device home screen does not return
                        // to the external activity.
                        Intent externalActivityIntent = new Intent(Intent.ACTION_PICK);
                        externalActivityIntent.setType("image/*");
                        externalActivityIntent.addFlags(
                                Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                        startActivity(externalActivityIntent);
                    }
                });

        return rootView;
    }
}

/**
 * A dummy fragment representing a section of the app, but that simply displays dummy text.
 */
public static class DummySectionFragment extends Fragment {

    public static final String ARG_SECTION_NUMBER = "section_number";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_section_dummy, container, false);
        Bundle args = getArguments();
        ((TextView) rootView.findViewById(android.R.id.text1)).setText(
                getString(R.string.dummy_section_text, args.getInt(ARG_SECTION_NUMBER)));
        return rootView;
    }
}

}

Activity_main XML File

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

template_activity Menu XML

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<item
    android:id="@+id/addTemplate"
    android:orderInCategory="100"
    android:icon="@drawable/ic_action_new"
    android:title="Add Template"
    app:showAsAction="always"/>

bond007
  • 2,434
  • 1
  • 17
  • 17
  • Do you have any menu options defined in the template_activity.xml file? – RogueBaneling Nov 18 '14 at 18:20
  • Yes Menu options are defined in the template_activity xml file and then it is inflated in the onCreateOptionsMenu. Even it is giving the Toast as well. Even on click of menu button it is showing the menu at bottom . But it is not displaying the option menu at the action Bar – bond007 Nov 19 '14 at 15:30

1 Answers1

0

Here are some things to try:

  1. I assume that in your template_activity.xml you remembered to include the </menu> tag? (You might have only missed copying+pasting it into your question).

  2. Try using this code in your activity:

    @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); getMenuInflater().inflate(R.menu.template_activity, menu); return true; }

Note: You may also have a possible typo in your getMenuInflater().inflate(R.menu.template_actvity, menu); line. Specifically, you misspelled "activity".

Edit: Try the following XML for your menu XML file:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity" >
  <item
      android:id="@+id/addTemplate"
      android:orderInCategory="1"
      android:icon="@drawable/ic_action_new"
      android:title="Add Template"
      android:showAsAction="always"/>
</menu>

As for getting the "three dot" overflow button, I realize now that you are using a device with a built-in menu button. It is intentional that devices with these don't include the overflow buttons. However, if you still want to implement this, you can read more about it here.

Community
  • 1
  • 1
RogueBaneling
  • 4,331
  • 4
  • 22
  • 33
  • I placed above shared code only save.onCreateOptionsMenu(Menu menu) was missing. but still no luck. – bond007 Nov 19 '14 at 16:22
  • I'm not entirely sure what you mean. Have you fixed the typo in "activity"? Also, note that it's `super` and not `save`. – RogueBaneling Nov 19 '14 at 16:25
  • Sorry for again a typo mistake it is super only. name of the xml file is actvity only so that's why it is not giving any error. You can refer to screenshot [1]: http://i.stack.imgur.com/judLr.png – bond007 Nov 19 '14 at 16:27
  • I've added some XML for you to try. – RogueBaneling Nov 19 '14 at 17:42
  • Thanks alot friend problem was with missing context i think as soon as making those changes in the code it resolved the issue. :) – bond007 Nov 19 '14 at 17:58