3

I've got this code:

package com.shipwreckt;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.LinearLayout;


/**
 * TODO: document your custom view class.
 */
public class GridBoard extends LinearLayout {

    Context c;



    public GridBoard(Context context){
        this(context,null);
    }

    public GridBoard(Context context,AttributeSet as){
        super(context,as);
        c=context;
        init();

    }

    void init(){
        this.setBackgroundColor(0xFF006600);
        this.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        for(int a=0;a<10;a++){
            LinearLayout base=createRow();
                for(int b=0;b<10;b++) {
                    base.addView(createPlace());
                }
            this.addView(base);
        }

    }

    LinearLayout createPlace(){
        LinearLayout ll=new LinearLayout(c){
            public int z;
        };
        ll.setOrientation(HORIZONTAL);
        ll.setPadding(2,2,2,2);
        ll.setLayoutParams(new LayoutParams(0, LayoutParams.MATCH_PARENT));
        ll.setBackgroundColor(0xFFFF0000);
        ll.setWeightSum(1);

        return ll;
    }

    LinearLayout createRow(){
        LinearLayout ll=new LinearLayout(c);
        ll.setOrientation(VERTICAL);
        //ll.setWeightSum(1);
        ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        ll.setBackgroundColor(0xFFFF0000);
        return ll;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension( (widthMeasureSpec>heightMeasureSpec?heightMeasureSpec:widthMeasureSpec) , (widthMeasureSpec>heightMeasureSpec?heightMeasureSpec:widthMeasureSpec));
    }
}

The problem is, when I execute it, I only get green square (as I planned), but without any layouts that should be inside and create a nice grid. Do you know why is it? I tried to add textfield even outside of the class but it failed totally too.

Just some of starting code to show u exactly what's going on:

package com.shipwreckt;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class Shipwreckt extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_shipwreckt);
        GridBoard gridBoard=new GridBoard(this);

        TextView tv=new TextView(this);
        tv.setText("Problem");
        gridBoard.addView(tv);
        setContentView(gridBoard);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_shipwreckt, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
3
  • I wouldn't reinvent the wheel here, developer.android.com/reference/android/widget/GridLayout.html Commented Feb 17, 2015 at 22:46
  • I want to learn something new and if I would to implement gridlayout I would encounter the same. Commented Feb 17, 2015 at 23:17
  • I have yet to come across a situation where I needed to implement my own layout, even when you do you should be making minimal changes. Layouts are meant to manipulated by other entities not the basis for your code. My advice would be to make a GridBoard object that manages a GridLayout instead of trying to take the place of it. It's better coding practice and a more useful skill to learn. Commented Feb 17, 2015 at 23:23

1 Answer 1

2

I realised that onMesure does more complicated calculations so I modified it :

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    super.onMeasure( (widthMeasureSpec>heightMeasureSpec?heightMeasureSpec:widthMeasureSpec) , (widthMeasureSpec>heightMeasureSpec?heightMeasureSpec:widthMeasureSpec));
}

Now it works perfectly

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.