I am new comer to android but not to java. I have been designing UI in android through XML file, in that page i have 3 linear Layouts, in my top layout(first LinearLayout) i have kept some image and in the last layout i have kept some buttons,now i need to place a circle(of red color) in my center layout using canvas,i have written a separate class that extends View where in onDraw(Canvas canvas) ,i have drawn a circle.
package com.project.TargetTrackr3; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.View; public class DrawCanvasCircle extends View{ public DrawCanvasCircle(Context mContext) { super(mContext); } public void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); canvas.drawColor(Color.WHITE); paint.setColor(Color.BLUE); canvas.drawCircle(20, 20, 15, paint); } }
Now i have to bring this canvas to the second layout,my main.xml is shown below
package com.project.TargetTrackr3; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.View; import android.widget.LinearLayout; public class TargetTrackr3Activity extends Activity { /** Called when the activity is first created. */ protected LinearLayout ll; DrawCanvasCircle c; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main1); //layouting file ll = (LinearLayout) findViewById(R.id.LinearLayout_DrawCircle);//This is where i have to bring the canvas c = new DrawCanvasCircle(this); ................................... ................................ } }