beachboy2009
beachboy2009

Reputation: 5

How can I update TextView in a different layout

I'm just trying to update TextView (@id/pref_gender_textView) in the "gender.xml" where gender.xml is defined in preference.xml.  I'd like to update the TextView from MyPreferenceFragment.java

How can I update the TextView genderTextView from MyPreferenceFragment.java?
Thank you much in advance!

-- preference.xml looks like:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <ListPreference
            android:key="lp_gender"
            android:entries="@array/array_lp_gender"
            android:entryValues="@array/array_lp_gender_values"
            android:title="@string/str_lp_android_choice_gender"
            android:layout="@layout/gender"/>

</PreferenceScreen>

Now, I'd like to access "genderTextView" from somewhere in "MyPreferenceFragment.java"

import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyPreferenceFragment extends PreferenceFragment{   

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);

        /** Defining PreferenceChangeListener */
        OnPreferenceChangeListener onPreferenceChangeListener = new OnPreferenceChangeListener() {

            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {             
                OnPreferenceChangeListener listener = ( OnPreferenceChangeListener) getActivity();
                listener.onPreferenceChange(preference, newValue);
                return true;
            }
        };  

    }

}

Upvotes: 0

Views: 765

Answers (1)

ckihm
ckihm

Reputation: 496

The Android framework takes care of persisting the preferences in an XML file within the application's /data directory on the device. The API is offering several ways to get an handle on the stored preferences.

You do not really want to update a TextView in a different layout. Doing so you would introduce an unnecessary tight coupling between you preferences view and the other view. What you really want is just to update the text in your TextView whenever the setting has changed. This could be achieved implementing the SharedPreferences.OnSharedPreferenceChangeListener. To do so register for example your Activity as Listener:

public class YourActivity extends Activity implements OnSharedPreferenceChangeListener {

   private TextView mMyText;

   @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);

        mMyText = (TextView) findViewById(android.R.id.my_text);

        // listen to preference changes
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
        sharedPref.registerOnSharedPreferenceChangeListener(this);

        ...
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
            String key) {

        if(key.equals(getString(R.string.pref_key_myText))){

            mMyText.setText(sharedPreferences.getString(key, getString(R.string.pref_myText_defaultValue)));
        }
    }
}

Upvotes: 1

Related Questions