0

i have a problem with ActionBarCompat (from support library). I have no idea how I can add a few buttons on ActionBar where I drew black circle (on screenshot) together with menu in the left of the screen.

Please, I need help!

enter image description here

Code of menu in ActionBar in the left of the screen.

     private DrawerLayout mDrawerLayout;
    private ListView mDrawer;
    private ActionBarHelper mActionBar;
    private ActionBarDrawerToggle mDrawerToggle;

...

        linearLayout = (LinearLayout)findViewById(R.id.fragment_container);
        linearLayout.setId(LAYOUT_ID);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawer = (ListView) findViewById(R.id.left_drawer);
        mDrawerLayout.setDrawerListener(new DDrawerListener());
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

...
        fragments = new  Fragment[NUMBER_OF_TABS];
        mDrawer.setAdapter(new CustomAdapter(this));
        mDrawer.setOnItemClickListener(new DrawerItemClickListener());


        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab));
        actionBar.setDisplayHomeAsUpEnabled(true);


        mActionBar = createActionBarHelper();
        mActionBar.init();


        mDrawer.setBackgroundColor(Color.parseColor("#e5c391"));
        mDrawer.setCacheColorHint(Color.parseColor("#e5c391"));

        initArrays(this);

        mDrawerToggle = new ActionBarDrawerToggle
        (this, mDrawerLayout,R.drawable.ic_drawer,R.string.app_drawer_open, R.string.app_drawer_close);

        if (savedInstanceState == null)
        {
            addFragment(0);
        }

Welcome other ways too =) Thank you! Happy New Year!

2 Answers 2

2

From https://developer.android.com/training/basics/actionbar/adding-buttons.html:

Specify the Actions in XML

All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory.

Add an element for each item you want to include in the action bar. For example:

res/menu/main_activity_actions.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Search, should appear as action button -->
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          android:showAsAction="ifRoom" />
    <!-- Settings, should always be in the overflow -->
    <item android:id="@+id/action_settings"
          android:title="@string/action_settings"
          android:showAsAction="never" />
</menu>

Add the Actions to the ActionBar

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
}

Respond to Action Buttons

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_search:
            openSearch();
            return true;
        case R.id.action_settings:
            openSettings();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
2
  • Thank you, but this don't work. How can I implement Up Navigate Button and standart Action Buttons together?
    – Romanzi
    Commented Dec 31, 2013 at 17:42
  • This isn't in the scope of your question, however you can read through the document that either Rahin or myself posted. developer.android.com/training/basics/actionbar/… Commented Dec 31, 2013 at 17:51
1

Please refer to this note in the Android Developer portal.

Not the answer you're looking for? Browse other questions tagged or ask your own question.