You are here:   Home >> Program >> Android examples of notepad

Android examples of notepad

AndroidManifest.xml

 <? xml version = "1.0" encoding = "utf-8"?> <manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "com.example.android.notepad "> <application android:icon="@drawable/app_notes" android:label="@string/app_name"> <provider android:name="NotePadProvider" android:authorities="com.google.provider.NotePad" /> < activity android: name = "NotesList" android: label = "@ string / title_notes_list"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android: name = "android. intent.category.LAUNCHER "/> </ intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW" /> <action android: name =" android.intent.action.EDIT "/> <action android:name="android.intent.action.PICK" /> <category android:name="android.intent.category.DEFAULT" /> <data android: mimeType =" vnd.android.cursor. dir / vnd.google.note "/> </ intent-filter> <intent-filter> <action android:name="android.intent.action.GET_CONTENT" /> <category android: name =" android.intent.category . DEFAULT "/> <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> </ intent-filter> </ activity> <activity android: name =" NoteEditor "android: theme = "@ android: style / Theme.Light" android: label = "@ string / title_note" android: screenOrientation = "sensor" android: configChanges = "keyboardHidden | orientation"> <! - This filter says that we can view or edit the data of a single note -> <intent-filter android:label="@string/resolve_edit"> <action android:name="android.intent.action.VIEW" /> <action android: name = "android . intent.action.EDIT "/> <action android:name="com.android.notepad.action.EDIT_NOTE" /> <category android:name="android.intent.category.DEFAULT" /> <data android: mimeType = "vnd.android.cursor.item / vnd.google.note" /> </ intent-filter> <! - This filter says that we can create a new note inside of a directory of notes. -> <intent -filter> <action android:name="android.intent.action.INSERT" /> <category android:name="android.intent.category.DEFAULT" /> <data android: mimeType = "vnd.android.cursor. dir / vnd.google.note "/> </ intent-filter> </ activity> <activity android: name =" TitleEditor "android: label =" @ string / title_edit_title "android: theme =" @ android: style / Theme . Dialog "android: windowSoftInputMode =" stateVisible "> <! - This activity implements an alternative action that can be performed on notes: editing their title. It can be used as a default operation if the user invokes this action, and is available as an alternative action for any note data. -> <intent-filter android:label="@string/resolve_title"> <! - This is the action we perform. It is a custom action we define for our application, not a generic VIEW or EDIT action since we are not a general note viewer / editor. -> <action android:name="com.android.notepad.action.EDIT_TITLE" /> <! - DEFAULT: execute if being directly invoked . -> <category android:name="android.intent.category.DEFAULT" /> <! - ALTERNATIVE: show as an alternative action when the user is working with this type of data. -> <category android: name = "android.intent.category.ALTERNATIVE" /> <! - SELECTED_ALTERNATIVE: show as an alternative action the user can perform when selecting this type of data. -> <category android: name = "android.intent.category . SELECTED_ALTERNATIVE "/> <! - This is the data type we operate on. -> <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> </ intent-filter> </ activity> </ application> </ manifest>

First search procedure
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</ intent-filter>
The Activity component, and in accordance with android: name = "NotesList" and package = "com.example.android.notepad" category to find the fully qualified path com.example.android.notepad.NotesList class, the initial Activity, the implementation of
onCreate (Bundle savedInstanceState) method.

