Aviran
Aviran

Reputation: 5480

Android ImageButton setMaxheight not having effect

I use a table layout to layout buttons using weights, the buttons get 0dip size or wrap_content and so they expand by their weight.

I'm trying to convert a button to ImageButton and set the image to be in the button size and not expand it.

I let the button get created and get it's size and then I assign the image and limit it's height using ImageButton.setMaxHeight();

final ImageButton ib = (ImageButton) findViewById(R.id.buttonOptions);
    ib.post(new Runnable() {
        public void run() {
            final int height = ib.getHeight();
            ib.setScaleType(ScaleType.FIT_CENTER);
            ib.setMaxHeight(5); // or height    
            ib.setImageResource(R.drawable.options);

        }});

The images is being drawn in it's original size, thus expanding the button... setMaxHeight() is not limiting the image height/width. Any idea why?

Upvotes: 1

Views: 868

Answers (3)

Aviran
Aviran

Reputation: 5480

solved it using

ib.setAdjustViewBounds(true);

Upvotes: 3

Johny Smith
Johny Smith

Reputation: 154

setMaxHeight() is for the imagebutton and not for the image in it.

Try,

ib.setScaleType(ScaleType.CENTER_CROP);

Upvotes: 0

Android Killer
Android Killer

Reputation: 18509

ViewTreeObserver observer = ib.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//here you can get height and set height
}

Not sure but try it.It may help you.

Upvotes: 0

Related Questions