0

This question comes up quite often, however in all examples i have found, the image src is defined in the XML. e.g android:src="..."

My code doesn't specificy the src untill the activity, using ImageButton.setImageResource() as it is a single button, performing play/stop

How do i fill the ImageButton with the src, when src is defined later?

I tryed ImageButton.setScaleType="fitXY", however sdk doesn't like it using string...

enter image description here

UPDATE: After trying to use the suggested below, the problem still occurs. here is more explanation to help

<ImageButton
    android:id="@+id/imgStart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    />


public class MainActivity extends Activity{

private ImageButton player;

protected void onCreate(){
player = (ImageButton) findViewById(R.id.imgStart);

...

if(isPlaying){
player.setImageResource(bitmap image);
}
else{
player.setImageResource(different bitmap image);
}
...
}
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Simon.
  • 1,886
  • 5
  • 29
  • 62

4 Answers4

2

Use ScaleType.CENTER_CROP instead

player .setScaleType(ImageView.ScaleType.CENTER_CROP);

It seems CENTER_CROP works as fit_xy. but, it's not sure why it does..

Sejong Oh
  • 21
  • 4
1

Please try below code to set ScaleType programatically

ImageButton object".setScaleType(ScaleType.FIT_XY)

"ImageButton object" should be object of class ImageButton (that you defined probably in xml).

Hamad
  • 5,096
  • 13
  • 37
  • 65
Nickolai Astashonok
  • 2,878
  • 3
  • 20
  • 23
0

Do it this way

ImageButton object.setScaleType(ScaleType.FIT_XY)

Use ScaleType.FIT_XY instead of fitXY in your code

Badrul
  • 1,582
  • 4
  • 16
  • 27
0
ImageButton.setScaleType="fitXY"

yes this will not work, because you have to use it properly using below code also should setAdjustViewBounds to true.like this:

player.setAdjustViewBounds(true);
player.setScaleType(ScaleType.FIT_XY);

This will definitely work!

Hamad
  • 5,096
  • 13
  • 37
  • 65