Android Programming Tutorials

(Romina) #1
Opening a JAR

<uses-permission android:name="android.permission.INTERNET" />
<application android:label="@string/app_name">
<activity android:name=".Patchy"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".EditPreferences">
</activity>
</application>
</manifest>

Then, we want to have an option menu to trigger editing the preferences, so


create Patchy/res/menu/option.xml with the following XML:


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/prefs"
android:title="Settings"
android:icon="@drawable/ic_menu_preferences"
/>
</menu>

You will need an ic_menu_preferences.png drawable resource, perhaps the


one you used in the LunchList tutorials.


Finally, we need to clone the code from LunchList that opens our option


menu using the above XML and routes the item click to the EditPreference


activity. Paste the following methods into Patchy:


@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(getApplication())
.inflate(R.menu.option, menu);

return(super.onCreateOptionsMenu(menu));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()==R.id.prefs) {
startActivity(new Intent(this, EditPreferences.class));

return(true);
}

169
Free download pdf