I found that if I implement implements 
SharedPreferences.OnSharedPreferenceChangeListener, inside the implemented 
method, findViewById works. Why is this?

public class SettingsActivity extends Activity implements SharedPreferences.
OnSharedPreferenceChangeListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Display the fragment as the main content.
        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new SettingsFragment( this ))
                .commit();

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences
( this );
        sp.registerOnSharedPreferenceChangeListener( this );

    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences 
sharedPreferences, String key) {
        if ( key.equals( "pref_catalogEdition" ) ) {
            ProgressBar bar = ( ProgressBar ) findViewById( 
R.id.catalogLoadProgressBar 
);
            bar.setVisibility(View.VISIBLE ); // WORKS!
        }
    }
}



On Tuesday, September 30, 2014 3:23:05 PM UTC-7, Qoheleth wrote:
>
> As suggested by the docs if using Android 3.0+, I used a Preference 
> fragment which is used by an Activity that uses getFragmentManager to 
> implement Preferences. My question is, how do you get the (root?) View of a 
> Preference layout?
> In both SettingsActivity and SettingsFragment, findViewById and getView 
> both return null, which probably means that the preference layout view is 
> null, but somehow there is a layout.
>
> I also have a custom preference, CustomCatalogListPreference. What I'm 
> trying to do is gain access to a progress bar that is part of the custom 
> preference, but findViewById or getView both return null.
>
> public class SettingsActivity extends Activity {
>
>     @Override
>     protected void onCreate(Bundle savedInstanceState) {
>         super.onCreate(savedInstanceState);
>
>         // Display the fragment as the main content.
>         getFragmentManager().beginTransaction()
>                 .replace(android.R.id.content, new SettingsFragment( this 
> ))
>                 .commit();
>     }
> }
>
> public class SettingsFragment extends PreferenceFragment {
>     public static final String ABILITY_SCORES_KEY = "pref_score";
>     public static final String SAVING_THROW_KEY = "pref_saveThrow";
>     Context mContext;
>
>     public SettingsFragment( Context ctx ) {
>         mContext = ctx;
>     }
>
>     @Override
>     public void onCreate(Bundle savedInstanceState) {
>         super.onCreate(savedInstanceState);
>
>         // Load the preferences from an XML resource
>         addPreferencesFromResource( R.xml.preferences );
>     }
> }
>
> public class CustomCatalogListPreference extends ListPreference {
>
>     Context mContext;
>
>     public CustomCatalogListPreference( Context context, AttributeSet 
> attrs ) {
>         super( context, attrs );
>         mContext = context;
>     }
>
>     public CustomCatalogListPreference( Context context ) {
>         super(context);
>         mContext = context;
>     }
>
>     @Override
>     public View onCreateView( ViewGroup parent ) {
>         super.onCreateView( parent );
>         LayoutInflater inflater = ( LayoutInflater ) mContext.
> getSystemService( Context.LAYOUT_INFLATER_SERVICE );
>         return inflater.inflate( R.layout.custpref_catalog_list, null );
>     }
>
>     @Override
>     protected void onBindView( View view ) {
>         super.onBindView( view );
>     }
>
>
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to