0

I've tried to code a custom blue square Component so that

  • width or height is the parent size whenever we tell to fill parent, in the parent layout (let's say res/layout/main.xml)
  • width or height is 30 px (or is 30 dpi better ?) whenever we tell to wrap content in the parent layout.

For now, thanks to rajesh.edi, I managed to paint a blue square : but the size is 30px * 30px in both cases (Fill parent or wrap content). So is there a way to detect the mode "wrap content" and the mode "fill parent", so that I can adapt the size ?

Here is my attempt for the component (I called it BoardView, because this blue square is a preleminary step before making a Chess Board component) :

package com.gmail.bernabe.laurent.android.simple_chess_board.views;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;

public class BoardView extends View {


    public BoardView(Context context) {
        super(context);
        setBackgroundColor(Color.BLUE);
    }



    public BoardView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }



    public BoardView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);

        int newWidth, newHeight;

        if (widthSpecMode == MeasureSpec.EXACTLY)
            newWidth = 30;
        else
            newWidth = MeasureSpec.getSize(widthMeasureSpec);

        if (heightSpecMode == MeasureSpec.EXACTLY)
            newHeight = 30;
        else
            newHeight = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(newWidth, newHeight);
    }

    @Override
protected void onDraw(Canvas canvas) {
    Rect rect = new Rect(0, 0, getWidth(), getHeight());
        Paint paint = new Paint();
        paint.setColor(Color.BLUE); 
        canvas.drawRect(rect, paint);
    }


}

and here is my main.xml

<com.gmail.bernabe.laurent.android.simple_chess_board.views.BoardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/boardView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>

I was sure that the error didn't come from the fact I did not override View.onDraw() : but I was wrong. Why ? Why setting background color to blue is not enough ?

Thanks in advance if someone can help me.

loloof64
  • 5,252
  • 12
  • 41
  • 78
  • Android API 3 is called Cupcake is the version 1.5. See also the [Platform Versions](http://developer.android.com/resources/dashboard/platform-versions.html). – rekire Feb 16 '12 at 10:41
  • brother, i think the plan view doesnt contains any thing if you try to draw some thing on the canvas by getting the width and height property you can see some thing with out that on the view nothing will be there – Triode Feb 16 '12 at 10:42
  • @rekire Thank you. Apologizes for the error. – loloof64 Feb 16 '12 at 10:53
  • @rajesh.adhi Thank you : I've gone a bit further. See my edit message. – loloof64 Feb 16 '12 at 11:03

1 Answers1

0

Finally I managed Whenever the user set to wrap_content, I get AT_MOST as mode.

 package com.gmail.bernabe.laurent.android.simple_chess_board.views;

 import android.content.Context;
 import android.graphics.Canvas;
 import android.graphics.Color;
 import android.graphics.Paint;
 import android.graphics.Rect;
 import android.util.AttributeSet;
 import android.view.View;

 public class BoardView extends View {


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



    public BoardView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }     



    public BoardView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }



    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);

        int widthMeasure = MeasureSpec.getSize(widthMeasureSpec);
        int heightMeasure = MeasureSpec.getSize(heightMeasureSpec);

        int newWidth, newHeight;

        if (widthSpecMode == MeasureSpec.AT_MOST)
            newWidth = 30;
        else
            newWidth = widthMeasure;

        if (heightSpecMode == MeasureSpec.AT_MOST)
            newHeight = 30;
        else
            newHeight = heightMeasure;

        setMeasuredDimension(newWidth, newHeight);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        Rect rect = new Rect(0, 0, getWidth(), getHeight());
        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        canvas.drawRect(rect, paint);
    }



 }
loloof64
  • 5,252
  • 12
  • 41
  • 78