The project describes How to implement option menu into your application. Option menu is the primary collection of menu items for an activity, which appears when the user touches the MENU button.
Underlying Algorithm:
Basic description of algorithm in step by step form:
- Create a Project MyCheckBoxMenu
- Create and Open the res/menu/menu.xml file and insert the following:
- Define the required strings in strings.xml.
- Make sure you write following method in MyCheckBoxMenu class:
- Create and Open res/xml/settings.xml file and insert following code:
- Next add a new activity in manifest.xml for prefs class.
- Run the application.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/settings_title"
android:title="@string/settings_title" />
<item android:id="@+id/back_title"
android:title="@string/back_title" />
<item android:id="@+id/exit_title"
android:title="@string/exit_title" />
</menu>
<item android:id="@+id/settings_title"
android:title="@string/settings_title" />
<item android:id="@+id/back_title"
android:title="@string/back_title" />
<item android:id="@+id/exit_title"
android:title="@string/exit_title" />
</menu>
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
{
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
The inflater.inflate() method loads the menu file for the Activity, specified by the resource ID — R.menu.menu refers to the res/menu/menu.xml menu file.
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference android:key="music"
android:title="@string/music_title" android:summary="@string/music_summary"
android:defaultValue="true" />
<CheckBoxPreference android:key="hints"
android:title="@string/hints_title" android:summary="@string/hints_summary"
android:defaultValue="true" />
</PreferenceScreen>
<CheckBoxPreference android:key="music"
android:title="@string/music_title" android:summary="@string/music_summary"
android:defaultValue="true" />
<CheckBoxPreference android:key="hints"
android:title="@string/hints_title" android:summary="@string/hints_summary"
android:defaultValue="true" />
</PreferenceScreen>
This will create a menu which will have checkbox to select and deselect settings for application and will be saved accordingly in prefs.
<activity android:name="org.example.MyCheckBoxMenu.Prefs"
android:label="@string/settings_title">
</activity>
android:label="@string/settings_title">
</activity>
Steps to Create New Project:
- Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. MyCheckBoxMenu
- Then enter the following information:
- Project name: MyCheckBoxMenu
- Build Target: Android 1.6
- Application name: MyCheckBoxMenu
- Package name: org.example.MyCheckBoxMenu
- Create Activity: MyCheckBoxMenu
On Clicking Finish MyCheckBoxMenu code structure is generated with the necessary Android Packages being imported along with MyCheckBoxMenu.java. Following code must be added in MyCheckBoxMenu class to get the menu work.
public booleanonOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.settings_title:
startActivity(new Intent(this, Prefs.class));
return true;
case R.id.exit_title:
finish();
return true;
}
return false;
}
{
switch (item.getItemId())
{
case R.id.settings_title:
startActivity(new Intent(this, Prefs.class));
return true;
case R.id.exit_title:
finish();
return true;
}
return false;
}
Prefs.java
package org.example.MyCheckBoxMenu;
import org.example.MyCheckBoxMenu.R;
import org.example.MyCheckBoxMenu.R;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceActivity;
public class Prefs extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
Output –The final output:
Click on “MENU”
Click on “Options”
No comments:
Post a Comment