| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Android example source code file (

This example Android source code file ( is included in the "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Android by Example" TM.

Java - Android tags/keywords

android, content, cursor, delete_id, edit_id, email_id, failed, home_id, list_id, override, string, textview, ui, uri, util, utils, view, wiki_word_matcher, wikiactivityhelper, wikinotes

The Android example source code

 * Copyright (C) 2008 Google Inc.
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.


import java.util.regex.Pattern;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.util.Linkify;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


 * The WikiNotes activity is the default handler for displaying individual
 * wiki notes. It takes the wiki note name to view from the intent data URL
 * and defaults to a page name defined in R.strings.start_page if no page name
 * is given. The lifecycle events store the current content URL being viewed.
 * It uses Linkify to turn wiki words in the body of the wiki note into
 * clickable links which in turn call back into the WikiNotes activity from
 * the Android operating system. If a URL is passed to the WikiNotes activity
 * for a page that does not yet exist, the WikiNoteEditor activity is
 * automatically started to place the user into edit mode for a new wiki note
 * with the given name.
public class WikiNotes extends Activity {
     * The view URL which is prepended to a matching wikiword in order to fire
     * the WikiNotes activity again through Linkify

    private static final String KEY_URL = "wikiNotesURL";

    public static final int EDIT_ID = Menu.FIRST;
    public static final int HOME_ID = Menu.FIRST + 1;
    public static final int LIST_ID = Menu.FIRST + 3;
    public static final int DELETE_ID = Menu.FIRST + 4;
    public static final int ABOUT_ID = Menu.FIRST + 5;
    public static final int EXPORT_ID = Menu.FIRST + 6;
    public static final int IMPORT_ID = Menu.FIRST + 7;
    public static final int EMAIL_ID = Menu.FIRST + 8;

    private TextView mNoteView;
    Cursor mCursor;
    private Uri mURI;
    String mNoteName;
    private static final Pattern WIKI_WORD_MATCHER;
    private WikiActivityHelper mHelper;

    static {
	// Compile the regular expression pattern that will be used to
	// match WikiWords in the body of the note

    public void onCreate(Bundle icicle) {

	mNoteView = (TextView) findViewById(;

	// get the URL we are being asked to view
	Uri uri = getIntent().getData();

	if ((uri == null) && (icicle != null)) {
	    // perhaps we have the URI in the icicle instead?
	    uri = Uri.parse(icicle.getString(KEY_URL));

	// do we have a correct URI including the note name?
	if ((uri == null) || (uri.getPathSegments().size() < 2)) {
	    // if not, build one using the default StartPage name
	    uri = Uri.withAppendedPath(WikiNote.Notes.ALL_NOTES_URI,

	// can we find a matching note?
	Cursor cursor = managedQuery(uri, WikiNote.WIKI_NOTES_PROJECTION,
				     null, null, null);

	boolean newNote = false;
	if ((cursor == null) || (cursor.getCount() == 0)) {
	    // no matching wikinote, so create it
	    uri = getContentResolver().insert(uri, null);
	    if (uri == null) {
		Log.e("WikiNotes", "Failed to insert new wikinote into " +
	    // make sure that the new note was created successfully, and
	    // select
	    // it
	    cursor = managedQuery(uri, WikiNote.WIKI_NOTES_PROJECTION, null,
				  null, null);
	    if ((cursor == null) || (cursor.getCount() == 0)) {
		Log.e("WikiNotes", "Failed to open new wikinote: " +
	    newNote = true;

	mURI = uri;
	mCursor = cursor;
	mHelper = new WikiActivityHelper(this);

	// get the note name
	String noteName = cursor.getString(cursor
	mNoteName = noteName;

	// set the title to the name of the page
	setTitle(getResources().getString(R.string.wiki_title, noteName));

	// If a new note was created, jump straight into editing it
	if (newNote) {
	    mHelper.editNote(noteName, null);
	} else if (!getSharedPreferences(Eula.PREFERENCES_EULA,
	    .getBoolean(Eula.PREFERENCE_EULA_ACCEPTED, false)) {

	// Set the menu shortcut keys to be default keys for the activity as
	// well

    protected void onSaveInstanceState(Bundle outState) {
	// Put the URL currently being viewed into the icicle
	outState.putString(KEY_URL, mURI.toString());

    protected void onResume() {
	Cursor c = mCursor;
	if (c.getCount() < 1) {
	    // if the note can't be found, don't try to load it -- bail out
	    // (probably means it got deleted while we were frozen;
	    // thx to joe.bowbeer for the find)

     * Show the wiki note in the text edit view with both the default Linkify
     * options and the regular expression for WikiWords matched and turned
     * into live links.
     * @param body
     *            The plain text to linkify and put into the edit view.
    private void showWikiNote(CharSequence body) {
	TextView noteView = mNoteView;

	// Add default links first - phone numbers, URLs, etc.
	Linkify.addLinks(noteView, Linkify.ALL);

	// Now add in the custom linkify match for WikiWords
	Linkify.addLinks(noteView, WIKI_WORD_MATCHER,
			 WikiNote.Notes.ALL_NOTES_URI.toString() + "/");

    public boolean onCreateOptionsMenu(Menu menu) {
	menu.add(0, EDIT_ID, 0, R.string.menu_edit).setShortcut('1', 'e')
	menu.add(0, HOME_ID, 0, R.string.menu_start).setShortcut('4', 'h')
	menu.add(0, LIST_ID, 0, R.string.menu_recent).setShortcut('3', 'r')
	menu.add(0, DELETE_ID, 0, R.string.menu_delete).setShortcut('2', 'd')
	menu.add(0, ABOUT_ID, 0, R.string.menu_about).setShortcut('5', 'a')
	menu.add(0, EXPORT_ID, 0, R.string.menu_export).setShortcut('7', 'x')
	menu.add(0, IMPORT_ID, 0, R.string.menu_import).setShortcut('8', 'm')
	menu.add(0, EMAIL_ID, 0, R.string.menu_email).setShortcut('6', 'm')
	return true;

    public boolean onMenuItemSelected(int featureId, MenuItem item) {
	switch (item.getItemId()) {
	case EDIT_ID:
	    mHelper.editNote(mNoteName, mCursor);
	    return true;
	case HOME_ID:
	    return true;
	case DELETE_ID:
	    return true;
	case LIST_ID:
	    return true;
	case WikiNotes.ABOUT_ID:
	    return true;
	case WikiNotes.EXPORT_ID:
	    return true;
	case WikiNotes.IMPORT_ID:
	    return true;
	case WikiNotes.EMAIL_ID:
	    return true;
	    return false;

     * If the note was edited and not canceled, commit the update to the
     * database and then refresh the current view of the linkified note.
    protected void onActivityResult(int requestCode, int resultCode,
				    Intent result) {
	super.onActivityResult(requestCode, resultCode, result);
	if ((requestCode == WikiActivityHelper.ACTIVITY_EDIT) &&
	    (resultCode == RESULT_OK)) {
	    // edit was confirmed - store the update
	    Cursor c = mCursor;
	    Uri noteUri = ContentUris
		.withAppendedId(WikiNote.Notes.ALL_NOTES_URI, c.getInt(0));
	    ContentValues values = new ContentValues();
	    values.put(WikiNote.Notes.BODY, result
	    values.put(WikiNote.Notes.MODIFIED_DATE, System
	    getContentResolver().update(noteUri, values,
					"_id = " + c.getInt(0), null);

Other Android examples (source code examples)

Here is a short list of links related to this Android source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.