Hello all,

I am trying an android application in android 2.2 using eclipse.

Am trying to draw using canvas in a single screen with two buttons at
the bottom of the screen.
I received no errors. But I need to draw an appropriate image by
clicking on the appropriate button.
If I run my code, it is executed but the image is drawn in a separate
screen. I need both the image and
the buttons in the same screen. Please help me to get it.

Here is my code:

DrawActivity.java
=============

package draw.tab;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class DrawActivity extends Activity implements OnClickListener
{

                //private DrawActivity draw;
                DrawView drawview;
                CircleView circleView;

                Button square,circle;

    /** Called when the activity is first created. */

        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        drawview=new DrawView(this);
        circleView=new CircleView(this);

        square=(Button)findViewById(R.id.buttonTest);
        square.setOnClickListener(this);
        circle=(Button)findViewById(R.id.circleButton);
        circle.setOnClickListener(this);
        }

        public void onClick(View v)
        {
            switch (v.getId()) {
            case R.id.circleButton:
                circleView.setBackgroundColor(Color.rgb(40,100,20));
                circleView.findViewById(R.id.CircleViewId);
                setContentView(circleView);

                break;

            case R.id.buttonTest:
                drawview.setBackgroundColor(Color.WHITE);
                drawview.findViewById(R.id.DrawViewId);
                setContentView(drawview);

                break;
            }
        }
}

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - -  - - - - - - - - - - - - - - - - - - - - - -

CircleView.java           //To draw circle
============

package draw.tab;

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

public class CircleView extends View{


        Paint p=new Paint();

        public CircleView(Context context)
        {
                super(context);
                p.setColor(Color.BLUE);
                p.setStyle(Style.STROKE);

        }

        public CircleView(Context con,AttributeSet atts)
        {
                super(con,atts);

        }

        public void onDraw(Canvas c)
        {
                c.drawLine(20,40,450,40, p);//horizontal top
                c.drawCircle(250,350,100,p);
                c.drawLine(20,40,20,500,p);//vertical left
                c.drawLine(20,500,450,500,p);//horizontal bottom
                c.drawLine(450,40,450,500,p);//vertical right
        }

}

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - -

DrawView.java                          // To draw Square
============

package draw.tab;

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

public class DrawView extends View{
        Paint paint=new Paint();
        public DrawView(Context context) {
        super(context);
        paint.setColor(Color.RED);
        paint.setStyle(Style.STROKE);

    }
        //Rect rec =new Rect();
        public DrawView(Context con,AttributeSet atts)
        {
                super(con,atts);

        }

    @Override
    public void onDraw(Canvas canvas) {
            canvas.drawLine(20,40,450,40,paint);          //horizontal
top
            canvas.drawRect(70,140,400,500,paint);

            canvas.drawLine(20,40,20,500,paint);            //vertical
left
                canvas.drawLine(20,700,450,500,paint);   //horizontal bottom
                canvas.drawLine(450,40,450,500,paint);   //vertical right


    }


}

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Main.xml
=======

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android";
        android:visibility="visible"

        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <draw.tab.DrawView
    android:id="@+id/DrawViewId"
    android:layout_width="320dp"
    android:layout_height="500dp">
    </draw.tab.DrawView>

    <draw.tab.CircleView
    android:id="@+id/CircleViewId"
    android:layout_width="320dp"
    android:layout_height="500dp">
    </draw.tab.CircleView>

<LinearLayout

        android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:gravity="bottom">


         <Button
            android:layout_height="wrap_content"
            android:text="Circle"
            android:id="@+id/circleButton"
            android:layout_width="160dp"
            >
    </Button>

    <Button
            android:layout_height="wrap_content"
            android:text="Square"
            android:id="@+id/buttonTest"
            android:layout_width="160dp">
    </Button>




</LinearLayout>
</FrameLayout>

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - -  - - - - -

The two classes  CircleView.java and DrawView.java are to draw circle
and square
respectively.

Please help me to draw in the same screen by clicking on the buttons
at the bottom of the screen.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to