• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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 com.example.android.supportv7.R;
20 
21 import android.content.Context;
22 import android.content.Intent;
23 import android.provider.Settings;
24 import android.support.v4.view.ActionProvider;
25 import android.support.v7.app.AppCompatActivity;
26 import android.view.LayoutInflater;
27 import android.view.Menu;
28 import android.view.MenuItem;
29 import android.view.View;
30 import android.widget.ImageButton;
31 import android.widget.Toast;
32 
33 /**
34  * This activity demonstrates how to implement an {@link android.view.ActionProvider}
35  * for adding functionality to the Action Bar. In particular this demo creates an
36  * ActionProvider for launching the system settings and adds a menu item with that
37  * provider.
38  */
39 public class ActionBarSettingsActionProviderActivity extends AppCompatActivity {
40     @Override
onCreateOptionsMenu(Menu menu)41     public boolean onCreateOptionsMenu(Menu menu) {
42         super.onCreateOptionsMenu(menu);
43         getMenuInflater().inflate(R.menu.action_bar_settings_action_provider, menu);
44         return true;
45     }
46 
47     @Override
onOptionsItemSelected(MenuItem item)48     public boolean onOptionsItemSelected(MenuItem item) {
49         // If this callback does not handle the item click, onPerformDefaultAction
50         // of the ActionProvider is invoked. Hence, the provider encapsulates the
51         // complete functionality of the menu item.
52         Toast.makeText(this, R.string.action_bar_settings_action_provider_no_handling,
53                 Toast.LENGTH_SHORT).show();
54         return false;
55     }
56 
57     public static class SettingsActionProvider extends ActionProvider {
58         /** An intent for launching the system settings. */
59         private static final Intent sSettingsIntent = new Intent(Settings.ACTION_SETTINGS);
60 
61         /**
62          * Creates a new instance.
63          *
64          * @param context Context for accessing resources.
65          */
SettingsActionProvider(Context context)66         public SettingsActionProvider(Context context) {
67             super(context);
68         }
69 
70         @Override
onCreateActionView()71         public View onCreateActionView() {
72             // Inflate the action view to be shown on the action bar.
73             LayoutInflater layoutInflater = LayoutInflater.from(getContext());
74             View view = layoutInflater.inflate(R.layout.action_bar_settings_action_provider, null);
75             ImageButton button = (ImageButton) view.findViewById(R.id.button);
76             // Attach a click listener for launching the system settings.
77             button.setOnClickListener(new View.OnClickListener() {
78                 @Override
79                 public void onClick(View v) {
80                     getContext().startActivity(sSettingsIntent);
81                 }
82             });
83             return view;
84         }
85 
86         @Override
onPerformDefaultAction()87         public boolean onPerformDefaultAction() {
88             // This is called if the host menu item placed in the overflow menu of the
89             // action bar is clicked and the host activity did not handle the click.
90             getContext().startActivity(sSettingsIntent);
91             return true;
92         }
93     }
94 }
95