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