1/*
2 * Copyright (C) 2014 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 */
16package com.example.android.supportv7.app;
17
18import android.app.SearchManager;
19import android.os.Bundle;
20import android.view.Menu;
21import android.view.MenuInflater;
22import android.view.MenuItem;
23import android.widget.Toast;
24
25import androidx.appcompat.app.AppCompatActivity;
26import androidx.appcompat.widget.SearchView;
27import androidx.appcompat.widget.Toolbar;
28
29import com.example.android.supportv7.R;
30
31/**
32 * This demonstrates idiomatic usage of the Toolbar as the action bar.
33 */
34public class ToolbarUsage extends AppCompatActivity {
35
36    @Override
37    protected void onCreate(Bundle savedInstanceState) {
38        super.onCreate(savedInstanceState);
39        setContentView(R.layout.toolbar_usage);
40
41        // Retrieve the Toolbar from our content view, and set it as the action bar
42        Toolbar toolbar = findViewById(R.id.toolbar);
43        setSupportActionBar(toolbar);
44    }
45
46    @Override
47    public boolean onCreateOptionsMenu(Menu menu) {
48        MenuInflater inflater = getMenuInflater();
49        inflater.inflate(R.menu.actions, menu);
50
51        // Retrieve the SearchView and plug it into SearchManager
52        final SearchView searchView =
53                (SearchView) menu.findItem(R.id.action_search).getActionView();
54
55        SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
56        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
57
58        return true;
59    }
60
61    @Override
62    public boolean onOptionsItemSelected(MenuItem item) {
63        Toast.makeText(this, "Selected Item: " + item.getTitle(), Toast.LENGTH_SHORT).show();
64        return true;
65    }
66
67}
68