Understanding User Interface in Android – Part 4: Even More Views

In this final installation of the Android Views series of articles, we shall continue our exploration of another category of views – Menu views, and some additional cool views. The views discussed include:

Context Menu

Options Menu

AnalogClock

DigitalClock

WebView

Note: for all the examples in this article, you shall use the project created in the previous article.

Menus

Menus are useful for displaying additional options that are not directly visible on the main UI of an application. There are two main types of menus in Android:

Context Menu – displays information related to a particular view on an activity. In Android, to activate a context menu you press and hold on to it.

Options Menu – displays information related to the current activity. In Android, you activate the options menu by pressing the MENU key.

Figure 1 shows an example of an options menu in the Browser application. The option menu is displayed whenever the user presses the MENU button. The menu items displayed is dependent on the current activity that is running.

Figure 1 – The options menu in the Browser application

Figure 2 shows a context menu that is displayed when the user presses and holds on a hyperlink displayed on the page. The menu items displayed are dependent on the component or view currently selected. To activate the context menu, the user selects an item on the screen and either presses and holds it or simply presses the center button on the directional keypad.

Figure 2 – The context menu in the Browser application

To see how to implement menus in Android, add a new file to the res/layout folder and name it as menu.xml. Populate it with the following element:

Then, add a new class to the src/net.learn2develop.AndroidViews folder and name it as MenuExample.java. You will leave this file alone for now. Modify the AndroidManifest.xml file to register the new activity:

Creating the Helper Methods

For the examples in this section, you will create two helper methods in the MenuExample.java file. They are CreateMenu() and MenuChoice(). Define the two helper methods and the onCreate() method as shown below:

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

packagenet.learn2develop.AndroidViews;

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.Button;

import android.widget.Toast;

publicclassMenuExampleextendsActivity

{

@Override

publicvoidonCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.menu);

Button btn=(Button)findViewById(R.id.btn1);

btn.setOnCreateContextMenuListener(this);

}

privatevoidCreateMenu(Menu menu)

{

menu.setQwertyMode(true);

MenuItem mnu1=menu.add(0,0,0,"Item 1");

{

mnu1.setAlphabeticShortcut('a');

mnu1.setIcon(R.drawable.alert_dialog_icon);

}

MenuItem mnu2=menu.add(0,1,1,"Item 2");

{

mnu2.setAlphabeticShortcut('b');

mnu2.setIcon(R.drawable.ic_popup_reminder);

}

MenuItem mnu3=menu.add(0,2,2,"Item 3");

{

mnu3.setAlphabeticShortcut('c');

mnu3.setIcon(R.drawable.icon);

}

MenuItem mnu4=menu.add(0,3,3,"Item 4");

{

mnu4.setAlphabeticShortcut('d');

}

menu.add(0,3,3,"Item 5");

menu.add(0,3,3,"Item 6");

menu.add(0,3,3,"Item 7");

}

privatebooleanMenuChoice(MenuItem item)

{

switch(item.getItemId()){

case0:

Toast.makeText(this,"You clicked on Item 1",

Toast.LENGTH_LONG).show();

returntrue;

case1:

Toast.makeText(this,"You clicked on Item 2",

Toast.LENGTH_LONG).show();

returntrue;

case2:

Toast.makeText(this,"You clicked on Item 3",

Toast.LENGTH_LONG).show();

returntrue;

case3:

Toast.makeText(this,"You clicked on Item 4",

Toast.LENGTH_LONG).show();

returntrue;

case4:

Toast.makeText(this,"You clicked on Item 5",

Toast.LENGTH_LONG).show();

returntrue;

case5:

Toast.makeText(this,"You clicked on Item 6",

Toast.LENGTH_LONG).show();

returntrue;

case6:

Toast.makeText(this,"You clicked on Item 7",

Toast.LENGTH_LONG).show();

returntrue;

}

returnfalse;

}

}

The CreateMenu() method creates a list of menu items and modifies the behavior of each menu item. The parameters for the add() method are: groupid, itemid, order, and title. The setAlphabeticShortcut() method assigns a keyboard shortcut to the menu item so that when the user presses a specific key, the menu item would be selected. The setIcon() method assigns an icon to the menu item.

The MenuChoice() method displays the selected menu item using the Toast class. Next, copy two images (as shown in Figure 3) into the res/drawable folder.

Figure 3 – Adding images to the res/drawable folder

Options Menu

To display the options menu for your activity, you need to override two methods – onCreateOptionsMenu() and onOptionsItemSelected(). The onCreateOptionsMenu() method is called when the MENU button is pressed. In this event, you would call the CreateMenu() helper method to display the options menu. When a menu item is selected, the onOptionsItemSelected() method will be called. In this case, you call the MenuChoice() method to display the menu item selected (and do whatever you need to do):

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

publicclassMenuExampleextendsActivity

{

@Override

publicvoidonCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.menu);

}

privatevoidCreateMenu(Menu menu)

{

//...

}

privatebooleanMenuChoice(MenuItem item)

{

//...

}

//---only created once---

@Override

publicbooleanonCreateOptionsMenu(Menu menu){

super.onCreateOptionsMenu(menu);

CreateMenu(menu);

returntrue;

}

@Override

publicbooleanonOptionsItemSelected(MenuItem item)

{

returnMenuChoice(item);

}

}

