I’m trying to update the image of an imageButton on run time. I have a switch statement that checks for an ID passed from another activity. I know the switch statement is working as the correct ID is passed to the TextView.
I’ve been searching and see some examples use the ImageView and others use the ImageButton. As you can see below I’ve tried both and none work.
XML Layout:
<ImageButton android:visibility="gone" android:id="@+id/imageButton" android:src="@drawable/defaultimage" android:layout_width="97dp" android:layout_height="95dp"></ImageButton>
<TextView android:visibility="gone" android:text="TextView" android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true"></TextView>
Java Code:
case 1:{
// Location 1
ImageView ImageButton = (ImageView)findViewById(R.id.imageButton);
ImageButton .setImageResource(R.drawable.image1);
ImageButton .setVisibility(0);
TextView Test = (TextView)findViewById(R.id.textView);
Test.setVisibility(0);
Test.setText("ID passed is" + id);
break;
}
case 2:{
// Location 2
ImageButton ImageButton = (ImageButton)findViewById(R.id.imageButtonGhostCamLocation);
ImageButton .setBackgroundResource(R.drawable.image2);
ImageButton .setVisibility(0);
TextView Test = (TextView)findViewById(R.id.textView);
Test.setVisibility(0);
Test.setText("ID passed is" + id);
break;
UPDATE
Got it to work! I just removed the android src from the ImageButton in xml layout and it’s working fine now. Thanks for the help!
The logic of your code seems correct(unless you have funny things like
ImageButton ImageButtonin your original code). The problem must be somewhere else.Can you see the button if you remove
android:visibility="gone"from layout?Btw instead of:
use
That way it is more readable.