How to manage custom preferences

I have a user preference with an image and two texts, and I need to programmatically change the image.

I can get a custom view of preferences and find the ImageView in the layout using findViewById, but if I try to change the image, this will not work.

Am I something wrong?

LAYOUT PREVENTION

 <?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 OF CHANGE OF THE CONTENT OF THE IMAGE

@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);
+5
source share
2 answers

I tried the subroutine getView, but this did not work for me, and finally I went through the user one 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.setImageto change ImageViewin the callback method onPreferenceClick.

I want this to help! Good luck.

+6

, res/drawable, R.drawable.? . URI, . URI, . , , , - "", .

0

All Articles