1

I've created my custom preference layout with imageView.

<Preference 

...
 android:key="pref_custom"

 android:layout="@layout/preference_layout"

 >

In my own PreferenceActivity inside onCreate I want to change my CustomPreference ImageView.

Problem is that code below always returns null.

imageViewSmallContact = (ImageView) findViewById(R.id.ImageViewSmallContact);

I can find this ImageView only inside OnPreferenceClickListener.onPreferenceClick method my pref_custom Preference.

In My class ConfigureActivity (extends PreferenceActivity) in onCreate method I have only":

...
super.onCreate(savedInstanceState); 
addPreferencesFromResource(R.xml.preferences); 
updatePreferenceView(); // here I update all my preferences `
...

In my PreferenceActivity onCreate method I read my preference
Preference pref= findPreference("pref_phone_");

Than I want to change Image in my custom layout of this one Preference.

imageViewSmallContact = (ImageView) findViewById(R.id.ImageViewSmallContact);

Here imageViewSmallContact returns null.

Then in this preference I register OnPreferenceClickListener

Inside onPreferenceClick findViewById(R.id.ImageViewSmallContact); return correct value.
My question is :

How can I initialize this layout to get imageView from my PreferenceActivity ?

  • can you add some code for your Activity? I would suspect you are trying to access the view before setContentView was called. – koljaTM Jan 16 '13 at 09:22
  • I've updated description. I think setcontentview in PreferenceActivity sets Activity not my custom Preference – Marcin Kasiński Jan 16 '13 at 09:57
  • Have you checked http://stackoverflow.com/questions/7846855/android-how-to-set-custom-layout-for-preferenceactivity-in-android-3-0 ? – koljaTM Jan 16 '13 at 10:02
  • Thanks. I don't get it. You are talking about layout for PreferenceActivity with Preference list. I'm talking about layout for only one Preference which exists in my PreferenceActivity. – Marcin Kasiński Jan 16 '13 at 10:25
  • I think I need yet more context. can you provide some more code around that line which returns null and the place where it doesn't return null? – koljaTM Jan 16 '13 at 10:29
  • I've updated description. I hope it helps. – Marcin Kasiński Jan 16 '13 at 11:03

1 Answers1

0

When setting the xml configuration for the PreferenceActivity, the layout for the Preferences isn't immediately inflated. This is only done, once it is needed. You could try to debug into

android.preference.Preference.onBindView/onCreateView(..)

to see, when this is called. I would think it is similar to the inflating of views in a ListView which is only done when they are actually displayed. What are you trying to do to your ImageView? Maybe you can specify it inside your layout.xml file already?

It might be possible to override Preference and supply a custom implementation of onBindView() that does whatever is necessary to your ImageView. I can only speculate here, because I ditched the whole PreferenceActivity when I wanted more sophisticated layout, but I would think you could create a subclass of Preference which you then reference in your xml. In that class (e.g. MyImagePreference) you would override the onBindView method to initialize your view as needed (after the super.onBindView() call).

koljaTM
  • 10,064
  • 2
  • 40
  • 42
  • I need change imageView based on SharedPreference. Sorry for stupid question, by how cak I get my ImageView in my onBindView method ? I tried findViewById(R.id.myimageview), but inside id there isn't my imageView key – Marcin Kasiński Jan 16 '13 at 13:42