Wednesday, May 16, 2012

14. Tutorial: Using the contextual action mode

Add a EditText element your your "main.xml" layout file.
   
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/myView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

</LinearLayout>
  
Create a new menu XML resource with the file name "contextual.xml"
   
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/toast"
        android:title="Toast">
    </item>

</menu>
  
Change your Activity to the following.
   
package de.vogella.android.socialapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class OverviewActivity extends Activity {
 protected Object mActionMode;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  // Define the contextual action mode
  View view = findViewById(R.id.myView);
  view.setOnLongClickListener(new View.OnLongClickListener() {
   // Called when the user long-clicks on someView
   public boolean onLongClick(View view) {
    if (mActionMode != null) {
     return false;
    }

    // Start the CAB using the ActionMode.Callback defined above
    mActionMode = OverviewActivity.this
      .startActionMode(mActionModeCallback);
    view.setSelected(true);
    return true;
   }
  });
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.mainmenu, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  Toast.makeText(this, "Just a test", Toast.LENGTH_SHORT).show();
  return true;
 }

 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

  // Called when the action mode is created; startActionMode() was called
  public boolean onCreateActionMode(ActionMode mode, Menu menu) {
   // Inflate a menu resource providing context menu items
   MenuInflater inflater = mode.getMenuInflater();
   // Assumes that you have "contexual.xml" menu resources
   inflater.inflate(R.menu.contextual, menu);
   return true;
  }

  // Called each time the action mode is shown. Always called after
  // onCreateActionMode, but
  // may be called multiple times if the mode is invalidated.
  public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
   return false; // Return false if nothing is done
  }

  // Called when the user selects a contextual menu item
  public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
   switch (item.getItemId()) {
   case R.id.toast:
    Toast.makeText(OverviewActivity.this, "Selected menu",
      Toast.LENGTH_LONG).show();
    mode.finish(); // Action picked, so close the CAB
    return true;
   default:
    return false;
   }
  }

  // Called when the user exits the action mode
  public void onDestroyActionMode(ActionMode mode) {
   mActionMode = null;
  }
 };

}
  
If you run this example and long press the EditText widget, your contextual ActionBar is displayed.
Contextual ActionBar demonstrated

2 comments:

  1. hey thanks , but public
    boolean onCreateOptionsMenu(Menu menu) , is not in use !

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete