Android Open Source - Books Settings Activity






From Project

Back to project page Books.

License

The source code is released under:

Apache License

If you think the Android project Books 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.contender.books;
//w w w  . jav a 2s . co m
import android.app.Activity;
import android.app.AlertDialog;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * Implements a View for changing user preferences.
 * <p>
 * Loads preferences(MainView.PREFS*) from the SharedPreferences data
 * and displays them in the UI, for the user to manipulate and save.
 * <p>
 * Currently supports settings for 'Default loan period' and
 * 'Sort order in ListViews'
 *  
 * @author Paul Klinkenberg <pklinken@gmail.com>
 * @version {@value com.contender.books.MainView#BOOKS_VERSION}
 * @see SharedPreferences
 */
public class SettingsActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
  
    // Populate spinners
    Spinner spinner = (Spinner) findViewById(R.id.period_spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,  
        R.array.period_spinner_array,  android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
        
    SharedPreferences settings = this.getSharedPreferences(MainView.PREFS_NAME, 0);
    TextView view = (TextView) findViewById(R.id.edit_period);
    int periodLength = settings.getInt(MainView.PREFS_PERIOD_LENGTH, 1);
    int periodUnit = settings.getInt(MainView.PREFS_PERIOD_UNIT, 0);
    view.setText(String.valueOf(periodLength));
    spinner.setSelection(periodUnit);
    
    spinner = (Spinner) findViewById(R.id.sortorder_spinner);
    adapter = ArrayAdapter.createFromResource(this,  R.array.sortorder_spinner_array,
        android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    int sortOrder = settings.getInt(MainView.PREFS_SORT_ORDER, 0);
    spinner.setSelection(sortOrder);
  }
  
  /**
   * Initializes the View, retrieving all preferences data and setting up the UI
   * to reflect this.
   */
  @Override
  public void onResume() {
    super.onResume();
    
  
    
  }
  
  
/** 
 * Reads the preferences from all the UI elements and stores them in the SharedPreferences
 * <p>
 * In case of out of bounds input this function resorts to saving default values without giving
 * an error message.
 * 
 * @param view View that called this function.
 * @see SharedPreferences
 */
  public void saveSettings(View view) {
    
    SharedPreferences settings = this.getSharedPreferences(MainView.PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();
    
    TextView periodLengthView = (TextView) findViewById(R.id.edit_period);
    int periodLength = Integer.valueOf(periodLengthView.getText().toString());
    if(periodLength < 1 || periodLength > 100) {
      // Outside of reasonable limits, default to 4
      periodLength = 4;
    }
    Spinner spinner = (Spinner) findViewById(R.id.period_spinner);
    int periodUnit = spinner.getSelectedItemPosition();
    if(periodUnit == AdapterView.INVALID_POSITION) {
      // Invalid position from spinner, default to 1 (Weeks)
      periodUnit = 1;
    }
    editor.putInt(MainView.PREFS_PERIOD_LENGTH, periodLength);
    editor.putInt(MainView.PREFS_PERIOD_UNIT, periodUnit);
    
    spinner = (Spinner) findViewById(R.id.sortorder_spinner);
    int sortOrder = spinner.getSelectedItemPosition();
    // 0 = duedate, 1 = bookname, 2=contact ...
    editor.putInt(MainView.PREFS_SORT_ORDER, sortOrder);
        
    editor.commit();
    finish();
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.settings, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    switch(item.getItemId()) {
    case R.id.action_about:
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setTitle(R.string.about_title);
      Resources res = getResources();
      String aboutMessage = res.getString(R.string.about_message, MainView.BOOKS_VERSION);
      builder.setMessage(aboutMessage);
      AlertDialog aboutDialog = builder.create();
      aboutDialog.show();
      TextView view = (TextView) aboutDialog.findViewById(android.R.id.message);
      view.setTextSize(12);
      return true;
    }
    return super.onOptionsItemSelected(item);
  }

}




Java Source Code List

com.contender.books.BookActivity.java
com.contender.books.BooksStorage.java
com.contender.books.DatePickerFragment.java
com.contender.books.EditOrDeleteDialogFragment.java
com.contender.books.HistoryActivity.java
com.contender.books.MainView.java
com.contender.books.ScanOrManualDialogFragment.java
com.contender.books.SettingsActivity.java
com.contender.books.SpecialCursorAdapter.java
com.google.zxing.integration.android.IntentIntegrator.java
com.google.zxing.integration.android.IntentResult.java