Pages
Android 2.2 Released
5:00 AM
Posted by Mina Samy
thi version has an API level equals to 8
some of the new features:
- Using Dalvik JIT (just in time) compiler which produces faster performance.
- the ability to store applications on SD card instead on phone memory as in previous versions.
- Application Backup API, enabling to store a backup of any application and use it on other phones.
- The browser java script rendering has became faster.
or watch this video
| Reactions: |
Android Menus part 4: Alternative menus
9:18 PM
Posted by Mina Samy
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:
So to add the alternative menu item in the activity of Application A, we write the following in onCreateOptionsMenu method:
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:
When you press on the Access Employees menu item, the activity in Application B will be launched.
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:
- Application A: deals with the employees data.
- Application B: receives the content Uri of the employees and do some calculations on it.
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.
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:- Action: Intent.ACTION_VIEW.
- Category: android.intent.category.ALTERNATIVE.
- Data of MIME type:vnd.android.cursor.item/mina.android.Employees.
When you press on the Access Employees menu item, the activity in Application B will be launched.
| Reactions: |
Android Menus: part 3: Context Menus
10:13 PM
Posted by Mina Samy
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:
- Register the widget for a context menu.
- Add menu items to the context menu.
@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:
- Context Menu: an instance of the context menu.
- View: the view to which the context menu is attached.
- ContextMenuInfo: carries additional information regarding the creation of the context menu.
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
12:43 AM
Posted by Mina Samy
- By implementing onOptionsItemSelected method.
- By implementing listeners to single menu items.
- By using intents.
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 OnMenuItemClickListenerthen 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
1:55 AM
Posted by Mina Samy
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:
- GroupID: used to group related menu items to apply some configurations on all items of a certain group once at a time (discussed later) .
- ItemID: an integer to identify the menu item.
- 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.. - 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.
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: |
Subscribe to:
Posts (Atom)









