how to change the image of a button with every click?

孤街醉人 提交于 2019-12-03 07:51:35

You can do it easily within the code.

boolean isPressed = false;
button.setOnClickListener(buttonListener);

OnClickListener buttonListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        if(isPressed)
           button.setBackgroundResource(R.drawable.icon1);
        else
           button.setBackgroundResource(R.drawable.icon2);

        isPressed = !isPressed;
   }
};

Simple way

btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
    btn.setBackgroundDrawable(getResources().getDrawable(R.drawable.locationbutton_on));
                }
        }); 

Make it in code perhaps. Put a listener on the button and when the button is clicked the background is changed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!