• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 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.actionbarcompat;
18 
19 import android.os.Bundle;
20 import android.view.Menu;
21 import android.view.MenuInflater;
22 import android.view.MenuItem;
23 import android.view.View;
24 import android.widget.Toast;
25 
26 public class MainActivity extends ActionBarActivity {
27     private boolean mAlternateTitle = false;
28 
onCreate(Bundle savedInstanceState)29     public void onCreate(Bundle savedInstanceState) {
30         super.onCreate(savedInstanceState);
31         setContentView(R.layout.main);
32 
33         findViewById(R.id.toggle_title).setOnClickListener(new View.OnClickListener() {
34             @Override
35             public void onClick(View view) {
36                 if (mAlternateTitle) {
37                     setTitle(R.string.app_name);
38                 } else {
39                     setTitle(R.string.alternate_title);
40                 }
41                 mAlternateTitle = !mAlternateTitle;
42             }
43         });
44     }
45 
46     @Override
onCreateOptionsMenu(Menu menu)47     public boolean onCreateOptionsMenu(Menu menu) {
48         MenuInflater menuInflater = getMenuInflater();
49         menuInflater.inflate(R.menu.main, menu);
50 
51         // Calling super after populating the menu is necessary here to ensure that the
52         // action bar helpers have a chance to handle this event.
53         return super.onCreateOptionsMenu(menu);
54     }
55 
56     @Override
onOptionsItemSelected(MenuItem item)57     public boolean onOptionsItemSelected(MenuItem item) {
58         switch (item.getItemId()) {
59             case android.R.id.home:
60                 Toast.makeText(this, "Tapped home", Toast.LENGTH_SHORT).show();
61                 break;
62 
63             case R.id.menu_refresh:
64                 Toast.makeText(this, "Fake refreshing...", Toast.LENGTH_SHORT).show();
65                 getActionBarHelper().setRefreshActionItemState(true);
66                 getWindow().getDecorView().postDelayed(
67                         new Runnable() {
68                             @Override
69                             public void run() {
70                                 getActionBarHelper().setRefreshActionItemState(false);
71                             }
72                         }, 1000);
73                 break;
74 
75             case R.id.menu_search:
76                 Toast.makeText(this, "Tapped search", Toast.LENGTH_SHORT).show();
77                 break;
78 
79             case R.id.menu_share:
80                 Toast.makeText(this, "Tapped share", Toast.LENGTH_SHORT).show();
81                 break;
82         }
83         return super.onOptionsItemSelected(item);
84     }
85 }
86