[android] Custom ActionBar with ActionMode

보통 상태

스크린샷 2014-06-26 오후 4.22.58

 

 

 

 

액션모드 상태

스크린샷 2014-06-26 오후 4.23.13

 

 

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="kr.susemi99.customactionbar.MainActivity" >

  <item
    android:id="@+id/action_delete"
    android:showAsAction="always"
    android:title="Delete"/>
  <item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>

</menu>

 

public class MainActivity extends Activity
{
  private enum ACTION_MODE
  {
    NORMAL, DELETE
  }
  
  private ActionMode _actionMode;
  private ACTION_MODE _currentMode = ACTION_MODE.NORMAL;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    Button btn1 = (Button) findViewById(R.id.button1);
    btn1.setOnClickListener(new OnClickListener()
    {
      @Override
      public void onClick(View v)
      {
        toggleDeleteMode();
      }
    });
  }
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu)
  {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }
  
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item)
  {
    if (item.getItemId() == R.id.action_delete)
      toggleDeleteMode();
    return super.onOptionsItemSelected(item);
  }
  
  
  private void toggleDeleteMode()
  {
    if (_currentMode == ACTION_MODE.NORMAL)
    {
      _currentMode = ACTION_MODE.DELETE;
      _actionMode = startActionMode(new ActionModeCallback());
      _actionMode.setTitle("xx selected");
    }
    else
    {
      _actionMode.finish();
    }
  }
  
  
  private void deleteAll()
  {
    // TODO delete all
    Log.i("MainActivity.java | deleteAll", "|" + "delete all" + "|");
    toggleDeleteMode();
  }
  
  /**************************************************
   * action mode
   ***************************************************/
  private class ActionModeCallback implements ActionMode.Callback
  {
    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu)
    {
      mode.getMenuInflater().inflate(R.menu.main_delete_mode, menu);
      return true; // important!
    }
    
    
    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu)
    {
      return false;
    }
    
    
    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item)
    {
      if (item.getItemId() == R.id.delete)
        deleteAll();
      
      return false;
    }
    
    
    @Override
    public void onDestroyActionMode(ActionMode mode)
    {
      Log.i("MainActivity.java | onDestroyActionMode", "|" + "action mode finish" + "|");
      _actionMode = null;
      _currentMode = ACTION_MODE.NORMAL;
    }
  }
}

 

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="kr.susemi99.customactionbar.MainActivity" >

  <item
    android:id="@+id/delete"
    android:icon="@android:drawable/ic_menu_delete"
    android:showAsAction="always"
    android:title=""/>

</menu>

 

source : https://github.com/susemi99/CustomActionbar