myworld
myworld

Reputation: 23

android application save and load value with button

I have programmed a BMI Calculator and now I want to save and load the weight and height value with a button, does anyone know how to do this?

package com.example.test.bmicalculator;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;

import android.widget.EditText;
import android.widget.TextView;

public class MyActivity extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
    }


    public void calculateClickHandler(View view) {
        // make sure we handle the click of the calculator button

        if (view.getId() == R.id.calculateButton) {

            // get the references to the widgets
            EditText weightText = (EditText)findViewById(R.id.weightText);
            EditText heightText = (EditText)findViewById(R.id.heightText);
            TextView resultText = (TextView)findViewById(R.id.resultLabel);

            // get the users values from the widget references

            float weight = Float.parseFloat(weightText.getText().toString());
            float height = Float.parseFloat(heightText.getText().toString());

            // calculate the bmi value

            float bmiValue = calculateBMI(weight, height);

            // interpret the meaning of the bmi value
            String bmiInterpretation = interpretBMI(bmiValue);

            // now set the value in the result text

            resultText.setText(bmiValue + "-" + bmiInterpretation);

        }


    }

    // the formula to calculate the BMI index

    // check for http://en.wikipedia.org/wiki/Body_mass_index
    private float calculateBMI (float weight, float height) {

        return (float) (weight * 4.88 / (height * height));
    }


    // interpret what BMI means
    private String interpretBMI(float bmiValue) {

        if (bmiValue < 16) {
            return "Severely underweight";
        } else if (bmiValue < 18.5) {

            return "Underweight";
        } else if (bmiValue < 25) {

            return "Normal";
        } else if (bmiValue < 30) {

            return "Overweight";
        } else {
            return "Obese";
        }

    }




    public void SaveClickHandler(View view) {

        // here I want to Save the weight and the height value
    }


    public void LoadClickHandler(View view) {
        // here I want to Load the weight and the height value
    }

}

Upvotes: 1

Views: 129

Answers (2)

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20626

You can do it with SharedPreferences as follows :

public void SaveClickHandler(View view) {

    SharedPreferences prefs = getSharedPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putFloat("height", height); 
    editor.putFloat("weight", weight);
    editor.commit();
}

And when you want to get this value you do this :

public void LoadClickHandler(View view) {

    float height = prefs.getFloat("height", 0.0f);  //Default value (0.0f)
    float weight= prefs.getFloat("weight", 0.0f);   //Default value (0.0f)
    //Do more stuff 
}

Edit

Make those height and weight as a global variables as follows :

float height, weight;

Then in your onCreate() you do this :

weight = Float.parseFloat(weightText.getText().toString());
height = Float.parseFloat(heightText.getText().toString());

And add this code in your LoadClickHandler()

public void LoadClickHandler(View view) {

    float heightSaved = prefs.getFloat("height", 0.0f);  //Default value (0.0f)
    float weightSaved= prefs.getFloat("weight", 0.0f);   //Default value (0.0f)
    //Do more stuff 
}

Change this :

SharedPreferences prefs = getSharedPreferences(Context.MODE_PRIVATE);

to this :

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(view.getContext());

FINAL CODE

public class MyActivity extends Activity {
float weight, height;
SharedPreferences prefs;
EditText weightText, heightText;
TextView resultText;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
     prefs = PreferenceManager.getDefaultSharedPreferences(this);
    // get the references to the widgets
    weightText = (EditText)findViewById(R.id.weightText);
    heightText = (EditText)findViewById(R.id.heightText);
    resultText = (TextView)findViewById(R.id.resultLabel);
}

public void calculateClickHandler(View view) {
    // make sure we handle the click of the calculator button

    if (view.getId() == R.id.calculateButton) {
        // get the users values from the widget references
        weight = Float.parseFloat(weightText.getText().toString());
        height = Float.parseFloat(heightText.getText().toString());

        // calculate the bmi value

        float bmiValue = calculateBMI(weight, height);

        // interpret the meaning of the bmi value
        String bmiInterpretation = interpretBMI(bmiValue);

        // now set the value in the result text

        resultText.setText(bmiValue + "-" + bmiInterpretation);

    }


}

// the formula to calculate the BMI index

// check for http://en.wikipedia.org/wiki/Body_mass_index
private float calculateBMI (float weight, float height) {

    return (float) (weight * 4.88 / (height * height));
}


// interpret what BMI means
private String interpretBMI(float bmiValue) {

    if (bmiValue < 16) {
        return "Severely underweight";
    } else if (bmiValue < 18.5) {

        return "Underweight";
    } else if (bmiValue < 25) {

        return "Normal";
    } else if (bmiValue < 30) {

        return "Overweight";
    } else {
        return "Obese";
    }

}


public void SaveClickHandler(View view) {

    // here I want to Save the weight and the height value
    prefs = PreferenceManager.getDefaultSharedPreferences(view.getContext());
    SharedPreferences.Editor editor = prefs.edit();
    editor.putFloat("height", height);
    editor.putFloat("weight", weight);
    editor.apply();
}

public void LoadClickHandler(View view) {
    // here I want to Load the weight and the height value
    height = prefs.getFloat("height", 0.0f);  //Default value (0.0f)
    weight= prefs.getFloat("weight", 0.0f);   //Default value (0.0f)
    heightText.setText(String.valueOf(height));
    weightText.setText(String.valueOf(weight));

}

Upvotes: 1

Nicolas Mauti
Nicolas Mauti

Reputation: 546

You can use the preferences system built in Android.

Check this guide : http://developer.android.com/training/basics/data-storage/shared-preferences.html

Basically, you access to the preferences like this :

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);

You store a value like this :

SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();

You retrieve the value like this :

int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);

Upvotes: 1

Related Questions