-2

I want to know if there is a way to make an imagebutton display its image after it is pressed.

I need this to be done in my Java main Activity, not in the XML layout file.

The button should be blank white in beginning and when pressed I want it to display its image. The method I am in gets passed a View from the button, if you need to know that.

Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
Gage Hafl
  • 21
  • 1
  • 2
    please post your code – Linh Sep 30 '16 at 03:18
  • with out code we can not guess what you are doing but I think there could be two possible reasons of it 1) Image is too bulky 2) ImageButton not getting validate , so validate it after setting image . 3) are Yew setting Image resource or background? – A.s.ALI Sep 30 '16 at 03:32

1 Answers1

0

To add image programmatically when pressed you need to do this :

ImageButton imgBtn = (ImageButton) findViewById(R.id.img_btn); imgBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { imgBtn.setImageResource(R.drawable.btn_img_drawable); } });

And in xml no need to use

android:src=" "

Do let me know if this was helpful

Nishant Dubey
  • 2,802
  • 1
  • 13
  • 18
  • i ended up just having to use these 2 lines `code` ImageButton v = (ImageButton) findViewById(view.getId()); v.setImageResource(R.drawable.bluex); `code` – Gage Hafl Oct 04 '16 at 18:14