aQ1711
aQ1711

Reputation: 27

Setting background of imageView in RelativeLayout

I am setting a background of an imageView of a relative layout using Helper.sync_settting_2(img_sync, context).

 try {
            final RelativeLayout layout_img_sync = (RelativeLayout) findViewById(R.id.relative_lay_6);
            final ImageView img_sync = (ImageView) findViewById(R.id.img_sync);
            layout_img_sync.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Helper.sync_settting_2(img_sync, context);
                }
            });
        } catch (Exception e) {
    };  

Here is the function Helper.sync_settting_2(ImageView sync, Context ct).

 public static void sync_settting_2(ImageView sync, Context ct) {
    Toast.makeText(ct, "Sync Started", Toast.LENGTH_LONG).show();
    getJobData getJobs = new getJobData(null);
    sync.setBackground(ct.getResources().getDrawable(
            com.org.courier.R.drawable.sync_image_orange));
    getJobs.setOnApiLIstner(new onApiCompleted() {
        @Override
        public void onApiResults(ArrayList<job_entity> events_list) {
            getMessageData getMessage = new getMessageData(null);
            getMessage.setOnApiLIstner(new getMessageData.onApiCompleted() {

                @Override
                public void onApiResults(
                        ArrayList<message_entity> events_list) {
                    getContactData getContact = new getContactData(null);
                    getContact
                            .setOnApiLIstner(new getContactData.onApiCompleted() {

                                @Override
                                public void onApiResults(
                                        ArrayList<contact_entity> events_list) {
                                }
                            });
                    getContact.execute();
                }
            });
            getMessage.execute();
        }
    });
    getJobs.execute();
    sync.setBackground(ct.getResources().getDrawable(
            com.org.courier.R.drawable.sync_image_03));
    Toast.makeText(ct, "Sync Finished", Toast.LENGTH_SHORT)
    .show();
}

It showing an error

"java.lang.NoSuchMethodError: android.widget,ImageView.setBackground"

Upvotes: 0

Views: 2212

Answers (1)

Cruceo
Cruceo

Reputation: 6834

View.setBackground wasn't added until API 16. I ran into this issue myself, and couldn't figure out for the life of me why it was happening, since I didn't get any newAPI warnings.

If you want it to be compatible with all versions of Android, you should use:

setBackgroundResource() or setBackgroundDrawable()

However, for an ImageView, you should instead be setting the SRC, not the Background:

setImageResource() or setImageDrawable()

Upvotes: 12

Related Questions