1
/*
2
* Copyright (C) 2013 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.actionbarcompat.basic;
18
19
import android.os.Bundle;
20
import android.support.v4.view.MenuItemCompat;
21
import android.support.v7.app.ActionBarActivity;
22
import android.view.Menu;
23
import android.view.MenuItem;
24
25
/**
26
* This sample shows you how to use ActionBarCompat to create a basic Activity which displays
27
* action items. It covers inflating items from a menu resource, as well as adding an item in code.
28
*
29
* This Activity extends from {@link ActionBarActivity}, which provides all of the function
30
* necessary to display a compatible Action Bar on devices running Android v2.1+.
31
*/
32
public class MainActivity extends ActionBarActivity {
33
34
@Override
35
protected void onCreate(Bundle savedInstanceState) {
36
super.onCreate(savedInstanceState);
37
setContentView(R.layout.sample_main);
38
}
39
41
/**
42
* Use this method to instantiate your menu, and add your items to it. You
43
* should return true if you have added items to it and want the menu to be displayed.
44
*/
45
@Override
46
public boolean onCreateOptionsMenu(Menu menu) {
47
// Inflate our menu from the resources by using the menu inflater.
48
getMenuInflater().inflate(R.menu.main, menu);
49
50
// It is also possible add items here. Use a generated id from
51
// resources (ids.xml) to ensure that all menu ids are distinct.
52
MenuItem locationItem = menu.add(0, R.id.menu_location, 0, R.string.menu_location);
53
locationItem.setIcon(R.drawable.ic_action_location);
54
55
// Need to use MenuItemCompat methods to call any action item related methods
56
MenuItemCompat.setShowAsAction(locationItem, MenuItem.SHOW_AS_ACTION_IF_ROOM);
57
58
return true;
59
}
61
63
/**
64
* This method is called when one of the menu items to selected. These items
65
* can be on the Action Bar, the overflow menu, or the standard options menu. You
66
* should return true if you handle the selection.
67
*/
68
@Override
69
public boolean onOptionsItemSelected(MenuItem item) {
70
switch (item.getItemId()) {
71
case R.id.menu_refresh:
72
// Here we might start a background refresh task
73
return true;
74
75
case R.id.menu_location:
76
// Here we might call LocationManager.requestLocationUpdates()
77
return true;
78
79
case R.id.menu_settings:
80
// Here we would open up our settings activity
81
return true;
82
}
83
84
return super.onOptionsItemSelected(item);
85
}
87
}