Simon.
Simon.

Reputation: 1896

ImageButton stretch src to fill

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);
}
...
}

Upvotes: 0

Views: 677

Answers (4)

Sejong Oh
Sejong Oh

Reputation: 21

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..

Upvotes: 2

Nickolai Astashonok
Nickolai Astashonok

Reputation: 2888

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).

Upvotes: 1

Hamad
Hamad

Reputation: 5152

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!

Upvotes: 0

Badrul
Badrul

Reputation: 1642

Do it this way

ImageButton object.setScaleType(ScaleType.FIT_XY)

Use ScaleType.FIT_XY instead of fitXY in your code

Upvotes: 0

Related Questions