Java – Why is my View not drawing?

Why is my View not drawing?… here is a solution to the problem.

Why is my View not drawing?

I’m trying to make an android app with some dynamically drawn View. Currently, the only thing a View draws is the circle in the middle of the View.

The Views are in the GridView, but they don’t seem to be drawn correctly.

Here’s what happens when I load the screen:

enter image description here

The orange block is the view in the GridView that has focus.

However, if I drag along the View with my finger (or mouse), it draws correctly:

enter image description here

Why is that?

How to make it draw the second picture all the way.

Here is the code I used:

public class ChooseTablePanel extends GamePanel {
    TableAdapter adapter;

    public ChooseTablePanel(Context context, GamePanel nextPanel,
            GamePanel failurePanel) {
        super(context, nextPanel, failurePanel);

    public ChooseTablePanel(Context context, AttributeSet attrs,
            GamePanel nextPanel, GamePanel failurePanel) {
        super(context, attrs, nextPanel, failurePanel);

    private void initialize() {     
        adapter = new TableAdapter(getContext());
        adapter.setTables(new int[] {5,4,3,2,1,6});

        GridView gridView = new GridView(getContext());


    class TableAdapter extends BaseAdapter {
        private Context context;
        private TableView[] tables;

        public TableAdapter(Context context) {
            this.context = context;

        public void setTables(int[] tables) {
            this.tables = new TableView[tables.length];

            for(int i = 0; i < tables.length; i++){
                this.tables[i] = new TableView(tables[i], context);

        public int getCount() {
            return tables.length;

        public Object getItem(int position) {
            return tables[position];

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;

        public View getView(int position, View convertView, ViewGroup parent) {
            TableView tableView;
            if (convertView == null) {  // if it's not recycled, initialize some attributes
                tableView = new TableView(1, this.context);
                tableView.setLayoutParams(new GridView.LayoutParams(85, 85));
                tableView.setPadding(8, 8, 8, 8);
            } else {
                tableView = (TableView) convertView;

            return tableView;

    class TableView extends View {
        private int seats;

        public TableView(int Seats, Context context) {
            this.seats = Seats;

            if(seats < 1){
                throw new IllegalArgumentException("Number of seats must be greater than one.");

        public int getSeats() {
            return seats;

        protected void onDraw(Canvas canvas){
            int tableWidth = canvas.getWidth() / 2;
            ShapeDrawable tableShape = new ShapeDrawable(new OvalShape());          
            tableShape.setBounds(canvas.getWidth()/2 - tableWidth/2, canvas.getHeight()/2 - tableWidth/2, canvas.getWidth()/2 + tableWidth/2, canvas.getHeight()/2 + tableWidth/2 );

            canvas.drawText(seats + "", 0, 0, new Paint());



Best Solution

Do not use and try to use canvas.getWidth() this.getWidth() and this .getHeight()canvas.getHeight() .

Related Problems and Solutions