Android 2.2 Released

Google released version 2.2 of the Android SDK.
thi version has an API level equals to 8

some of the new features:
  1. Using Dalvik JIT (just in time) compiler which produces faster performance.
  2. the ability to store applications on SD card instead on phone memory as in previous versions.
  3. Application Backup API, enabling to store a backup of any application and use it on other phones.
  4. The browser java script rendering has became faster.
for more info you can check this link http://developer.android.com/sdk/android-2.2.html

or watch this video

Reactions: 

Android Menus part 4: Alternative menus

Android offers a third type of menus: Alternative menus which allow multiple applications to use each other. An application menu can contain menu items that point to other applications that deal with a certain data type that is passed from the application by an intent.

This functionality is related to the concept of Content Providers which is in brief the ability of an application to expose its data (stored in a database or a file) by defining a MIME type to it and through a content URI to be accessed by any other application through this URI.

For example if an application name Employees has some data of employees stored within this application context, and another application wants to access this data; then Employees application should declare a content provider  to expose its data, with a MIME type:

vnd.android.cursor.item/mina.android.Employees so any other application can access the employees data by calling the Uri content://employees/All to access all employees or the Uri content://employees/1 to access a single employee instance (for example).

Back to our Alternative menus issue, suppose this Scenario: We have two applications:
  1. Application A: deals with the employees data.
  2. Application B: receives the content Uri of the employees and do some calculations on it.
Now we want to add an alternative menu item in an activity in Application A so that when clicked passes a URI of employees and launches an activity in Application B (that manipulates the employees data according to the URI received).

So to add the alternative menu item in the activity of Application A, we write the following in onCreateOptionsMenu method:
public boolean onCreateOptionsMenu(Menu menu) {

     //adds a regular menu item
     menu.add("Regular item");
        //create the intent with the Uri of the employees content provider
     Intent targetIntent=
        new Intent(Intent.ACTION_VIEW, Uri.parse("content://employees/All"));
     targetIntent.addCategory(Intent.CATEGORY_ALTERNATIVE);
     menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, //item group
       Menu.CATEGORY_ALTERNATIVE,  //item id
       Menu.CATEGORY_ALTERNATIVE,  //item order
       this.getComponentName(), //our activity class name
       null, //no specific menu items required
       targetIntent, // the intent to handle
       0, //no flags
       null); //Optional array in which to place the menu
//items that were generated for each of
//the specifics that were requested

     return true;
    }
Here's what we did:
  • Create an intent with the desired action (Intent.ACTION_VIEW) on the specified content provider Uri (content://employees/All).
  • Call addIntentOptions method with the specified parameters.
The above code adds a menu item that launches all possible activities in all applications on the device that can deal with the action Intent.ACTION_VIEW on the Uri content://employees/All.

Now in Application B, if we want it to handle such an intent we have to do the following.

Define an activity in the application and specify in the AndroidManifest.xml file that this activity can handle the requests of the employees content provider like this:
<activity android:name=".ContentProvidersDemo" android:label="@string/app_name">

   <intent-filter android:label="Access Employees">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.ALTERNATIVE" />
    <data android:mimeType="vnd.android.cursor.item/mina.android.Employees" />
   </intent-filter>
  </activity>
The above IntentFilter means that this activity will respond t0 any implicit intent from any application with the following parameters:
  1. Action: Intent.ACTION_VIEW.
  2. Category: android.intent.category.ALTERNATIVE.
  3. Data of MIME type:vnd.android.cursor.item/mina.android.Employees.
So in Application A when you press on the menu button, you'll see a menu like this:



When you press on the Access Employees menu item, the activity in Application B will be launched.


Reactions: 

Android Menus: part 3: Context Menus

In  previous posts we explored two types of android menus: Options menu and submenus. In this post we’re going to see another menu type: Context menus.

Context Menu:


Context menus are the menus that appear when you right-click in windows.
In android Context menu are attached to widgets and the appear when you click on the widget for a long time (long click).

To add context menus to widgets you have to do two things:
  1. Register the widget for a context menu.
  2. Add menu items to the context menu.
So to register a context menu with a TextView you do it like this:
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txt=(TextView)findViewById(R.id.txt);
        registerForContextMenu(txt);
    }

We call registerForContextMenu(View V) method to tell the activity that the view is going to have a context menu.

Then we add items to the context menu by overriding the onCreateContext method:
@Override
    public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo info)
    {
     menu.setHeaderTitle("Context Menu");
     menu.add("Item");
    }

When you make a long click on the text view the context menu will appear like this:


The onCreateContextMenu method has the following parameters:
  1. Context Menu: an instance of the context menu.
  2. View: the view to which the context menu is attached.
  3. ContextMenuInfo: carries additional information regarding the creation of the context menu.
To handle the context menu items click events you can implement the callback method onContextItemSelected.
We do it the same way we handle onOptionsItemSelected.
@Override
    public boolean onContextItemSelected(MenuItem item)
    {
     txt.setText(item.getTitle());
     return true;
    }

