Take the 2-minute tour ×

I have a custom view and want to add one more custom view on that custom view. This is my custom view class:

public class CustomCircle extends View{

float radius;
Paint paint = new Paint();

String message = "";
public CustomCircle(Context context, AttributeSet attrs) {
    super(context, attrs);


protected void onDraw(Canvas canvas) {
    canvas.drawCircle(getWidth()/2, getHeight()/2,radius, paint);

public boolean onTouchEvent(MotionEvent event) {
    boolean isClickInCircle = false;
    float x = event.getX();
    float y = event.getY();

    double check = Math.sqrt((x-getWidth()/2)*(x-getWidth()/2) + (y-getHeight()/2)*(y-getHeight()/2));
    if (check<=radius) {
        isClickInCircle= true;
    if (isClickInCircle) {

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            Toast.makeText(getContext(),message, Toast.LENGTH_LONG).show();
            return true;

        case MotionEvent.ACTION_UP:
            return true;


    return false;

and am using another class that extends LinearLayout:

public class B extends LinearLayout {

private Paint paint;

public B(Context context) {
    paint = new Paint();

public void addCircle() {

    CustomCircle circleBlue = new CustomCircle(getContext(), null);
    circleBlue.radius = 160;
    circleBlue.message = "Clicked";

    CustomCircle circleRed = new CustomCircle(getContext(), null);
    circleRed.radius = 80;
    circleRed.message = "Clicked";


and I'm calling the B class from the main activity class using :

public void onCreate(Bundle savedInstanceState) {
    B root = new B(this);

The output is showing me only one circle instead of a circle inside another circle. What's wrong with my code?

share|improve this question
add comment

2 Answers

And the Output is showing me only One circle instead of circle inside circle.

You picked the wrong layout if you want to overlap children, a RelativeLayout or FrameLayout is the way to go. Also, regarding your code:

public class B extends RelativeLayout {
public void addCircle() {

    // the constructor that uses the AttributeSet should be added if you use the 
    // custom component in the xml layout
    CustomCircle circleBlue = new CustomCircle(getContext());
    // ...
    // add it with LayoutParams
    RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
    addView(circleBlue, rlp);
    // the same for the other view

Also your two circle will have the same dimensions so they will overlap perfectly(and you'll not be able to see them), you would need to give them different dimensions, through the LayoutParams, for example:

RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(200, 200);

for the first one and:

RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(100, 100);

for the second one.

share|improve this answer
add comment

Is it possible that there are 2 circles in the layout but you can see only one because it has the same color as the first circle and they are laying on top of each other?

change circleRed.paint.setColor(Color.WHITE); to circleRed.paint.setColor(Color.BLACK); and see what it gives

share|improve this answer
Yes i changed circleRed.paint.setColor(Color.BLACK); Still its displaying me one circle. –  Vizzz Oct 3 '12 at 11:33
than i would suggest looking at @Luksprog answer –  Lars Werkman Oct 3 '12 at 11:41
So swap the colours. What do you see now? If the colour of the circle you see is also swapped, then you have 2 circles exactly overlaying each other. –  Simon Oct 3 '12 at 11:51
add comment

Your Answer


