Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.supportv7.app;
     18 
     19 import android.os.Bundle;
     20 import android.view.Menu;
     21 import android.view.MenuItem;
     22 import android.view.View;
     23 import android.widget.Toast;
     24 
     25 import androidx.appcompat.app.AppCompatActivity;
     26 import androidx.appcompat.view.ActionMode;
     27 import androidx.appcompat.widget.Toolbar;
     28 
     29 import com.example.android.supportv7.R;
     30 
     31 /**
     32  * This demonstrates idiomatic usage of an action mode with a Toolbar.
     33  */
     34 public class ToolbarActionMode extends AppCompatActivity {
     35 
     36     @Override
     37     protected void onCreate(Bundle savedInstanceState) {
     38         super.onCreate(savedInstanceState);
     39         setContentView(R.layout.toolbar_action_mode);
     40 
     41         Toolbar toolbar = findViewById(R.id.toolbar);
     42         setSupportActionBar(toolbar);
     43 
     44         findViewById(R.id.btn_start_action_mode).setOnClickListener(new View.OnClickListener() {
     45             @Override
     46             public void onClick(View view) {
     47                 startActionMode();
     48             }
     49         });
     50     }
     51 
     52     private void startActionMode() {
     53         startSupportActionMode(new ActionMode.Callback() {
     54             @Override
     55             public boolean onCreateActionMode(ActionMode mode, Menu menu) {
     56                 mode.getMenuInflater().inflate(R.menu.actions, menu);
     57                 return true;
     58             }
     59 
     60             @Override
     61             public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
     62                 return false;
     63             }
     64 
     65             @Override
     66             public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
     67                 Toast.makeText(ToolbarActionMode.this,
     68                         "Action Mode item clicked:" + item.getTitle(), Toast.LENGTH_SHORT).show();
     69                 return true;
     70             }
     71 
     72             @Override
     73             public void onDestroyActionMode(ActionMode mode) {
     74             }
     75         });
     76     }
     77 
     78 }
     79