Android Open Source - introToDroid4ed Super Simple Preferences Activity






From Project

Back to project page introToDroid4ed.

License

The source code is released under:

GNU General Public License

If you think the Android project introToDroid4ed listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.introtoandroid.simplepreferences;
/*  ww  w  . j a  va 2  s .  co m*/

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public abstract class SuperSimplePreferencesActivity extends Activity {
  public static final String PREFERENCE_FILENAME = "AppPrefs";
  public static final String PREFERENCE_STRING_NAME = "StringPrefActivity";
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main);
        
        // Spit out the current preferences
        SharedPreferences settings = getSharedPreferences(PREFERENCE_FILENAME, 0); 
        SharedPreferences settingsActivity = getPreferences(MODE_PRIVATE); 

        // Show the activity name  
        final TextView activityName = (TextView)findViewById(R.id.Title);
        activityName.setText(this.getLocalClassName());
        
        // Show the shared preferences 
        final TextView prefs = (TextView)findViewById(R.id.CurrentPrefs);
        prefs.setText(settings.getAll().toString());
        
        // Set the private activity preferences 
        final TextView prefsAct = (TextView)findViewById(R.id.CurrentActivityPrefs);
        prefsAct.setText(settingsActivity.getAll().toString());
        
        // Move to other activity
        final Button goButton = (Button) findViewById(R.id.ButtonGo);         
        goButton.setOnClickListener(new View.OnClickListener() {             
          public void onClick(View v) {                     
            // Go to the Main screen
            Intent intent = new Intent(SuperSimplePreferencesActivity.this, GetTargetClass());
            startActivity(intent);
          }         
        });
        
        // HANDLE ACTIVITY PREFERENCE BUTTONS
        // Handle add/update activity preference button clicks
        final Button prefAddActButton = (Button) findViewById(R.id.ButtonAddActivityPref);         
        prefAddActButton.setOnClickListener(new View.OnClickListener() {             
          public void onClick(View v) {                 
            final EditText prefName = (EditText)findViewById(R.id.EditTextPrefName);
            final EditText prefValue = (EditText)findViewById(R.id.EditTextPrefValue);
            final TextView prefs = (TextView)findViewById(R.id.CurrentActivityPrefs);
            
            // Get the application settings and open the editor
                SharedPreferences settings = getPreferences(0);
                SharedPreferences.Editor prefEditor = settings.edit();
                
                String strPrefName = prefName.getText().toString();
                String strPrefValue = prefValue.getText().toString();
                               
                // Add the preference and commit the changes
                prefEditor.putString(strPrefName, strPrefValue);
                prefEditor.commit();
                
                // Update the screen with all settings
            prefs.setText(settings.getAll().toString()); 
          }         
        });
        
        // Handle clearing a single activity preference by name clicks
        final Button clearActPrefByNameButton = (Button) findViewById(R.id.ButtonClearActPrefByName);         
        clearActPrefByNameButton.setOnClickListener(new View.OnClickListener() {             
          public void onClick(View v) {                 
  
            final EditText prefName = (EditText)findViewById(R.id.EditTextPrefName);
            final TextView prefs = (TextView)findViewById(R.id.CurrentActivityPrefs);
            
            // Get the application settings and open the editor
                SharedPreferences settings = getPreferences(0);
                SharedPreferences.Editor prefEditor = settings.edit();
                
                String strPrefName = prefName.getText().toString();
                
                // Remove the preference if it exists 
                if(settings.contains(strPrefName)) {
                  // Remove the existing preference by the same name
                  prefEditor.remove(strPrefName);
                }
                
                // Commit our changes and update the display
                prefEditor.commit();
            prefs.setText(settings.getAll().toString()); 
          }         
        });

        // Handle clearing preference button clicks
        final Button clearButton = (Button) findViewById(R.id.ButtonClearAct);         
        clearButton.setOnClickListener(new View.OnClickListener() {             
          public void onClick(View v) {                 
            final TextView prefs = (TextView)findViewById(R.id.CurrentActivityPrefs);
            
            // Get the application settings and open the editor
                SharedPreferences settings = getPreferences(0);
                SharedPreferences.Editor prefEditor = settings.edit();
                                
                // Clear all the preferences
                prefEditor.clear();
                prefEditor.commit();
                
                // Update the screen
            prefs.setText(settings.getAll().toString()); 

            // Also tell the user specifically what we did
            Toast.makeText(SuperSimplePreferencesActivity.this, "Activity Preferences Cleared", Toast.LENGTH_SHORT).show();
          }         
        });
        
        // HANDLE SHARED PREFS BUTTONS
        // Handle add/update preference button clicks
        final Button prefButton = (Button) findViewById(R.id.ButtonAddSharedPref);         
        prefButton.setOnClickListener(new View.OnClickListener() {             
          public void onClick(View v) {                 
  
     
            final EditText prefName = (EditText)findViewById(R.id.EditTextPrefName);
            final EditText prefValue = (EditText)findViewById(R.id.EditTextPrefValue);
            final TextView prefs = (TextView)findViewById(R.id.CurrentPrefs);
            
            // Get the application settings and open the editor
                SharedPreferences settings = getSharedPreferences(PREFERENCE_FILENAME, 0);
                SharedPreferences.Editor prefEditor = settings.edit();
                
                String strPrefName = prefName.getText().toString();
                String strPrefValue = prefValue.getText().toString();
                               
                // Add the preference and commit the changes
                prefEditor.putString(strPrefName, strPrefValue);
                prefEditor.commit();
                
                // Update the screen with all settings
            prefs.setText(settings.getAll().toString()); 

          }         
        });
        
        // Handle clearing a single preference by name clicks
        final Button clearPrefByNameButton = (Button) findViewById(R.id.ButtonClearSharedPrefByName);         
        clearPrefByNameButton.setOnClickListener(new View.OnClickListener() {             
          public void onClick(View v) {                 
  
            final EditText prefName = (EditText)findViewById(R.id.EditTextPrefName);
            final TextView prefs = (TextView)findViewById(R.id.CurrentPrefs);
            
            // Get the application settings and open the editor
                SharedPreferences settings = getSharedPreferences(PREFERENCE_FILENAME, 0);
                SharedPreferences.Editor prefEditor = settings.edit();
                
                String strPrefName = prefName.getText().toString();
                
                // Remove the preference if it exists 
                if(settings.contains(strPrefName)) {
                  // Remove the existing preference by the same name
                  prefEditor.remove(strPrefName);
                }
                
                // Commit our changes and update the display
                prefEditor.commit();
            prefs.setText(settings.getAll().toString()); 
          }         
        });
        
        // Handle clearing preference button clicks
        final Button clearSharedButton = (Button) findViewById(R.id.ButtonClearShared);         
        clearSharedButton.setOnClickListener(new View.OnClickListener() {             
          public void onClick(View v) {
            final TextView prefs = (TextView)findViewById(R.id.CurrentPrefs);
            
            // Get the application settings and open the editor
                SharedPreferences settings = getSharedPreferences(PREFERENCE_FILENAME, 0);
                SharedPreferences.Editor prefEditor = settings.edit();
                                
                // Clear all the preferences
                prefEditor.clear();
                prefEditor.commit();
                
                // Update the screen
            prefs.setText(settings.getAll().toString()); 

            // Also tell the user specifically what we did
            Toast.makeText(SuperSimplePreferencesActivity.this, "Shared Preferences Cleared", Toast.LENGTH_SHORT).show();
          }         
        });
        
        // Register to be notified whenever preferences change
        settings.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() {             
          public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {   
            // Tell the user what changed
            Toast.makeText(SuperSimplePreferencesActivity.this, "Preference Changed: "+key, Toast.LENGTH_SHORT).show();
          }         
        });
  }
  
  abstract Class<?> GetTargetClass();
}




