Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7861461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:44:58+00:00 2026-06-02T22:44:58+00:00

I would like to load the value of Name from the sharedpreference that is

  • 0

I would like to load the value of Name from the sharedpreference that is defined below, but I cant. How can I extract the value of Name and toast it or ideally put it in the text field?

public class FillingActivity extends Activity{

TextView username;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.foodandexercise);

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

    String user = prefs.getString("namepref", "");
    Toast.makeText(FillingActivity.this, user, Toast.LENGTH_LONG).show();

    username = (TextView) findViewById(R.id.loggedastoedit);
    username.setText("najib");




}

}

and here is my SharedPreferences class

public class ProfilePreferenceActivity extends Activity {

DatabaseAdapter usertable = new DatabaseAdapter(this);

float calnum, bmr;
String level, gender;
int Age, Height, Weight, temp;
RadioButton rb_male;
RadioButton rb_female;
RadioButton rb_light;
RadioButton rb_moderate;
RadioButton rb_heavy;
EditText name;
EditText age, weight, height;
RadioGroup genderradiogroup;
RadioGroup levelofactivity;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.createprofile);
    SharedPreferences customSharedPreference = getSharedPreferences(
            "myCustomSharedPrefs", Activity.MODE_PRIVATE);

    name = (EditText) findViewById(R.id.namefield);
    name.setText("");
    age = (EditText) findViewById(R.id.agefield);
    age.setText("");

    genderradiogroup = (RadioGroup) findViewById(R.id.radioGroup2);
    rb_male = (RadioButton) findViewById(R.id.maleradiobutton);
    rb_female = (RadioButton) findViewById(R.id.femaleradiobutton);
    genderradiogroup.check(customSharedPreference.getInt("genderprefs",
            rb_male.getId()));
    genderradiogroup.check(customSharedPreference.getInt("genderprefs",
            rb_male.getId()));

    weight = (EditText) findViewById(R.id.weightfield);
    weight.setText("");
    height = (EditText) findViewById(R.id.heightfield);
    height.setText("");

    levelofactivity = (RadioGroup) findViewById(R.id.radioGroup3);
    rb_light = (RadioButton) findViewById(R.id.lightradiobutton);
    rb_moderate = (RadioButton) findViewById(R.id.moderateradiobutton);
    rb_heavy = (RadioButton) findViewById(R.id.heavyradiobutton);
    levelofactivity.check(customSharedPreference.getInt("levelpref",
            rb_light.getId()));

    try {
        Age = Integer.parseInt(age.getText().toString());
        Weight = Integer.parseInt(weight.getText().toString());
        Height = Integer.parseInt(height.getText().toString());
    } catch (Exception e) {
    }

    Button addUser = (Button) findViewById(R.id.checkcreateprofilebutton);

    addUser.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            savePreferences();

            float calnumber = calculateCalories(Age, Weight, Height);
            String lala = String.valueOf(calnumber);
            Toast.makeText(ProfilePreferenceActivity.this, lala,
                    Toast.LENGTH_LONG).show();

            usertable.open();
            long id = usertable.createUser(name.getText().toString(), age
                    .getText().toString(), gender, weight.getText()
                    .toString(), height.getText().toString(), level,
                    calnumber);

            Toast.makeText(ProfilePreferenceActivity.this,
                    "user added with" + level + gender, Toast.LENGTH_LONG)
                    .show();

            usertable.close();

            Intent Filling = new Intent();
            Filling.setClass(ProfilePreferenceActivity.this,
                    FillingActivity.class);
            startActivity(Filling);

        }
    });

}

private void savePreferences() {

    SharedPreferences customSharedPreference = getSharedPreferences(
            "myCustomSharedPrefs", Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = customSharedPreference.edit();
    editor.putString("namepref", name.getText().toString());
    editor.putString("agepref", age.getText().toString());
    editor.putInt("genderprefs", genderradiogroup.getCheckedRadioButtonId());
    editor.putString("heightpref", height.getText().toString());
    editor.putString("weightpref", weight.getText().toString());
    editor.putInt("levelpref", levelofactivity.getCheckedRadioButtonId());
    editor.putFloat("calpref", calnum);
    editor.commit();
    finish();

}

public float calculateCalories(int age, int weight, int height) {

    if (rb_male.isChecked()) {

        gender = "male";

        bmr = 66.5f + (13.75f * weight) + (5.003f * height)
                - (6.755f * age);

        if (rb_light.isChecked()) {
            level = "light";
            calnum = bmr * 1.375f;
        }
        if (rb_moderate.isChecked()) {
            level = "moderate";
            calnum = bmr * 1.55f;
        }
        if (rb_heavy.isChecked()) {
            level = "heavy";
            calnum = bmr * 1.725f;
        }

    } else if (rb_female.isChecked()) {

        gender = "female";

        bmr = 665 + (9.563f * weight) + (1.850f * height) - (4.676f * age);

        if (rb_light.isChecked()) {
            level = "light";
            calnum = bmr * 1.375f;
        }
        if (rb_moderate.isChecked()) {
            level = "moderate";
            calnum = bmr * 1.55f;
        }
        if (rb_heavy.isChecked()) {
            level = "heavy";
            calnum = bmr * 1.725f;
        }

    }
    return calnum;

}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:45:01+00:00Added an answer on June 2, 2026 at 10:45 pm

    You should either use getDefaultSharedPreferences() or getSharedPreferences("myCustomSharedPrefs",...) in both places.

    That way you create (AFAIK) different shared preferences and fail to read the data you write into the other.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i would like to load xml into dataset with only 2 columns (name, price)
I have an assembly I would like to load from a sub-folder of the
I have several PDF templates that I would like to load and modify and
I would like to load a context file from a jar file. For example,
I would like to determine the jar file name from my java code. I
I would like to load a HTML document and modify it's text in PHP.
$('#selector').click(function() { // here I would like to load a javascript file // let's
I have a C# .Net 2.0CF application where I would like to load a
I have been toying with the modulino perl pattern and would like to load
I have a list of files on a server and would like to load

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.