Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ignoring my setWidth() and setHeight()

So, why does this code:

package org.popoffka.apicross;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

public class Game extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     Button testButton = new Button(this);
     testButton.setBackgroundResource(R.drawable.cell);
     testButton.setWidth(20);
     testButton.setHeight(20);
     setContentView(testButton);
 }
}

...produce this thing: http://i42.tinypic.com/2hgdzme.png even though there's a setWidth(20) and setHeight(20) in the code? (R.drawable.cell is actually a 20x20 PNG image containing a white cell with a silver border)

like image 999
Aleksejs Popovs Avatar asked Dec 06 '22 02:12

Aleksejs Popovs


1 Answers

The proper way to set the width and height of a View is to do so via the LayoutParams. See ViewGroup.LayoutParams and View.getLayoutParams().

like image 141
Romain Guy Avatar answered Dec 19 '22 01:12

Romain Guy