Modify the ViewsActivity.java file as follows to start the MenuExample activity:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

packagenet.learn2develop.AndroidViews;

import android.app.Activity;

import android.os.Bundle;

import android.content.Intent;

publicclassViewsActivityextendsActivity{

/** Called when the activity is first created. */

@Override

publicvoidonCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

startActivity(newIntent(this,MenuExample.class));

}

}

Press F11 to debug the application. When you press the MENU key, you will see the options menu as shown in Figure 4.

Figure 4 – Displaying a options menu in your application

Observe the icons displayed for menu item 1, 2 and 3. Also, if the options menu has more than six items, there will be a More menu item to represent the additional menu items. The right of Figure 3-24 shows pressing the More item displays the additional menu items as a list.

Context Menu

If you want to associate a context menu to a view on an activity, you need to call the setOnCreateContextMenuListener() method of that particular view. For example, the following code shows how you can associate a context menu with the Button view:

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

packagenet.learn2develop.AndroidViews;

import android.app.Activity;

import android.os.Bundle;

import android.view.ContextMenu;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.ContextMenu.ContextMenuInfo;

import android.widget.Button;

import android.widget.Toast;

publicclassMenuExampleextendsActivity

{

@Override

publicvoidonCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.menu);

Button btn=(Button)findViewById(R.id.btn1);

btn.setOnCreateContextMenuListener(this);

}

privatevoidCreateMenu(Menu menu)

{

//...

}

privatebooleanMenuChoice(MenuItem item)

{

//...

}

//---only created once---

@Override

publicbooleanonCreateOptionsMenu(Menu menu)

{

super.onCreateOptionsMenu(menu);

CreateMenu(menu);

returntrue;

}

@Override

publicbooleanonOptionsItemSelected(MenuItem item)

{

returnMenuChoice(item);

}

@Override

publicvoidonCreateContextMenu(ContextMenu menu,View view,

ContextMenuInfo menuInfo)

{

super.onCreateContextMenu(menu,view,menuInfo);

CreateMenu(menu);

}

@Override

publicbooleanonContextItemSelected(MenuItem item)

{

returnMenuChoice(item);

}

}

Like the Options menu, you need to override the onCreateContextMenu() and onContextItemSelected() methods. Press F11 in Eclipse to debug the application. Press the Button view and hold for a few seconds. You will now see the context menu (see Figure 5). Observe the keyboard shortcuts for menu items 1 to 4 shown below the menu names.

Figure 5 – Displaying context menu in your application

Some Additional Views

Besides the standard views that you have seen up to this point, the Android SDK also provides some interesting views that make your applications much more interesting. In the following sections, you will learn more about the following views:

AnalogClock

DigitalClock

WebView

AnalogClock and DigitalClock Views

The AnalogClock view displays an analog clock. Populate the main.xml file with the following element:

For the WebView view to work, you need to add the INTERNET permission to the AndroidManifest.xml file. Modify the ViewsActivity.java file as follows to start the WebViewExample activity:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

packagenet.learn2develop.AndroidViews;

import android.app.Activity;

import android.os.Bundle;

import android.content.Intent;

publicclassViewsActivityextendsActivity{

/** Called when the activity is first created. */

@Override

publicvoidonCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

startActivity(newIntent(this,WebViewExample.class));

}

}

Press F11 to debug the application on the Android emulator. Figure 8 shows the WebView view in action.

Figure 8 – The WebView displaying a web page

You can also dynamically formulate a HTML string and load it into the WebView view, using the loadDataWithBaseURL() method:

1

2

3

4

5

6

7

8

WebView wv=(WebView)findViewById(R.id.webview1);

finalStringmimeType="text/html";

finalStringencoding="UTF-8";

Stringhtml="<H1>A simple HTML page</H1><body>"+

"<p>The quick brown fox jumps over the lazy dog</p>";

wv.loadDataWithBaseURL("",html,mimeType,encoding,"");

Figure 9 shows the WebView with the HTML string loaded.

Figure 9 – Displaying the content of a HTML string

If you have static pages that you want to load directly from your project, you can save the HTML pages under the assets folder of your package. Figure 10 shows that I have added a page named Index.html under the assets folder.

Figure 10 – Adding a HTML page in the assets folder

The content of the Index.html is as follows:

1

2

3

4

5

<h1>Asimple HTML page</h1>

<body>

<p>The quick brown fox jumps over the lazy dog</p>

<img src='https://www.google.com/logos/Logo_60wht.gif'/>

</body>

To load the WebView using the content stored in the Index.html file, use the loadUrl() method as follows:

1

2

WebView wv=(WebView)findViewById(R.id.webview1);

wv.loadUrl("file:///android_asset/Index.html");

Figure 11 shows the content loaded in the WebView view.

Figure 11 – Loading the HTML page using WebView

Summary

In this article, you have seen how to implement context and option menus in your Android applications. In addition, you have also seen how to use some of the other cool views, such as the AnalogClock and DigitalClock. And this concludes the series of Android views. I hope you now have a great grasp of how views work in Android. Have a great Android application experience!

This is a website of Afilias Technologies Ltd, a private company limited by shares, incorporated and registered in the Republic of Ireland with registered number 398040 and registered office at 6th Floor, 2 Grand Canal Square, Dublin 2, Ireland