To draw different geometric figure based on user option?
<p>In my program user is asked for three options to draw a figure</p>

<p>Rectangle
Square
Circle</p>

<p>based on option the figure is drawn on canvas.</p>

<p>My porblem is that the figure is not displayed on canvas.</p>

<p>I cannot figure-out what is wrong and why the canvas is not drawing anything.</p>

<pre><code>DrawCanvas.java

public class DrawCanvas extends View {

    private Paint bluePaint;
    private String figure;
    private Rect rect;

    public DrawCanvas(Context context, AttributeSet attrs) {
        super(context, attrs);
        rect = new Rect(150,200,150,200);
        bluePaint = new Paint(Color.BLUE);
        bluePaint.setStyle(Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if("Rectangle".equalsIgnoreCase(getFigure())) {
            canvas.drawRect(rect, bluePaint);
        }else if("Square".equalsIgnoreCase(getFigure())) {
            canvas.drawRect(rect, bluePaint);
        }else if("Circle".equalsIgnoreCase(getFigure())) {
            canvas.drawCircle(150, 150, 25, bluePaint);
        }
    }

    public String getFigure() {
        return figure;
    }

    public void setFigure(String figure) {
        this.figure = figure;
    }

}
</code></pre>

<p>In XML file i have included the custom view</p>

<p>XML file: activity_demo_app.xml</p>

<pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" &gt;

    &lt;com.example.demoapp.DrawCanvas
        android:id="@+id/drawCnavas"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/spinner1" /&gt;

    &lt;Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:entries="@array/drawingOption" /&gt;

&lt;/RelativeLayout&gt;
</code></pre>

<p>the main activity class in which this layout is set is </p>

<p>DemoAppActivity.java</p>

<pre><code>public class DemoAppActivity extends Activity {

    DrawCanvas drawCanvas;
    Bitmap bitmap;
    Canvas canvas;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo_app);
        drawCanvas = (DrawCanvas) findViewById(R.id.drawCnavas);
        drawCanvas.setBackgroundColor(Color.YELLOW);
        bitmap = Bitmap.createBitmap(480, 250, Config.RGB_565);
        canvas = new Canvas(bitmap);
        Spinner drawingOptionsSpinner = (Spinner) findViewById(R.id.spinner1);
        drawingOptionsSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView&lt;?&gt; arg0, View view,
                    int arg2, long arg3) {
                final TextView textView = (TextView) view;
                if(!"None".equalsIgnoreCase(textView.getText().toString())) {
                    if("Rectangle".equalsIgnoreCase(textView.getText().toString())) {
                        drawCanvas.setFigure(textView.getText().toString());
                        drawCanvas.draw(canvas);
                    }else if("Square".equalsIgnoreCase(textView.getText().toString())) {
                        drawCanvas.setFigure(textView.getText().toString());
                        drawCanvas.draw(canvas);
                    }else{
                        drawCanvas.setFigure(textView.getText().toString());
                        drawCanvas.draw(canvas);
                    }
                }
            }

            @Override
            public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
                // TODO Auto-generated method stub

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_demo_app, menu);
        return true;
    }


}
</code></pre>

<p>There is no compile time error or any run time error.
But still the canvas does not draw the figures.</p>

<p>Can any bode help me on this.</p>

<p>Thanks and regards,
Allwyn Joseph.</p>