NotesList.java

 package com.example.android.notepad; import com.example.android.notepad.NotePad.Notes; import android.app.ListActivity; import android.content.ComponentName; import android.content.ContentUris; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ContextMenu.ContextMenuInfo; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SimpleCursorAdapter; public class NotesList extends ListActivity (private static final String TAG = &quot;NotesList &quot;; / / Menu item ids public static final int MENU_ITEM_DELETE = Menu.FIRST; / / 1 public static final int MENU_ITEM_INSERT = Menu.FIRST + 1; / / 2 / ** * database columns of interest * / private static final String [] PROJECTION = new String [] (Notes._ID, / / 0 &quot;_id&quot; Notes.TITLE, / / 1 &quot;title&quot;); / ** header column index * / private static final int COLUMN_INDEX_TITLE = 1; @ Override protected void onCreate (Bundle savedInstanceState) (super.onCreate (savedInstanceState); / * * public final void setDefaultKeyMode (int mode) * Set a shortcut key handling * / setDefaultKeyMode (DEFAULT_KEYS_SHORTCUT); / * * create an Intent, if no data is content providers use the default * / Intent intent = getIntent (); if (intent.getData () == null) (intent.setData (Notes.CONTENT_URI);) / * * public ListView getListView () * Registration createContextMenu listener * / getListView (). setOnCreateContextMenuListener (this); / * * define a cursor object * public final Cursor managedQuery (Uri uri *, String [] projection *, String selection *, String [] selectionArgs *, String sortOrder) * execute a query , and the results returned cursor * public Intent getIntent () * Returns to call this component Intent. * projection: PROJECTION out in an array * selection: where * selectionArgs: parameter * sortOrder: Sort * * implementation of a managed query, and Activity component management query closed and re-check. * / Cursor cursor = managedQuery (getIntent (). getData (), PROJECTION, null, null, Notes.DEFAULT_SORT_ORDER); / * * create a simple cursor adapter, binding column View * public SimpleCursorAdapter (Context context *, int layout *, Cursor c *, String [] from *, int [] to) * context: contains the ListView component * layout: the layout of the document to contain * c: Cursor * from : the column array * to: view, id array * mapping from the database to view the log entries * / SimpleCursorAdapter adapter = new SimpleCursorAdapter (this, R.layout.noteslist_item, cursor, new String [] (Notes.TITLE), new int [] (android.R.id.text1)); / * * public void setListAdapter (ListAdapter adapter) * for the ListView to provide the cursor * / setListAdapter (adapter);) / * * Initialize the standard options menu, but only once in each show the first call. * Note, it is called before onPrepareOptionsMenu. * return value is true, then the display menu, or do not appear. * / @ Override public boolean onCreateOptionsMenu (Menu menu) (super.onCreateOptionsMenu (menu); / / This is our one standard application action - inserting a / / new note into the list. menu.add (0, MENU_ITEM_INSERT, 0, R.string.menu_insert). setShortcut (&#39;3 &#39;,&#39; a &#39;). setIcon (android.R . drawable.ic_menu_add); / / Generate any additional actions that can be performed on the / / overall list. In a normal install, there are no additional / / actions found here, but this allows other applications to extend / / our menu with their own actions. Intent intent = new Intent (null, getIntent (). getData ()); intent.addCategory (Intent.CATEGORY_ALTERNATIVE); menu.addIntentOptions (Menu.CATEGORY_ALTERNATIVE, 0, 0, new ComponentName (this, NotesList.class ), null, intent, 0, null); return true;) / * * ready to display the standard options. Each menu is displayed before the call. This method can enable / disable menu items or dynamically modify the contents of the menu. * default under the Activity Status update the system menu item. derived class must call the base class implementation. * return value is true, then the display menu, or do not appear. * / @ Override public boolean onPrepareOptionsMenu (Menu menu) (super.onPrepareOptionsMenu (menu); final boolean haveItems = getListAdapter (). getCount ()&gt; 0; / / If there are any notes in the list (which implies that one of / / them is selected), then we need to generate the actions that / / can be performed on the current selection. This will be a combination / / of our own specific actions along with any extensions that can be / / found. if (haveItems) (/ / This is the selected item. Uri uri = ContentUris.withAppendedId (getIntent (). getData (), getSelectedItemId ()); / / Build menu ... always starts with the EDIT action ... Intent [] specifics = new Intent [1]; specifics [0] = new Intent (Intent.ACTION_EDIT, uri) ; MenuItem [] items = new MenuItem [1]; / / ... is followed by whatever other actions are available ... Intent intent = new Intent (null, uri); intent.addCategory (Intent.CATEGORY_ALTERNATIVE); menu. addIntentOptions (Menu.CATEGORY_ALTERNATIVE, 0, 0, null, specifics, intent, 0, items); / / Give a shortcut to the edit action. if (items [0]! = null) (items [0]. setShortcut ( &#39; 1 &#39;,&#39; e &#39;);)) else (menu.removeGroup (Menu.CATEGORY_ALTERNATIVE);) return true;) / * * Options menu click the menu item callback * / @ Override public boolean onOptionsItemSelected (MenuItem item) (switch (item.getItemId ()) (case MENU_ITEM_INSERT: / / Launch activity to insert a new item startActivity (new Intent (Intent.ACTION_INSERT, getIntent (). getData ())); return true;) return super.onOptionsItemSelected ( item);) / * * display the context menu each time before the call. * / @ Override public void onCreateContextMenu (ContextMenu menu, View view, ContextMenuInfo menuInfo) (AdapterView.AdapterContextMenuInfo info; try (info = (AdapterView.AdapterContextMenuInfo) menuInfo;) catch (ClassCastException e) (Log.e (TAG, &quot;bad menuInfo&quot;, e); return;) Cursor cursor = (Cursor) getListAdapter (). getItem (info.position); if (cursor == null) (/ / For some reason the requested item isn&#39;t available, do nothing return;) / / Setup the menu header menu.setHeaderTitle (cursor.getString (COLUMN_INDEX_TITLE)); / / Add a menu item to delete the note menu.add (0, MENU_ITEM_DELETE, 0, R.string.menu_delete);) / * * the context menu menu item click callback * / @ Override public boolean onContextItemSelected (MenuItem item) (AdapterView.AdapterContextMenuInfo info; try (info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo ();) catch (ClassCastException e) (Log.e (TAG, &quot;bad menuInfo&quot;, e); return false;) switch (item.getItemId ()) (case MENU_ITEM_DELETE: (/ / Delete the note that the context menu is for Uri noteUri = ContentUris.withAppendedId (getIntent (). getData (), info.id); getContentResolver (). delete (noteUri, null, null); return true;)) return false;) / * * ListView list item click event callback * / @ Override protected void onListItemClick (ListView l, View v, int position, long id) (Uri uri = ContentUris.withAppendedId (getIntent (). getData (), id); String action = getIntent (). getAction (); if (Intent.ACTION_PICK.equals (action) | | Intent.ACTION_GET_CONTENT.equals (action)) (/ / The caller is waiting for us to return a note selected by / / the user. The have clicked on one, so return it now. setResult (RESULT_OK, new Intent (). setData (uri));) else (/ / Launch activity to view / edit the currently selected item startActivity (new Intent (Intent.ACTION_EDIT, uri ));)))

Menu Category:
Context Menu: no support for shortcuts and icons.
Options Menu: icon menu does not support the check mark, and the compact display the menu title. A maximum of six, the rest appear more.
Sub-Menu: does not support the icon, can not be nested submenu.

Permalink: Code Library » https://www.ucosoft.com/android-examples-notepad-2.html

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *