Thursday, June 19, 2014

How to Build a Menu in Android



1. Open the Eclipse software and load your Android app you want to edit. In the left panel, right-click the 'res' file and select 'New.' Click 'Android XML File' to create a new resource file.

2. Add the menu XML data in the new XML file. The following code creates two menu buttons for the app:

android:title='Button 1' />

android:title='Button 2'

android:icon='@drawable/icon' />

3. Double-click the Java file you want to use to load the menu on the phone or table. The following code shows you how to load the menu:MenuInflater menuoptions = getMenuInflater();

menuoptions.inflate(R.menu.menu, menu);

4. Add the code that displays text when the user taps the icon. The following code displays 'Hello World' when the user taps a menu item:@Override

public boolean onOptionsItemSelected(MenuItem item) {

Toast.makeText(this, 'Hello World!', Toast.LENGTH_LONG).show();

}

return true;

5. Click the 'Save' button, then click 'Run' to load the menu in an Android emulator. The program runs in the Android emulator, so you can test its functionality.

No comments:

Post a Comment