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
17package com.example.android.supportv7.app;
18
19import com.example.android.supportv7.R;
20
21import android.os.Bundle;
22import android.support.v7.app.AppCompatActivity;
23import android.support.v7.view.ActionMode;
24import android.support.v7.widget.Toolbar;
25import android.view.Menu;
26import android.view.MenuItem;
27import android.view.View;
28import android.widget.Toast;
29
30/**
31 * This demonstrates idiomatic usage of an action mode with a Toolbar.
32 */
33public class ToolbarActionMode extends AppCompatActivity {
34
35    @Override
36    protected void onCreate(Bundle savedInstanceState) {
37        super.onCreate(savedInstanceState);
38        setContentView(R.layout.toolbar_action_mode);
39
40        Toolbar toolbar = findViewById(R.id.toolbar);
41        setSupportActionBar(toolbar);
42
43        findViewById(R.id.btn_start_action_mode).setOnClickListener(new View.OnClickListener() {
44            @Override
45            public void onClick(View view) {
46                startActionMode();
47            }
48        });
49    }
50
51    private void startActionMode() {
52        startSupportActionMode(new ActionMode.Callback() {
53            @Override
54            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
55                mode.getMenuInflater().inflate(R.menu.actions, menu);
56                return true;
57            }
58
59            @Override
60            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
61                return false;
62            }
63
64            @Override
65            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
66                Toast.makeText(ToolbarActionMode.this,
67                        "Action Mode item clicked:" + item.getTitle(), Toast.LENGTH_SHORT).show();
68                return true;
69            }
70
71            @Override
72            public void onDestroyActionMode(ActionMode mode) {
73            }
74        });
75    }
76
77}
78