How to add Canvas in a specific LinearLayout?

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

  • Android studio isn't opening on Windows
  • Android SAX parser not getting full text from between tags
  • ANDROID - color detection using openCV - how to?
  • unable to find com.android.camera.CropImage activity in android
  • Android: How to get background color of Activity in Java?
  • How to make an email address clickable?
  • 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);
               ...................................
               ................................
    
    
    
        }
    }
    
    

    Related posts:

    what is the fastest FFT library for iOS/Android ARM devices?
    ambiguous behavior of new line character
    Pixel Perfect Collision detection between a custom view and an ImageView
    Transfer data through audio jack cable over two Android devices
    What do I have to add to the manifest to debug an Android application on an actual Device?
    Get total “Areas/Zones” in which I am currently in, based on Current Location
  • How to get APK signing signature?
  • Indeterminate Horizontal ProgressBar BELOW ActionBar using AppCompat?
  • ViewPager + FragmentStatePagerAdapter + orientation change
  • Is there a way to define a min and max value for EditText in Android?
  • APKs supporting Android Wear must have a minimum SDK version of at least 23
  • get email type and phone number type 's label used android
  • One Solution collect form web for “How to add Canvas in a specific LinearLayout?”

    Here is what I did to include your view.

    Start with adding a new layout to your xml file, then you can retrieve that, and then you can add to it like this:

        DrawCanvasCircle pcc = new DrawCanvasCircle (this);
        Bitmap result = Bitmap.createBitmap(25, 25, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(result);
        pcc.draw(canvas);
        pcc.setLayoutParams(new LayoutParams(25, 25));
        mControls.addView(pcc);
    

    In this example mControls is a layout that is added to the main activity layout.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.