user1302569
user1302569

Reputation: 7191

Change icon in android

I would like to know how I can change image in imageview but in specific way. I would like to when I start application I have a default icon and when I click on them I change icon on the othe. But when I click again my icon return to default, and again.... This is what I got at this moment:

    final ImageView check_box = (ImageView) rowView.findViewById(R.id.email_list_item_checkbox_icon);

    check_box.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            check_box.setImageResource(R.drawable.checkbox_android);
        }
    });

How I can do next step and loop this solution?

Upvotes: 0

Views: 279

Answers (3)

Narendra Pal
Narendra Pal

Reputation: 6604

Simple, As per your requirement. Just take a boolean flag variable.

@Override
public void onClick(View arg0) {
    if ( !flag) {
        check_box.setImageResource(R.drawable.checkbox_android);
        flag = true;
    }
    else {
        check_box.setImageResource(R.drawable.other_image);
        flag = false;
    }
});

Upvotes: 1

Chirag Patel
Chirag Patel

Reputation: 11508

ImageView check_box = (ImageView) rowView.findViewById(R.id.email_list_item_checkbox_icon);

check_box.setTag(R.id.email_list_item_checkbox_icon);

check_box.setOnClickListener(new OnClickListener() {
    public void onClick(View view) {

        Integer integer = (Integer) view.getTag();
        switch(integer) {
        case R.id.email_list_item_checkbox_icon:
            check_box.setDrawableResource(R.drawable.checkbox_android);
            check_box.setTag(R.drawable.checkbox_android);
            break;
        case R.drawable.checkbox_android:
            check_box.setDrawableResource(R.id.email_list_item_checkbox_icon);
            check_box.setTag(R.id.email_list_item_checkbox_icon);
            break;
        }
    }

});

Upvotes: 1

Vikalp Patel
Vikalp Patel

Reputation: 10877

You can use some flags which becomes true to default icon and false to checked icon. Using flag you can achieve your part of goal.

Upvotes: 1

Related Questions