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