3

So I built a custom preference view for one of the preferences in my preference activity:

<Preference 
      android:key="facebook_sharing"
      android:layout="@layout/fb_pref_item"/>

I want to be able to edit the ImageView in that custom preference layout programmatically (based on the value of the preference itself.

It would look something like this:

Preference facebookItem = (Preference)findPreference("facebook_sharing");
facebookItem.setOnPreferenceChangeListener(this);

RelativeLayout rl = //get relative layout someway or another...

mFacebookIcon = (ImageView)rl.findViewById(R.id.fb_imgview);
if(!mFacebookConn.isSessionValid())
    mFacebookIcon.setColorFilter(R.color.transparent_black);

I can't figure out how to pull off that commented line. I've tried the following:

RelativeLayout rl = (RelativeLayout)facebookItem.getView(null, null); //dont know what the params ought to be...

RelativeLayout rl = (RelativeLayout)findViewById(facebookItem.getLayoutResource());

Neither work. I searched around but can't seem to find anyone who has tried this. Any ideas? Thanks!

Edit: providing the fb_pref_item:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fb_view"
    android:layout_width="match_parent"
    android:layout_height="68dp" >

    <TextView android:id="@+id/fb_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="2dp"
        android:layout_alignParentTop="true"
        android:text="@string/facebook"
        android:textSize="20sp"
        android:textColor="@color/item_view_text_color" />

    <TextView android:id="@+id/fb_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="8dp"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/fb_title" />

    <ImageView android:id="@+id/fb_imgview"
        android:src="@drawable/facebook64"
        android:layout_width="42dp"
        android:layout_height="42dp"
        android:layout_centerVertical="true"
        android:layout_margin="12dp"  
        android:layout_alignParentRight="true" 
        android:scaleType="centerInside"
        android:contentDescription="@string/cd_icon"/>


</RelativeLayout>
JMRboosties
  • 15,500
  • 20
  • 76
  • 116
  • You probably should provide your layout resource (`fb_pref_item.xml`), because that is what is relevant for finding the icon you're looking for. – Joe Jul 15 '12 at 00:57
  • 1
    This can work: http://stackoverflow.com/questions/11790163/how-to-manage-custom-preference-layout. He used custom preference. – sontungdt7 Jun 21 '13 at 10:39

1 Answers1

0

Based on the information provided so far (see comment above, where I suggest posting the fb_pref_item.xml source -- you also didn't indicate if you have multiple ImageViews, or just one), seems to me like you're not looking for the right view. Instead of this:

RelativeLayout rl = //get relative layout someway or another...
mFacebookIcon = (ImageView)rl.findViewById(R.id.fb_imgview);

It looks like you should be able to ignore this unnecessary RelativeLayout completely, and just go straight to fb_imgview:

mFacebookIcon = (ImageView) this.findViewById(R.id.fb_imgview);

Note that in this case, you'd be using the inherited findViewById(int) method, which PreferenceActivity inherits directly from Activity, not trying to find the RelativeLayout first.

You can't use the Preference#getView() method, because that is used to generate the view (which will then be rendered in place in the PreferenceActivity). It is not used to obtain the view after it is already rendered.

Joe
  • 42,036
  • 13
  • 45
  • 61