Search code examples
android-layoutandroid-custom-viewandroidandroid-gridlayout

How to add more than one same custom view to a grid layout in android from Java code


I am learning custom views in android. I made one custom view, with a rectangle and a text. The code of the custom view is:

    public class TileGenerator extends View {

    // rectangle parameters
    private Rect rect = new Rect();
    private Paint rectPaint = new Paint();

    private Integer rectEndX;
    private Integer rectEndY;
    private Integer rectStartX;
    private Integer rectStartY;
    private Integer rectFillColor;


    private Float rectTextStartX;
    private Float rectTextStartY;

    //rectangle text
    private String rectText;
    private Paint rectTextPaint = new Paint();

    public TileGenerator(Context context) {
        super(context);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void setTileTitleText(String rectText) {
        this.rectText = rectText;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        rectEndX = getrectEndX();
        rectEndY = getrectEndY();
        rectStartX = getRectStartX();
        rectStartY = getRectStartY();

        rectTextStartX = rectEndX/4f + rectStartX;
        rectTextStartY = 3.5f * rectEndY/4f + rectStartY;

        rectTextPaint.setTextSize(rectEndY/8);
        rectTextPaint.setColor(Color.BLACK);

        rect.set(rectStartX,rectStartY,rectEndX,rectEndY);
        rectPaint.setColor(getRectFillColor());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawRect(rect, rectPaint);
        canvas.drawText(rectText,rectTextStartX,rectTextStartY,rectTextPaint );
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    public Integer getrectEndX() {
        return rectEndX;
    }

    public void setrectEndX(Integer rectEndX) {
        this.rectEndX = rectEndX;
    }

    public Integer getrectEndY() {
        return rectEndY;
    }

    public void setrectEndY(Integer rectEndY) {
        this.rectEndY = rectEndY;
    }

    public Integer getRectStartX() {
        return rectStartX;
    }

    public void setRectStartX(Integer rectStartX) {
        this.rectStartX = rectStartX;
    }

    public Integer getRectStartY() {
        return rectStartY;
    }

    public void setRectStartY(Integer rectStartY) {
        this.rectStartY = rectStartY;
    }

    public Integer getRectFillColor() {
        return rectFillColor;
    }

    public void setRectFillColor(Integer rectFillColor) {
        this.rectFillColor = rectFillColor;
    }

    public String getRectText() {
        return rectText;
    }
}

After that I created an blank activity. I am doing all with JAVA code. No XML. Then I try to add above custom view to a gridview layout. I want to add two custom views with different text in a horizontal gridview. So far my code is as below:

        @Override
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    GridLayout gridLayout = new GridLayout(this);

    // first custom view        
    CustomRectWithText customRectWithText = new CustomRectWithText(this);
    customRectWithText.setRectEndX(200);
    customRectWithText.setRectEndY(200);

    customRectWithText.setRectStartX(2);
    customRectWithText.setRectStartY(2);

    customRectWithText.setImage(image);

    customRectWithText.setRectText("Text");

    customRectWithText.setRectFillColor(Color.BLUE);

    gridLayout.addView(customRectWithText);

    // second custom view
    CustomRectWithText customRectWithText1 = new CustomRectWithText(this);
    customRectWithText1.setRectEndX(400);
    customRectWithText1.setRectEndY(200);

    customRectWithText1.setRectStartX(200 + 5);
    customRectWithText1.setRectStartY(2);

    customRectWithText1.setTileTitleText("Text 1");

    customRectWithText1.setRectFillColor(Color.GREEN);

    gridLayout.addView(customRectWithText1);
    setContentView(gridLayout);
    }

But still I am not getting both of the rectangles in a grid view. Only one rectangle is displayed at a time. In above case only first custom view is displayed.

Where am I doing wrong. All I want is to make a repetitive rectangle of varying labels of any size inside a grid view. Is this the way to do it. I mean is there any other way around.

  • I dont want to use ListItems.

Solution

  • Sorry but i do not have enough repo to comment. But why dont you make an adapter? Gridview behaves same as listView. Use adapter to fill your grid. This is the proper way to populate listView and gridView also.