How do I programmatically change the ImageButton src target when a condition is met?

隐身守侯 提交于 2019-12-04 23:36:45
Chor Wai Chun

Is it a requirement that it has to be imageResource? You can try using backgroundResource instead.

ImageButton flashButtonOn = (ImageButton) findViewById(R.id.flashButtonOn);
flashButtonOn.setBackgroundResource(R.drawable.on_selector);

Or try

flashButtonOn.setImageResource(R.drawable.replacementGraphic);

It should be "setImageResource", "setBackgroundResource" changes the background, not the src. btn_MyButton.setImageResource(R.drawable.button_bg_selector_collapsed);

When setting the image using the following attribute:

android:src="@drawable/on_selector"

You will need to use the following to change the image dynamically:

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