Skip to content Skip to sidebar Skip to footer

How Can I Add Items To My Actionbar?

I've tried searching around but I am only limited to a very minimal amount of internet usage till 16th so I was looking around on how I could implement buttons to my action bar. Ri

Solution 1:

This is an example how you can add HELP icon to your action bar

Create a menu XML inside your menu folder like the following

<item
    android:id="@+id/help_menu_item"
    android:icon="@android:drawable/ic_menu_help"
    android:title="Help"
    android:showAsAction="ifRoom" /> 

And in your activity do something like that

@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
    MenuInflaterinflater= getMenuInflater();
    inflater.inflate(R.menu.help_menu, menu);
    returntrue;
}
@OverridepublicbooleanonOptionsItemSelected(MenuItem item) {
    // Handle item selectionswitch (item.getItemId()) {
        case R.id.help_menu_item:
            //do your menu press here       returntrue;

        default:
            returnsuper.onOptionsItemSelected(item);
    }
}

Solution 2:

ActionBarmActionBar= getSupportActionBar();
    mActionBar.setDisplayShowTitleEnabled(false);
    mActionBar.setDisplayUseLogoEnabled(false);
    mActionBar.setDisplayHomeAsUpEnabled(false);
    mActionBar.setDisplayShowCustomEnabled(true);
    mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    mActionBar.setCustomView(R.layout.titlebar);

    homeButton = (ImageView) mActionBar.getCustomView().findViewById(R.id.titlebar_iv_home);
    menuButton = (ImageView) mActionBar.getCustomView().findViewById(R.id.titlebar_iv_menu);
    titlebar_title = (TitleTextView)mActionBar.getCustomView().findViewById(R.id.titlebar_title);
    titlebar_title.setText("TITLE");

you can give click events for the buttons also.Here i am taking one custom layout added to action bar. so create custom layout in your res/layout folder. and give appropriate id's to them.and give click events hope this helps

Post a Comment for "How Can I Add Items To My Actionbar?"