tags:

views:

49

answers:

1

When I comment out setContentView(boardView); in my Game.java my custom view in BoardView works fine and displays everything nicely... but onSizeChanged never gets called in BoardView.java... so I can't read the device width and height at runtime. If I leave setContentView uncommented onSizeChanged works... but the screen is blank!

I want to be able to read the screen width and height at runtime and set the sizes of my ImageViews at creation so they are the optimal size.

public class Game extends Activity implements OnClickListener{

private BoardView boardView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boardView = new BoardView(this);

    setContentView(boardView);  // when this line disabled, it looks ok
    boardView.requestFocus();

}

public class BoardView extends View { private final Game game; private float width; // width of one unit private float height; // height of one unit

public BoardView(Context context){ super(context); this.game = (Game)context; setFocusable(true); setFocusableInTouchMode(true);

    LinearLayout maincontainer = new LinearLayout(game);
    maincontainer.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
    maincontainer.setGravity(Gravity.CENTER);
    maincontainer.setOrientation(LinearLayout.VERTICAL);   
    maincontainer.setBackgroundColor(Color.BLACK);

    LinearLayout innercontainer = new LinearLayout(game);
    innercontainer.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
    innercontainer.setGravity(Gravity.CENTER);
    innercontainer.setOrientation(LinearLayout.HORIZONTAL);   

    // declare a new table
    TableLayout layout = new TableLayout(game);
    layout.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 

    // build a grid of ImageViews in a TableLayout
    for (int f=1; f<=7; f++) {
        TableRow tr = new TableRow(game);
        for (int c=1; c<=7; c++) {
         ImageView b = new ImageView(game);
         b.setImageResource(R.drawable.neworb);
         b.setOnClickListener(game);             
            tr.addView(b, 30,30);  // I'd like to not use fixed values here
        } // for
        layout.addView(tr);
    } // for
    innercontainer.addView(layout);
    maincontainer.addView(innercontainer);

    game.setContentView(maincontainer);

}

@Override protected void onSizeChanged(int w, int h, int oldw, int oldh){ width = w/9f; height = width;

super.onSizeChanged(w, h, oldw, oldh); }

}

A: 

Thought I solved my own problem. I was neglecting to place my my view building code withing the Overriden onDraw method. i.e.:

@Override
protected void onDraw(Canvas canvas)
{

    LinearLayout maincontainer = new LinearLayout(this.game);

    // etc..

I was just including it within the main class and not Overriding onDraw... which you gotta do when you extend View.

DeadTime
******************************Actually, not such a good solution. Yes, it worked. But.... now I'm creating new objects and throwing them out with each screen draw. Very bad form. LogCat tells me I'm working the Garbage Collector overtime.
DeadTime