Java Source Code List

com.introtoandroid.advancedlayouts.AdaptersActivity.java
com.introtoandroid.advancedlayouts.AdvancedLayoutsActivity.java
com.introtoandroid.advancedlayouts.BasicLayoutActivity.java
com.introtoandroid.advancedlayouts.ContactAdapterActivity.java
com.introtoandroid.advancedlayouts.DialogActivity.java
com.introtoandroid.advancedlayouts.GridAdapterSampleActivity.java
com.introtoandroid.advancedlayouts.GridLayoutActivity.java
com.introtoandroid.advancedlayouts.GridListMenuActivity.java
com.introtoandroid.advancedlayouts.ListAdapterSampleActivity.java
com.introtoandroid.advancedlayouts.MenuActivity.java
com.introtoandroid.advancedlayouts.MyListActivity.java
com.introtoandroid.advancedlayouts.StyleSamplesActivity.java
com.introtoandroid.filesoc.FileStreamOfConsciousnessActivity.java
com.introtoandroid.filesoc.ViewLogActivity.java
com.introtoandroid.myfirstandroidapp.MyFirstAndroidAppActivity.java
com.introtoandroid.navigation.FirstChildActivity.java
com.introtoandroid.navigation.ParentActivity.java
com.introtoandroid.navigation.SecondChildActivity.java
com.introtoandroid.navigation.ThirdChildActivity.java
com.introtoandroid.parisview.ParisViewActivity.java
com.introtoandroid.passwordmatcher.PasswordMatcherActivity.java
com.introtoandroid.resourceroundup.ResourceRoundupActivity.java
com.introtoandroid.samelayout.MenuActivity.java
com.introtoandroid.samelayout.ProgrammaticLayoutActivity.java
com.introtoandroid.samelayout.ResourceLayoutActivity.java
com.introtoandroid.samelayout.SameLayoutActivity.java
com.introtoandroid.simpleactionbar.SimpleActionBarActivity.java
com.introtoandroid.simplealtresources.SimpleAltResourcesActivity.java
com.introtoandroid.simplecontacts.SimpleContactsActivity.java
com.introtoandroid.simplecontentprovider.MenuActivity.java
com.introtoandroid.simplecontentprovider.SimpleBookmarks.java
com.introtoandroid.simplecontentprovider.SimpleCallLog.java
com.introtoandroid.simplecontentprovider.SimpleContentProviderMenuActivity.java
com.introtoandroid.simplecontentprovider.SimpleMediaStore.java
com.introtoandroid.simplefiles.SimpleFilesActivity.java
com.introtoandroid.simplefragdialogs.SimpleFragDialogActivity.java
com.introtoandroid.simplefragments.FieldNoteListFragment.java
com.introtoandroid.simplefragments.FieldNoteViewActivity.java
com.introtoandroid.simplefragments.FieldNoteWebViewFragment.java
com.introtoandroid.simplefragments.SimpleFragmentsActivity.java
com.introtoandroid.simplelayout.FrameLayoutActivity.java
com.introtoandroid.simplelayout.GridLayoutActivity.java
com.introtoandroid.simplelayout.LinearLayoutActivity.java
com.introtoandroid.simplelayout.MenuActivity.java
com.introtoandroid.simplelayout.MultipleLayoutActivity.java
com.introtoandroid.simplelayout.RelativeLayoutActivity.java
com.introtoandroid.simplelayout.SimpleLayoutActivity.java
com.introtoandroid.simplelayout.TableLayoutActivity.java
com.introtoandroid.simplemultimedia.AudioActivity.java
com.introtoandroid.simplemultimedia.MenuActivity.java
com.introtoandroid.simplemultimedia.SimpleMultimediaActivity.java
com.introtoandroid.simplemultimedia.StillImageActivity.java
com.introtoandroid.simplemultimedia.VideoPlayActivity.java
com.introtoandroid.simplepreferences.MoreSimplePreferencesActivity.java
com.introtoandroid.simplepreferences.SimplePreferencesActivity.java
com.introtoandroid.simplepreferences.SuperSimplePreferencesActivity.java
com.introtoandroid.simpleresourceview.SimpleResourceViewActivity.java
com.introtoandroid.simplescrolling.BothScrollActivity.java
com.introtoandroid.simplescrolling.HorizontalScrollActivity.java
com.introtoandroid.simplescrolling.MenuActivity.java
com.introtoandroid.simplescrolling.NoScrollActivity.java
com.introtoandroid.simplescrolling.SimpleScrollingActivity.java
com.introtoandroid.simplescrolling.VerticalScrollActivity.java
com.introtoandroid.simpleuserprefs.SimpleUserPrefsActivity.java
com.introtoandroid.supportfragdialog.MyAlertDialogFragment.java
com.introtoandroid.supportfragdialog.SupportFragDialogActivity.java
com.introtoandroid.userprefsheaders.UserPrefsActivity.java
com.introtoandroid.viewsamples.ButtonsActivity.java
com.introtoandroid.viewsamples.ContainersActivity.java
com.introtoandroid.viewsamples.EventsActivity.java
com.introtoandroid.viewsamples.FormsActivity.java
com.introtoandroid.viewsamples.IndicatorsActivity.java
com.introtoandroid.viewsamples.MenuActivity.java
com.introtoandroid.viewsamples.PickersActivity.java
com.introtoandroid.viewsamples.TextDisplayActivity.java
com.introtoandroid.viewsamples.TextInputActivity.java
com.introtoandroid.viewsamples.ViewSampleActivity.java