gaurav gaurav - 3 months ago 17
Android Question

Remove Image of ImageButton and set new Image

ImageButton's background is not getting changed
Everything is working counter is getting changed it was working till it was onTouchListener, I changed to onclickListener then changing of background stopped, how should I change background in onClickListener

I tried http://stackoverflow.com/a/11835365/6648201 but it's not helping me

final ImageButton user_btn_like = (ImageButton) view.findViewById(R.id.user_btn_like);

user_btn_like.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(presslikecount ==0) {
user_btn_like.setPressed(true);
presslikecount++;
pressdislikecount = 0;

user_btn_like.setBackgroundResource(R.drawable.ic_user_like_red);
user_btn_dislike.setPressed(false);
user_btn_dislike.setBackgroundResource(R.drawable.ic_user_dislike_black);
like.setText(String.valueOf(presslikecount));
dislike.setText(String.valueOf(pressdislikecount));
}
else
presslikecount--;
user_btn_like.setPressed(false);
user_btn_like.setBackgroundResource(R.drawable.ic_user_like_black);
like.setText(String.valueOf(presslikecount));
}
});

Answer

Use

 user_btn_like.setImageResource(R.drawable.ic_user_like_red);

Instead of

 user_btn_like.setBackgroundResource(R.drawable.ic_user_like_red);