Skip to content

Commit 43bb34f

Browse files
committed
Implemented Days of the Week option as Menu Item Option
1 parent 2db7e7b commit 43bb34f

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<string name="app_name">MenuOptionsExample</string>
55
<string name="hello_world">Example app to show case the Menu Options an Items in Android!</string>
66
<string name="colors">Background Color Preference</string>
7-
<string name="options">Random Options</string>
7+
<string name="days_of_week">Days of Week</string>
88
<string name="quit">Quit App</string>
99
<string name="action_settings">Settings</string>
1010

src/com/example/menuoptions/MainActivity.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package com.example.menuoptions;
22

33
import android.app.Activity;
4+
import android.app.AlertDialog;
5+
import android.content.DialogInterface;
6+
import android.content.DialogInterface.OnClickListener;
47
import android.os.Bundle;
58
import android.view.Menu;
69
import android.view.MenuItem;
10+
import android.widget.Toast;
711

812
public class MainActivity extends Activity {
913

1014
private static final int ITEM_COLORS_ID = Menu.FIRST;
11-
private static final int ITEM_OPTIONS_ID = Menu.FIRST + 1;
15+
private static final int ITEM_DAYS_OF_WEEK_ID = Menu.FIRST + 1;
1216
private static final int ITEM_QUIT_ID = Menu.FIRST + 2;
1317

1418
@Override
@@ -24,7 +28,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
2428

2529
//Adding Menu Items Programatically
2630
menu.add(Menu.NONE, ITEM_COLORS_ID, Menu.NONE, R.string.colors);
27-
menu.add(Menu.NONE, ITEM_OPTIONS_ID, Menu.NONE, R.string.options);
31+
menu.add(Menu.NONE, ITEM_DAYS_OF_WEEK_ID, Menu.NONE, R.string.days_of_week);
2832
menu.add(Menu.NONE, ITEM_QUIT_ID, Menu.NONE, R.string.quit); //Is Quit Menu, a good practice for Android app??
2933

3034
return true;
@@ -36,8 +40,19 @@ public boolean onOptionsItemSelected(MenuItem item) {
3640
//if (id == R.id.action_settings) { return true; }
3741
if (id == ITEM_COLORS_ID) {
3842
//
39-
} else if (id == ITEM_OPTIONS_ID ) {
40-
//
43+
} else if (id == ITEM_DAYS_OF_WEEK_ID ) {
44+
final String[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
45+
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
46+
alertDialogBuilder.setTitle("Sample Options");
47+
alertDialogBuilder.setItems(days, new OnClickListener() {
48+
@Override
49+
public void onClick(DialogInterface dialog, int which) {
50+
String day = days[which];
51+
Toast.makeText(MainActivity.this, "So you like " + day + ", ha?", Toast.LENGTH_LONG).show();
52+
}
53+
});
54+
alertDialogBuilder.setNegativeButton("Cancel", null);
55+
alertDialogBuilder.show();
4156
} else if (id == ITEM_QUIT_ID){
4257
finish();
4358
}

0 commit comments

Comments
 (0)