Sunday, 20 January 2013

Action Bar Sherlock : Creating Action Menu Items in ICS Action Bar in Android

Add ActionBarSherlock Library Project to your Application Project, Here is a Link for this

MainActivity.java

package com.rajeshvijayakumar.actionmenuitem;

import android.os.Bundle;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Menu;

public class MainActivity extends SherlockActivity {

    private MenuItem mGoItem;
    private MenuItem mClearItem;
   
    private static final int GO_ITEM_ID = 1;
    private static final int CLEAR_ITEM_ID = 2;

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        mGoItem = menu.add(0, GO_ITEM_ID, 0, null);
        mGoItem.setIcon(R.drawable.abs__ic_go).setShowAsAction(
                MenuItem.SHOW_AS_ACTION_ALWAYS);
        mClearItem = menu.add(0, CLEAR_ITEM_ID, 0, null);
        mClearItem.setIcon(R.drawable.abs__ic_clear).setShowAsAction(
                MenuItem.SHOW_AS_ACTION_ALWAYS);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch(item.getItemId()) {
        case GO_ITEM_ID :
            Toast.makeText(MainActivity.this, "You have Pressed 'Go' Menu Item", Toast.LENGTH_LONG).show();
            return true;
        case CLEAR_ITEM_ID :
            Toast.makeText(MainActivity.this, "You have Pressed 'Clear' Menu Item", Toast.LENGTH_LONG).show();
            return true;
        }
       
        return false;
    }   
}

Output :



Source Code : Download this Example with Library Here

No comments:

Post a Comment