1
/*
2
* Copyright 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.donebar;
18
19
import android.app.ActionBar;
20
import android.app.Activity;
21
import android.os.Bundle;
22
import android.view.LayoutInflater;
23
import android.view.Menu;
24
import android.view.MenuItem;
25
import android.view.View;
26
27
/**
28
* A sample activity demonstrating the "done button" alternative action bar presentation. For a more
29
* detailed description see {@link R.string.done_button_description}.
30
*/
31
public class DoneButtonActivity extends Activity {
32
protected void onCreate(Bundle savedInstanceState) {
33
super.onCreate(savedInstanceState);
34
36
// Inflate a "Done" custom action bar view to serve as the "Up" affordance.
37
final LayoutInflater inflater = (LayoutInflater) getActionBar().getThemedContext()
38
.getSystemService(LAYOUT_INFLATER_SERVICE);
39
final View customActionBarView = inflater.inflate(
40
R.layout.actionbar_custom_view_done, null);
41
customActionBarView.findViewById(R.id.actionbar_done).setOnClickListener(
42
new View.OnClickListener() {
43
@Override
44
public void onClick(View v) {
45
// "Done"
46
finish();
47
}
48
});
49
50
// Show the custom action bar view and hide the normal Home icon and title.
51
final ActionBar actionBar = getActionBar();
52
actionBar.setDisplayOptions(
53
ActionBar.DISPLAY_SHOW_CUSTOM,
54
ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME
55
| ActionBar.DISPLAY_SHOW_TITLE);
56
actionBar.setCustomView(customActionBarView);
58
59
setContentView(R.layout.activity_done_button);
60
}
61
63
@Override
64
public boolean onCreateOptionsMenu(Menu menu) {
65
super.onCreateOptionsMenu(menu);
66
getMenuInflater().inflate(R.menu.cancel, menu);
67
return true;
68
}
69
70
71
@Override
72
public boolean onOptionsItemSelected(MenuItem item) {
73
switch (item.getItemId()) {
74
case R.id.cancel:
75
// "Cancel"
76
finish();
77
return true;
78
}
79
return super.onOptionsItemSelected(item);
80
}
82
}