Reactions: 

Android Menus: part 2: Handling Menu Items Events

We can handle menu items events by three ways:
  1. By implementing onOptionsItemSelected method.
  2. By implementing listeners to single menu items.
  3. By using intents.
Using onOptionsItemSelected method:
Implement the onOptionsItemSelected method like this:
@Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
     TextView txt=(TextView)findViewById(R.id.txt);
     txt.setText("you clicked on item "+item.getTitle());
     return true;
    }

Notice that it returns a Boolean which should be true to execute the method, if false it will not execute.

You can switch between the menu items like this:
@Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
     TextView txt=(TextView)findViewById(R.id.txt);
     switch(item.getItemId())
     {
     case 1:
      txt.setText("you clicked on item "+item.getTitle());
      return true;
     case 2:
      txt.setText("you clicked on item "+item.getTitle());
      return true;
     case 3:
      txt.setText("you clicked on item "+item.getTitle());
      return true;
      
       
     }
     return super.onOptionsItemSelected(item);
    
    }


Notice that we return true for every handled menu item. And for un handled menu items (outside switch block) we call the super class method.

Using listners:

We can handle options menu items click events by making the activity implement onMenuItemClickListner interface provide an implementation of onMenuItemClick method like this:
public class MenusDemo extends Activity implements OnMenuItemClickListener
then implement the method:
public boolean onMenuItemClick(MenuItem item) {
  TextView txt=(TextView)findViewById(R.id.txt);
  txt.append("listner");
  return false;
 }


Notice that the method returns a Boolean. If it returns true no other callbacks will be executed. If returns false then onOptionsItemSelected callback will be executed directly after this callback.

Using intents:
You can specify an intent to be launched when an options menu item is clicked like this:
menu.add(1, “dialItem”, 1, "Dial").setIcon(R.drawable.dvd).setIntent(new Intent(Intent.ACTION_DIAL));

note that if you specify an intent for an item and at the same time override the onOptionsItemSelected method and handle the selection for that item, the precedence of execution is to the code in the onOptionsItemSelected.
meaning that the code in onOptionsItemSelected method will be executed first, if it returns true then the intent will not be launched, if returns false then the intent will launch.

So if you want to use intent for menu items don’t handle it in onOptionsItemSelected method but invoke the parent onOptionsItemSelected.

Scenario 1:
@Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
     menu.add(1, “dialItem”, 1, "Dial").setIcon(R.drawable.dvd).setIntent(new Intent(Intent.ACTION_DIAL));     return super.onCreateOptionsMenu(menu);
     
    }

@Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
     TextView txt=(TextView)findViewById(R.id.txt);
     switch(item.getItemId())
     {
     case 1:
      txt.append("you clicked on item "+item.getTitle());
      return true;
     case “dialItem”:
      txt.setText("you clicked on item "+item.getTitle());
      return true;
     case 3:
      txt.setText("you clicked on item "+item.getTitle());
      return true;
      
       
     }
     return super.onOptionsItemSelected(item);
    
    }


This will execute the code of the onOptionsItemSelected method and the dialer intent will not be launched.

Scenario 2:

@Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
     menu.add(1, “dialItem”, 1, "Dial").setIcon(R.drawable.dvd).setIntent(new Intent(Intent.ACTION_DIAL));     return super.onCreateOptionsMenu(menu);
     
    }

@Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
     TextView txt=(TextView)findViewById(R.id.txt);
     switch(item.getItemId())
     {
     case 1:
      txt.append("you clicked on item "+item.getTitle());
      return true;
     case “dialItem”:
      txt.setText("you clicked on item "+item.getTitle());
      return false;
     case 3:
      txt.setText("you clicked on item "+item.getTitle());
      return true;
      
       
     }
     return super.onOptionsItemSelected(item);
    
    }
This will execute the code of  the onOptionsItemSelected method and then dialer intent will be launched.


Scenario 3:

@Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
     menu.add(1, “dialItem”, 1, "Dial").setIcon(R.drawable.dvd).setIntent(new Intent(Intent.ACTION_DIAL));     return super.onCreateOptionsMenu(menu);
     
    }

@Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
     TextView txt=(TextView)findViewById(R.id.txt);
     switch(item.getItemId())
     {
     case 1:
      txt.append("you clicked on item "+item.getTitle());
      return true;
     
     case 3:
      txt.setText("you clicked on item "+item.getTitle());
      return true;
      
       
     }
     return super.onOptionsItemSelected(item);
    
    }



the onOptionsItemSelected does not handle the "dialerItem" selection, so The dialer intent will be launched directly.

Reactions: 

Android Menus: Part 1: Options menu and sub menus

Android phones have the menu button which displays a menu with several items that provide navigation or more functionality or settings to your applications

Android has three types of menus
1. Options Menu
2. Context Menu
3. Sub menus

Menu items can be grouped and each menu item can have submenu items.

Options Menu

