[android] Custom ActionBar with ActionMode and Custom View

[android] Custom ActionBar with ActionMode

 

 

기본 모드

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

 

 

 

custom view가 추가된 action mode

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

 

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:paddingRight="10dp" >

  <CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Selete all" />

  <TextView
    android:id="@+id/textView1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="right|center_vertical"
    android:text="not selected" />

  <ImageButton
    android:id="@+id/btn_delete_all"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/ic_menu_delete" />

</LinearLayout>

 

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");
      
      View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.view_delete_mode, null);
      final TextView text1 = (TextView) view.findViewById(R.id.textView1);
      CheckBox check1 = (CheckBox) view.findViewById(R.id.checkBox1);
      check1.setOnCheckedChangeListener(new OnCheckedChangeListener()
      {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
          text1.setText(isChecked ? "selected all" : "not selected");
        }
      });
      ImageButton btn = (ImageButton) view.findViewById(R.id.btn_delete_all);
      btn.setOnClickListener(new OnClickListener()
      {
        @Override
        public void onClick(View v)
        {
          deleteAll();
        }
      });
      
      _actionMode.setCustomView(view);
    }
    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;
    }
  }
}

 

 

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