Devrath
Devrath

Reputation: 42824

How to programatically check if a imageview has any background drawable attached to it

What i have:: I have a imageview below for which i have attached a background drawable

<ImageView
    android:id="@+id/myId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/plus" />

What I am trying to find:: How can i check programmatically, if that imageview object has background drawable attached to it

Upvotes: 0

Views: 1962

Answers (2)

MeetTitan
MeetTitan

Reputation: 3568

I believe the getBackground() method is what you want. You got down voted because this is a trivial issue.

ImageView yourImageView = findViewById(R.id.myId);
Drawable background = yourImageView.getBackground();
if(background != null)
    System.out.println("View has background!");
else
    System.out.println("View has no background!");

Upvotes: 2

Dilavar Malek
Dilavar Malek

Reputation: 1167

ImageView myimage = (ImageView) v.findViewById(R.id.img_call_icon);
myimage.setTag(R.drawable.myicon);
myimage.setBackgroundResource(R.drawable.myicon);
myimage.
        Object tag = myimage.getTag();
        int id = tag == null ? -1 : (int) tag;
        switch (id) {
        case R.drawable.icon:
            Toast.makeText(mContext, "skype", Toast.LENGTH_SHORT).show();
            break;

        case R.drawable.myicon:
            Toast.makeText(mContext, "call", Toast.LENGTH_SHORT).show();
            break;
        }

Upvotes: 0

Related Questions