8

I have a custom preference with an image and two text and I need to change the image programmatically.

I am able to get the custom preference view and to find the ImageView in the layout using findViewById, but if I try to change the image this do not work.

Do I am wrong something?

PREFERENCE LAYOUT

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >


    <ImageView
        android:id="@+id/imageforfacebook"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:padding="5dip"
        android:src="@drawable/icon" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fadingEdge="horizontal"
            android:singleLine="true"
            android:text="titolo"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="18sp" />

        <TextView
            android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@android:id/title"
            android:layout_below="@android:id/title"
            android:maxLines="2"
            android:text="descrizione"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </LinearLayout>

</LinearLayout>

CODE TO CHANGE THE IMAGEVIEW CONTENT

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

View v = (View) prefs_facebookimage.getView(null, null);
ImageView img = (ImageView) v.findViewById(R.id.imageforfacebook);
Uri uri = Uri.parse(path);
img.setImageURI(uri);
Giuseppe
  • 281
  • 3
  • 7

2 Answers2

12

I tried getView routine but it doesn't work for me, finally I got it through with a custom Preference:

public class ImageViewPreference extends Preference {
    private ImageView mImageView;
    private Bitmap mPhoto;

    public ImageViewPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setWidgetLayoutResource(R.layout.pref_account_image);
        if (mPhoto == null) {
            mPhoto = BitmapFactory.decodeResource(
                    getContext().getResources(), R.drawable.icon);
        }
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        mImage = (ImageView) view.findViewById(R.id.pref_imageView);
        mImage.setImageBitmap(mPhoto);
    }

    public void setImage(Intent imageData) {
        mPhoto = data.getParcelableExtra("data");
    }
}

And here is the Preference.xml:

<PreferenceCategory
    android:key="@string/pref_category_myNote_key"
    android:title="@string/pref_category_myNote" >
    <com.flounder.xeniaNote.view.ImageViewPreference
        android:key="@string/pref_image_key"
        android:title="@string/pref_image"
        android:widgetLayout="@layout/pref_account_image" />
</PreferenceCategory>

And call mImagePref.setImage to change your ImageView in the callback method onPreferenceClick.

I wish it helps! Good luck.

Yaohui.W
  • 328
  • 3
  • 15
  • I found that `findViewById()` still didn't work in `onBindView()`. I did it in `onCreateView()` instead, which worked. – ryan Sep 20 '13 at 01:57
  • @ryan that's strange. I'm not having this issue and the documentation says `This is a good place to grab references to custom Views in the layout and set properties on them`. http://developer.android.com/reference/android/preference/Preference.html#onBindView%28android.view.View%29 – Sufian Aug 25 '14 at 09:24
0

What if you put all images under res/drawable and then access them through R.drawable. ? This approach has always worked for me. If you don't have compelling reasons for using URI this approach might be worth trying. Even if you have to use URI I would suggest to test this approach and get it working. Then at least you would know that there is something wrong with the value of the 'path' and not your implementation in general.

ali.chousein
  • 95
  • 1
  • 1
  • 10
  • I can't use the drawable solution as the image will be pick from camera or sdcard and it can change any time user want. The code work out the preference, also I have just tried to change some Text in my custom layout, and after I call setText("newText"); during the debug the TextView have the new text, but once finish then it return as original. – Giuseppe Aug 03 '12 at 09:03