Thursday, September 30, 2010

[android-developers] Re: Setting/Passing values

I looked at the URL's that you provided and tried some coding with it,
but I am failing to understand
the proper coding methods.

In the main, I have the options menu as:

// options menu

// Called only the first time the options menu is displayed.
// Create the menu entries.
// Menu adds items in the order shown.
public boolean onCreateOptionsMenu(Menu menu) {
//return true;

// handle menu selected
public boolean onOptionsItemSelected(MenuItem item){

if (item.getTitle().equals("Setup")){
Intent intent = new Intent(this,
startActivityForResult(intent, MY_SETUP);
return true;

if (item.getTitle().equals("About")){
Intent intent = new Intent(this,
startActivityForResult(intent, MY_ABOUT);
return true;
//return false;

I believe that I need this to read the preferences still in the main
and set a textview as such:

public void onResume() {

SharedPreferences prefs=PreferenceManager
TextView tv = (TextView) findViewById(;
tv.setText(prefs.getString("tview1", ""));

The preference name for this textview is called "tview1". According
to the example, it looks like you have
to give it a value. I just want to read that preference, and take the
value it has, and place it in the TextView01 text
but according to the example, you have to give it a value.

In the setup, I created this:
protected void onStop(){

// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME,
SharedPreferences.Editor editor = settings.edit();

editor.putString("tview1","this is a test");

// Commit the edits!

The PREFS_NAME was declared at the top:
public static final String PREFS_NAME = "calcsettings";

If I am following this, the "editor" creates the name/value pair
("tview1" = "this is a test") then writes it to the preferences
by calling the editor.commit();

What am I doing wrong? As I am testing, I am attempting to creating/
one preference (name/value).

Thanks again for all your help.


On Sep 30, 4:59 pm, Mark Murphy <> wrote:
> On Thu, Sep 30, 2010 at 6:51 PM, rb <> wrote:
> > Thanks for shedding some light on this issue.
> > Here's some of my code:
> :: snip ::
> > I was going to save and read the data from a file which would be
> > stored
> > in the phones default applications directory as one file.
> All the more reason to use a PreferenceActivity.
> > Not sure how you would use the preference activity for storing name/
> > values as
> > I need to store and retrieve 6 different items.
> Ever looked at the Settings application in your device? That's a
> PreferenceActivity. That has well more than "6 different items".
> > Any chance you could reply with some code on how I could utilize the
> > preferences activity
> > from the code I have provided?
> --
> Mark Murphy (a Commons Guy)||
> _The Busy Coder's Guide to Android Development_ Version 3.1 Available!

You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

No comments:

Post a Comment