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 17package com.example.android.supportv7.app; 18 19import com.example.android.supportv7.R; 20 21import android.content.Context; 22import android.content.Intent; 23import android.provider.Settings; 24import android.support.v4.view.ActionProvider; 25import android.support.v7.app.AppCompatActivity; 26import android.view.LayoutInflater; 27import android.view.Menu; 28import android.view.MenuItem; 29import android.view.View; 30import android.widget.ImageButton; 31import 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 */ 39public class ActionBarSettingsActionProviderActivity extends AppCompatActivity { 40 @Override 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 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 */ 66 public SettingsActionProvider(Context context) { 67 super(context); 68 } 69 70 @Override 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 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