Android Open Source - StrTrainer_android App Settings_ Modify Increment Values






From Project

Back to project page StrTrainer_android.

License

The source code is released under:

GNU General Public License

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

/**
 *//from w w w  .j ava  2s.com
 * StrTrainer /com/davefarinelli/strtrainer/ModifyIncrementValues.java
 *
 * Copyright (c) 2011 Dave Farinelli
 *
 * LICENSE:
 *
 * This file is part of StrTrainer, a companion Android app for the Starting Strength weight
 * training program (http://davefarinelli.com/developer/android).
 *
 * StrTrainer is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
 * later version.
 *
 * StrTrainer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with StrTrainer.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * @author Dave Farinelli <davefarinelli[at]gmail[dot]com>
 * @license http://www.gnu.org/licenses/gpl.html
 * @copyright 2011 Dave Farinelli
 */

package com.davefarinelli.strtrainer;

import com.davefarinelli.strtrainer.R;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class AppSettings_ModifyIncrementValues extends Activity {
  /** Widgets in the app */
  private Spinner spinnerSquat;
  private Spinner spinnerBench;
  private Spinner spinnerPress;
  private Spinner spinnerDeadlift;
  private Spinner spinnerClean;
  
  /** Declaration of Shared Preference name */
  public static final String PREFS_NAME = "MyPrefsFile";
  
  //Create widget handles
  EditText etSquatIncrementValue;
  EditText etBenchIncrementValue;
  EditText etPressIncrementValue;
  EditText etDeadliftIncrementValue;
  EditText etCleanIncrementValue;
  
  // Create increment global variables
  float squatIncrementValue;
  float benchIncrementValue;
  float pressIncrementValue;
  float deadliftIncrementValue;
  float cleanIncrementValue;
  
  /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_inc_values);
        
        // Restore preferences
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        squatIncrementValue = settings.getFloat("squatIncrementValue", (float) 5.0);
        benchIncrementValue = settings.getFloat("benchIncrementValue", (float) 5.0);
        pressIncrementValue = settings.getFloat("pressIncrementValue", (float) 5.0);
        deadliftIncrementValue = settings.getFloat("deadliftIncrementValue", (float) 10.0);
        cleanIncrementValue = settings.getFloat("cleanIncrementValue", (float) 5.0);
        
        /** Initialize variables for widget handles */
        spinnerSquat = (Spinner)findViewById(R.id.spinner_squat);
        spinnerBench = (Spinner)findViewById(R.id.spinner_bench);
        spinnerPress = (Spinner)findViewById(R.id.spinner_press);
        spinnerDeadlift = (Spinner)findViewById(R.id.spinner_deadlift);
        spinnerClean = (Spinner)findViewById(R.id.spinner_clean);
        
        //Set up adapter for all spinners
      ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
              this, R.array.increments_array, android.R.layout.simple_spinner_item);
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      
      // Set adapter to all spinner widgets
      spinnerSquat.setAdapter(adapter);
      spinnerBench.setAdapter(adapter);
      spinnerPress.setAdapter(adapter);
      spinnerDeadlift.setAdapter(adapter);
      spinnerClean.setAdapter(adapter);
      
      // Set spinners to correct values from Shared Preference
      setSpinnerToCorrectValue(spinnerSquat, squatIncrementValue);
      setSpinnerToCorrectValue(spinnerBench, benchIncrementValue);
      setSpinnerToCorrectValue(spinnerPress, pressIncrementValue);
      setSpinnerToCorrectValue(spinnerDeadlift, deadliftIncrementValue);
      setSpinnerToCorrectValue(spinnerClean, cleanIncrementValue);
      
      /** Sets OnItemSelected listeners for all spinners */
      spinnerSquat.setOnItemSelectedListener(mOnItemSelectedListener);
      spinnerBench.setOnItemSelectedListener(mOnItemSelectedListener);
      spinnerPress.setOnItemSelectedListener(mOnItemSelectedListener);
      spinnerDeadlift.setOnItemSelectedListener(mOnItemSelectedListener);
      spinnerClean.setOnItemSelectedListener(mOnItemSelectedListener);
    }
    
    private void setSpinnerToCorrectValue(Spinner spinner, float incrementValue) {
    if (incrementValue == 1)
      spinner.setSelection(0);
    else if (incrementValue == 2)
      spinner.setSelection(1);
    else if (incrementValue == 2.5)
      spinner.setSelection(2);
    else if (incrementValue == 5)
      spinner.setSelection(3);
    else if (incrementValue == 10)
      spinner.setSelection(4);
    else if (incrementValue == 15)
      spinner.setSelection(5);
    else if (incrementValue == 20)
      spinner.setSelection(6);
  }

  private OnItemSelectedListener mOnItemSelectedListener = new OnItemSelectedListener() {
      public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
      {
      String coreLift;
      
      switch(parent.getId())
      {
        case R.id.spinner_squat:
          coreLift = "Squat";
          saveIncrementValue("squatIncrementValue", translateSpinnerSelection(pos));
          break;
        case R.id.spinner_bench:
          coreLift = "Bench";
          saveIncrementValue("benchIncrementValue", translateSpinnerSelection(pos));
          break;
        case R.id.spinner_press:
          coreLift = "Press";
          saveIncrementValue("pressIncrementValue", translateSpinnerSelection(pos));
          break;
        case R.id.spinner_deadlift:
          coreLift = "Deadlift";
          saveIncrementValue("deadliftIncrementValue", translateSpinnerSelection(pos));
          break;
        case R.id.spinner_clean:
          coreLift = "Clean";
          saveIncrementValue("cleanIncrementValue", translateSpinnerSelection(pos));
          break;
      }
      
      // Report change in increment value
      // removed this since it would report at beginning without being requested.
      
      // Toast.makeText(parent.getContext(), coreLift + " increment weight is set to " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
      }

      private float translateSpinnerSelection(int pos) {
        if (pos == 0)
        return (float) 1;
        else if (pos == 1)
        return (float) 2;
        else if (pos == 2)
        return (float) 2.5;
        else if (pos == 3)
        return (float) 5;
        else if (pos == 4)
        return (float) 10;
        else if (pos == 5)
        return (float) 15;
        else if (pos == 6)
        return (float) 20;
        
      return 0;
    }

    private void saveIncrementValue(String SharedPreferenceName, float incrementValue) {
        //Save Work Set Value weight into memory
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putFloat(SharedPreferenceName, incrementValue);

        // Commit the edits!
        editor.commit();
    }

    public void onNothingSelected(AdapterView<?> parent) {
        // Do nothing.
      }
  };
}




Java Source Code List

com.davefarinelli.strtrainer.About.java
com.davefarinelli.strtrainer.AppSettings_InitializeWorkSetValues.java
com.davefarinelli.strtrainer.AppSettings_ModifyIncrementValues.java
com.davefarinelli.strtrainer.AppSettings.java
com.davefarinelli.strtrainer.ComputeTab.java
com.davefarinelli.strtrainer.Compute.java
com.davefarinelli.strtrainer.Records.java
com.davefarinelli.strtrainer.StrTrainer.java
com.davefarinelli.strtrainer.Utilities_Estimate1RM.java
com.davefarinelli.strtrainer.Utilities.java