Wednesday, December 28, 2011

Starting an deployed Android application

After you ran your application on the virtual device you can start it again on the device. If you press the Home button you can also select your application.

How to select your application from the Android home menu

Selecting the application from the application choicer

8. Menus and Action Bar

8.1. Definition of menu entries

Android provides two possible ways to display global actions which the user can select. The first one is the usage of the Action Bar in the application. The Action Bar is a window feature at the top of the activity that may display the activity title, navigation modes, and other interactive items.

The second option is that the app can open a menu which show additional actions via a popup menu. Typical you define your menu entries in a way that they are added to the action bar if sufficient space is available in the action bar and if not that remaining menu items are displayed in the popup menu.

The option menu and the action bar of your activity is filled by the method onCreateOptionsMenu() of your activity.

The ActionBar also shows an icon of your application. You can also add an action to this icon. If you select this icon the onOptionsItemSelected() method will be called with the value android.R.id.home. The recommendation is to return to the main Activity in your program.

     // If home icon is clicked return to main Activity case android.R.id.home: 
Intent intent = new Intent(this, OverviewActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent); break;

In this method you can create the menu programmatically or you can use a pre-defined XML resources which you inflate via the class "MenuInflator". Each activity has already an instance of the class available and this instance can get accessed via the method getMenuInflator().

onCreateContextMenu() is only called once. If you want to influence the menu later you have to use the method onPrepareOptionsMenu().

8.2. Action bar tabs

It is also possible to add tabs to an action bar.

8.3. Context menus

You can also assign a context menu to an UI widget (view). A context menu is activated if the user "long presses" the view.

A context menu for a view is registered via the method registerForContextMenu(view). The method onCreateContextMenu() is called every time a context menu is activated as the context menu is discarded after its usage. The Android platform may also add options to your view, e.g. "EditText" provides context options to select text, etc.

9. Tutorial: Menus and Action Bar

9.1. Project

This chapter will demonstrate how to create and evaluate a option menu which is displayed in the action bar if sufficient space is available. This example will be extended in the chapter about preferences.

Create a project "de.vogella.android.socialapp" with the activity "OverviewActivity". Change the UI in the file "/res/layout/main.xml" to the following:

                        

9.2. Add a menu XML resource

Select your project, right click on it and select NewOtherAndroidAndroid XML File to create a new XML resource.

Select the option "Menu", enter as File "mainmenu.xml" and press the button "Finish".

Creating a new XML resource for the menu

This will create a new file "mainmenu.xml" in the folder "res/menu" of your project. Android provides an nice editor to edit this file, unfortunately this editor is not always automatically used. To use this editor right-click on your menu file and select Open withAndroid Menu Editor.

Switch if necessary to the "Layout" tab of the editor. Press Add and select "Item". Maintain the following value. This defines the entries in your menu. We will also define that the menu entry is displayed in the action bar if there is sufficient space available.

How to maintain the menu entries in an menu xml file

Change your Activity class "OverviewActivity" to the following. The OnCreateOptionsMenu method is used to create the menu. The behavior in "onOptionsItemSelected" is currently hard-coded to show a Toast and will soon call the preference settings. In case you want to disable or hide menu items you can use the method "onPrepareOptionsMenu" which is called every time the menu is called.

     package de.vogella.android.socialapp;  import android.app.Activity; import android.os.Bundle;
import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.Toast;
public class OverviewActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override public boolean onCreateOptionsMenu(Menu menu)
{ MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.mainmenu, menu); return true; }
@Override public boolean onOptionsItemSelected(MenuItem item) { Toast.makeText(this, "Just a test", Toast.LENGTH_SHORT).show(); return true; } }

Run your application. As there is enough space in the action bar your item will be displayed there. If there would be more items you could press "Menu" on the emulator to see them. If you select the menu item you should see a small info message.

Screenshot of the running application with the menu open

The two "Preference" buttons are not yet active. We will use them in the next chapter.

10. Preferences

Android supports the usage of Preferences to allow you to save data for your application. Preferences are stored as key values. The definition of Preferences can also be done via an XML resource.

Android provides the class "PreferenceActivity" which extends the class Activity. PreferenceActivity supports the simple handling of preferences. This activity can load a preference definition resources via the method addPreferencesFromResource().

To communicate between different components Android uses Intents. Typically the PreferenceActivity is started from another activity via an Intent.

In your application you can access the preference manager via the following:

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);   

Values can get access via the key of the preference setting.

    String username = preferences.getString("username", "n/a");   

To create or change preferences you have to call the edit() methods. Once you have changed the value you have to call commit() to apply your changes.

     Editor edit = preferences.edit(); edit.putString("username", "new_value_for_user"); edit.commit();    

No comments:

Post a Comment

My Headlines

RF Optimization 2G,3G,4G and Wimax

Subscribe Now: google

Add to Google Reader or Homepage