Context Menu Example for Android

Take Your First Steps in Implementing Context Menus in Android

In this tutorial we provide an Android context menu example, using the aptly named ContextMenu class. On a Windows PC system the context menu is sometimes referred to as the right-click menu. (Though using this term is no recommended in case the user is left handed and has switched the mouse, in which case the context menu requires the left-click!)

Android Calendar Context MenuA context menu is a menu of options that apply to the item immediately under the pointer (mouse or finger). For example in the Android Calendar the context menu can be used to add an event to the diary, for the selected hour press and hold until the menu appears. The parts required to implement a simple context menu are:

  1. Define the menu captions, usually in a string resource file.
  2. Define the menu layout in an XML file.
  3. Tell Android a View is using a context menu.
  4. When the context menu is requested show it.
  5. React to the selected menu item.

The example code presented here is going to call the setColorFilter method of an ImageView using a context menu. This will be used to add a basic image effect to a bitmap graphic by applying a red, blue or green filter. In this tutorial we are assuming that you have a working project in Eclipse to use as a base. If not see the article Displaying a Bitmap in Android to generate one. Continue reading

Running an Android SDK Sample App

How to Load and Run a Sample Program from the Android SDK

The Android Robot LogoThe Android Software Development Kit (SDK) gets updated with new version platforms for each new release of the Operating System (OS). Each version platform can be loaded onto your development machine with SDK Manager.exe. There are available sample Applications (Apps) to showcase Android features and provide example code that allows developers to see how to use the Android Application Programming Interface (API).

Getting Hold of the Android Samples

Each SDK platform version provides Android sample source code project, however, they must be selected for installation on your development machine using the SDK Manager program. Run SDK Manager and wait for it to finish doing it’s checks. Expand the required Android Version folder to see and select the Samples for SDK option.

Exmaple Apps can be Loaded with the SDK Manager

Once the samples have been installed they will be found in the android-sdk/samples folder in the Android SDK’s install directory. (On Windows this may be under the Local Settings/Application Data folder, e.g. C:\Documents and Settings\John Doe\Local Settings\Application Data\Android, or wherever you chose to install it.) There will be a folder for each Android API version for which you chose to install the samples, called android-x where x is the API version.

Android SDK Samples

See our article Use Android SDK Samples for Guidance to see a list, and brief description, of the example Apps supplied with the various Android versions. Continue reading

Changing the Font for Android TextViews and EditTexts

Easy Font Modifications to Get Different Text Styles in Android

Setting a new font when developing an Android App puts your own style onto the user interface (UI) and can help it stand out from the crowd. For many Activity screens the interface is usually constructed using Android layouts and interface widgets. The interface widgets, such as CheckBoxes and TextViews, have various properties that can be easily modified to change their look. Changing properties is done via the Properties list when viewing an Activity in the Graphical Layout screen in Eclipse, by editing the layout XML directly, or finally by using code to set property values.

This article mainly covers changing the look of a widget’s font using the Properties list. You can follow this article as a tutorial by starting a new Android Application Project in Eclipse and playing with widget properties as we go. (See our article Your First Android Java Program if you do not now how to create a basic App project. Here we have called the new App and project Android Fonts and given the package name biz.tekeye.fonts but you will be using you own package name.)

Edit Text Properties with the Android Development Tools Graphical Layout

Drag and drop a medium Android TextViewWith the activity_main.xml layout open and the Graphical Layout shown (click the tab at the bottom of the edit area if required) drag and drop some Medium TextViews onto the first activity’s screen. Click on the Form Widgets tab if needed to find the TextViews. With a TextView selected its properties can be changed in the Outline pane. If the Outline pane is not visible use the Eclipse Window and Show View menu option to select Outline to display it. Continue reading