Saturday, 2 October 2010

Android: Menu button click event & Options Menu



 1)First of all we should create our menu.xml(create new folder menu in res):
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/search"
android:title="search"
android:icon="@drawable/icon" />
<item android:id="@+id/deleteall"
android:title="delete all"
android:icon="@drawable/icon" />
</menu>

2) To handle event we need override:
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

3)And for determine what button is pressed:
 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.deleteall:Log.v("ttt", "You pressed the icon!");
                  break;
            case R.id.search:Log.v("ttt", "You pressed the text!");
                  break;
        }
        return true;
    }

1 comment:

  1. Option Menu in android studio
    http://themasterworld.com/android-menu-in-android/

    ReplyDelete