user2875404
user2875404

Reputation: 3218

ScaleType on ImageButton results in tiny image

Hey I have a grid of ImageButtons that is being scaled by display width and contains a PNG file as image. I add the whole thing to the linear layout like this:

public void createButtons(){

            int buttonX = 9;
            int buttonY = 9;
            int size    = 80;
            int tag     = 0;

          TableLayout layout = new TableLayout (this);
          layout.setLayoutParams( new TableLayout.LayoutParams(900,900) );
          layout.setPadding(1,1,1,1);
          layout.setBackgroundColor(0xff00af00); //green

          RelativeLayout ll = (RelativeLayout)findViewById(R.id.rel);
          ll.addView(layout);

          for(int x=0;x<buttonX;x++)    {

              TableRow tr = new TableRow(this);                     
              for(int y=0;y<buttonY;y++)    {
                but[x][y] = new ImageButton(this); 
                but[x][y].setBackgroundColor(0xff0000af); //blue
                but[x][y].setImageResource(R.drawable.buttonmask3); 
                     but[x][y].setScaleType(ImageButton.ScaleType.CENTER_INSIDE); 
                tr.addView(but[x][y], height/10,height/10);
                }
                layout.addView(tr);
            }
}  

The problem is, that the layout now looks like the following:

does

whereas it is supposed to look like:

should

(this one was a quick photoshop but you get the idea, that the scale is supposed to fill the button completely)

What can I do about these tiny images? I tried CENTER_INSIDE, FITXY and all the other ScaleTypes but I haven't been lucky so far :/

The width is taken from the screen width (or height in landscape)

the buttonmask3.png is about 170*170 px.

Upvotes: 4

Views: 43

Answers (1)

user2875404
user2875404

Reputation: 3218

The problem is the padding that the ImageButton comes with naturally.

adding

but[x][y].setPadding(0, 0, 0, 0);

along with

but[x][y].setScaleType(ImageButton.ScaleType.CENTER_CROP); 

Solved it for me.

Upvotes: 3

Related Questions