Each activity has a single menu. It appears when you press the menu button Each activity creates this menu in the callback method onCreateOptionsMenu you can override this method and add items to the menu.
If you press the menu button you will see the menu like this:
public class MenusDemo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
     menu.add("Item1");
     menu.add("Item2");
     return true;
    }
}



Notice that the onCreateOptionsMenu method returns a Boolean. It should return true for the menu to appear, if returns false the menu will be invisible.

If you add many menu items, more than five items they will appear like this:
public boolean onCreateOptionsMenu(Menu menu)
    {
     menu.add(1, 1, 0, "Item1");
     menu.add(1, 2, 1, "Item2");
     menu.add(1, 3, 2, "Item3");
     menu.add(1, 4, 3, "Item4");
     menu.add(1, 5, 4, "Item5");
     
     menu.add(2, 6, 0, "Item6");
     menu.add(2, 7, 1, "Item7");
     menu.add(2, 8, 2, "Item8");
     menu.add(2, 9, 3, "Item9");
     menu.add(2, 10, 4, "Item10");
     
     
     
     return true;
    }



When you press on the “more” link you will see the rest of the menu items



The menu.add(int GroupID,int ItemID,int Order,String Title) method has the following parameters:
  1. GroupID: used to group related menu items to apply some configurations on all items of a certain group once at a time (discussed later) .
  2. ItemID: an integer to identify the menu item.
  3. Order: the sort order or the order ID, controls the order of menu items in a menu. Items with lower order id appear first. There are some constants defined for sort order for different kinds of menu items
    Like Menu.CATEGORY_SECONDRY for secondry menu items, Menu.CATEGORY_SYSTEM for system menu items and Menu.CATEGORY_ALTERNATIVE for altenative menu items..
  4. Title: the text of the menu item


We could have written the previous code using the predefined the sort order constants like this:
menu.add(1, 1, Menu.FIRST, "Item1");
     menu.add(1, 2, Menu.FIRST+1, "Item2");
     menu.add(1, 3, Menu.FIRST+2, "Item3");
     menu.add(1, 4, Menu.FIRST+3, "Item4");
     menu.add(1, 5, Menu.FIRST+4, "Item5");
     
     menu.add(2, 6, Menu.CATEGORY_SECONDARY, "Item1");
     menu.add(2, 7, Menu.CATEGORY_SECONDARY+1, "Item2");
     menu.add(2, 8, Menu.CATEGORY_SECONDARY+2, "Item3");
     menu.add(2, 9, Menu.CATEGORY_SECONDARY+3, "Item4");
     menu.add(2, 10, Menu.CATEGORY_SECONDARY+4, "Item5");




Adding Icons to menu items:
We saw that only the first five items can appear in the options menu and the other items appear in a dialog when you press “more” item.
For the first five items you can add an icon to appear next to the item's title:
menu.add(1, 1, 0, "Blu-Ray").setIcon(R.drawable.bluray);
     menu.add(1, 2, 1, "DVD").setIcon(R.drawable.dvd);
     menu.add(1, 3, 2, "Hard Disk").setIcon(R.drawable.hd);
     menu.add(1, 4, 3, "Sites").setIcon(R.drawable.sites);
     menu.add(1, 5, 4, "USB").setIcon(R.drawable.usb);
     
     menu.add(2, 6, 5, "Item1");
     menu.add(2, 7, 6, "Item2");
     menu.add(2, 8, 7, "Item3");
     menu.add(2, 9, 8, "Item4");
     menu.add(2, 10, 9, "Item5");



Grouping Menu Items:

We saw that the method we use to add menu items us Menu.Add(GroupID,ItemId,OrderID,Title)
The first parameter is GroupID, what is it ? the answer leads us to grouping Menu Items.

Grouping menu items makes it easy to apply some options on some related menu items by applying these options on all menu items that belong to a group once instead of applying them on each single item at a time.

The actions that can be applied on a group can be:
  • Menu.removeGroup(int GroupID): to remove all menu items belonging to the same group.
  • Menu.setGroupCheckable(int GroupID,Boolean Checakble,Boolean Exclusive): maked a group appear with a check mark beside each item. If the Boolean Exclusive is set to true then the items will appear as if they are in a radiobuttongroup and only one item can be selected at a time. If set to false then they will appear with check boxes beside each item and multiple items can be selected at a time.
  • Menu.setGroupEnabled(int GroupID,Boolean Enabled): sets all items in a group to be enabled/disabled.
  • Menu.setGroupVisible(int GroupID,Boolean Visible): sets all items in a group to be visible/invisible.
Note: the methods mentioned above can be applied only to menu items in the secondary menu (items that appear when you click the more button)


Adding Alpha-Numeric shortcuts to menu Items:
You can add alphabetic or numeric shortcuts to menu items like this:
menu.add(2, 8, 7, "Item3").setNumericShortcut('3');
menu.add(2, 9, 8, "Item4").setAlphabeticShortcut('c');
then setting the querty mode of the menu to true like this:
menu.setQwertyMode(true);

and that's it for Android's options menu.

In a next post we'll talk about handling options menu events

Reactions: