66import android .content .SharedPreferences .Editor ;
77import android .os .Bundle ;
88import android .preference .PreferenceManager ;
9+ import android .view .ContextMenu ;
10+ import android .view .ContextMenu .ContextMenuInfo ;
911import android .view .Menu ;
1012import android .view .MenuInflater ;
1113import android .view .MenuItem ;
1214import android .view .View ;
1315import android .view .View .OnClickListener ;
1416import android .widget .Button ;
17+ import android .widget .EditText ;
1518import android .widget .Toast ;
1619
1720public class HelloPreferences extends Activity {
21+ private static final int MENU_SECOND = 1 ;
22+ private static final int MENU_FIRST = 0 ;
1823 SharedPreferences preferences ;
1924
2025 /** Called when the activity is first created. */
2126 @ Override
2227 public void onCreate (Bundle savedInstanceState ) {
2328 super .onCreate (savedInstanceState );
2429 setContentView (R .layout .main );
30+
31+ EditText text = (EditText ) findViewById (R .id .contextmenu );
32+ registerForContextMenu (text );
2533 Button button = (Button ) findViewById (R .id .Button01 );
2634 // Initialize preferences
2735 preferences = PreferenceManager .getDefaultSharedPreferences (this );
@@ -50,6 +58,11 @@ public void onClick(View v) {
5058 }
5159 edit .putString ("username" , buffer .toString ());
5260 edit .commit ();
61+ // A toast is a view containing a quick little message for the
62+ // user. We give a little feedback
63+ Toast .makeText (HelloPreferences .this ,
64+ "Reverted string sequence of user name." ,
65+ Toast .LENGTH_LONG ).show ();
5366 }
5467 });
5568 }
@@ -70,7 +83,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
7083 // Launch Preference activity
7184 Intent i = new Intent (HelloPreferences .this , Preferences .class );
7285 startActivity (i );
73- // A toast is a view containing a quick little message for the user.
86+ // Some feedback to the user
7487 Toast .makeText (HelloPreferences .this ,
7588 "Here you can maintain your user credentials." ,
7689 Toast .LENGTH_LONG ).show ();
@@ -79,4 +92,30 @@ public boolean onOptionsItemSelected(MenuItem item) {
7992 }
8093 return true ;
8194 }
95+
96+ @ Override
97+ public void onCreateContextMenu (ContextMenu menu , View v ,
98+ ContextMenuInfo menuInfo ) {
99+ // We could make a distinction if several views would have registered a
100+ // context menu. We could also use a XML file but this time we do it
101+ // manually
102+ menu .add (Menu .NONE , MENU_FIRST , Menu .NONE , "Say hello" );
103+ menu .add (Menu .NONE , MENU_SECOND , Menu .NONE , "Another option" );
104+ }
105+
106+ @ Override
107+ public boolean onContextItemSelected (MenuItem item ) {
108+ switch (item .getItemId ()) {
109+ case MENU_FIRST :
110+ Toast .makeText (this , "First option selected" , Toast .LENGTH_SHORT )
111+ .show ();
112+ break ;
113+ case MENU_SECOND :
114+ Toast .makeText (this , "Second option selected" , Toast .LENGTH_SHORT )
115+ .show ();
116+ break ;
117+
118+ }
119+ return super .onContextItemSelected (item );
120+ }
82121}
0 commit comments