-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_33_overflow.txt
214 lines (182 loc) · 8.03 KB
/
code_33_overflow.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
[source code] Android Development Tutorial 33 - Overflow
***** Tutorial Notes
** 33 - Overflow Menu
//Overview//
The overflow menu appears in the ActionBar as 3 stacked squares. The menu is included when you chose the Basic Activity template. By default, it has one item called settings. Try running the Activity before modifying it to see what happens when you click the overflow menu.
The goal here is to replace the default Setting menu with 3 coloritems, and change the display background color when you select one.
//Tutorial//
NOTE: The overflow menu was not showing up in a new Basic activity, in Design and Preview, but is there in the emulator:
• It won’t show at all if you don’t have something in menu_main.xml.
• It won’t show up if the device has a hardware menu button.
• If you don't even get the App title in the preview, change the theme setting using the Editor tool bar.
NOTE: Bucky uses ActionBarActivity which is depreciated. Use AppCompatActivity instead. This also means that you can't select the Holo or Material themes that he does, without getting rendering errors. Don't worry the Overflow menu will be there when you run your app.
1. Start with a Basic Activity
a. Open the context layout
b. Delete hello world TextView
c. Add a large text.
d. Give the Layout and Id so you can reference.
2. Main_menu.xml has only the settings item by default.
a. Delete it because we are not going to use it here.
b. Add a <group>
c. Add items with new ids
d. Change the strings to string references
3. Main Activity
a. import android widget relative layout
b. onOptionsItemSelected delete everything because we are not using the settings menu item
i. get a reference to relative layout
ii. use a switch to determine which item was selected
iii. have default return to insure method returns something.
NOTE: If you didn't import Color you will get errors. Just use Alt-Enter to import it.
***** manifest.xml
***** MainActivity.java
package com.thenewboston.overflowmenu;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
RelativeLayout main_view = (RelativeLayout)findViewById(R.id.main_view);
switch (item.getItemId()){
case R.id.menu_red:{
if (item.isChecked())
item.setChecked(false);
else
item.setChecked(true);
main_view.setBackgroundColor(Color.RED);
return true;
}
case R.id.menu_green:{
if (item.isChecked())
item.setChecked(false);
else
item.setChecked(true);
main_view.setBackgroundColor(Color.GREEN);
return true;
}
case R.id.menu_yellow:{
if (item.isChecked())
item.setChecked(false);
else
item.setChecked(true);
main_view.setBackgroundColor(Color.YELLOW);
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
}
***** activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.thenewboston.overflowmenu.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
***** content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.thenewboston.overflowmenu.MainActivity"
tools:showIn="@layout/activity_main"
android:id="@+id/main_view">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
***** menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.thenewboston.overflowmenu.MainActivity">
<group android:checkableBehavior="single">
<item
android:id="@+id/menu_red"
android:orderInCategory="1"
app:showAsAction="never"
android:title="@string/red_string"/>
<item
android:id="@+id/menu_green"
android:orderInCategory="2"
app:showAsAction="never"
android:title="@string/green_string"/>
<item
android:id="@+id/menu_yellow"
android:orderInCategory="3"
app:showAsAction="never"
android:title="@string/yellow_string"/>
</group>
</menu>
***** strings.xml
<resources>
<string name="app_name">OverflowMenu</string>
<string name="action_settings">Settings</string>
<string name="red_string">Red</string>
<string name="green_string">Green</string>
<string name="yellow_string">Yellow</string>
</resources>