Android Open Source - CITA330_Android_Example_Preferences User Preferences Test App Activity






From Project

Back to project page CITA330_Android_Example_Preferences.

License

The source code is released under:

Apache License

If you think the Android project CITA330_Android_Example_Preferences 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

/*
 * Copyright (C) 2012 Scott M. Everts Greysky Software.
 * /* www.j  a v a  2s .  c om*/
 * 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
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * 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.
 */
package com.greyskysoftware.android.demos;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class UserPreferencesTestAppActivity extends Activity {
  private SharedPreferences preferences;
  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button) findViewById(R.id.button1);
    // Initialize preferences
    preferences = PreferenceManager.getDefaultSharedPreferences(this);

    button.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        String username = preferences.getString("username", "n/a");
        String password = preferences.getString("password", "n/a");
        showPrefs(username, password);
      }
    });

    Button buttonChangePreferences = (Button) findViewById(R.id.button2);
    buttonChangePreferences.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        
        updatePreferenceValue();
      }
    });
    }
    
    @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mainmenu, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // We have only one menu option
    case R.id.preferences:
      // Launch Preference activity
      Intent i = new Intent(UserPreferencesTestAppActivity.this, MyPreferencesActivity.class);
      startActivity(i);
      // Some feedback to the user
      Toast.makeText(UserPreferencesTestAppActivity.this, "Enter your user credentials.",
        Toast.LENGTH_LONG).show();
      break;

    }
    return true;
    
  }    
    
    private void showPrefs(String username, String password){
    Toast.makeText(
        UserPreferencesTestAppActivity.this,
        "Input: " + username + " and password: "
            + password, Toast.LENGTH_LONG).show();

  }
  
  private void updatePreferenceValue(){
    Editor edit = preferences.edit();
    String username = preferences.getString("username", "n/a");
    // We will just revert the current user name and save again
    StringBuffer buffer = new StringBuffer();
    for (int i = username.length() - 1; i >= 0; i--) {
      buffer.append(username.charAt(i));
    }
    edit.putString("username", buffer.toString());
    edit.commit();
    // A toast is a view containing a quick little message for the
    // user. We give a little feedback
    Toast.makeText(UserPreferencesTestAppActivity.this,
        "Reverted string sequence of user name.",
        Toast.LENGTH_LONG).show();
  }

    
}




Java Source Code List

com.greyskysoftware.android.demos.MyPreferencesActivity.java
com.greyskysoftware.android.demos.UserPreferencesTestAppActivity.java