Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6032459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:20:14+00:00 2026-05-23T05:20:14+00:00

I am new to Android. When i run this code, only one circle is

  • 0

I am new to Android. When i run this code, only one circle is displayed. If i remove view1, then view2 is displayed. but they are never displayed together!!! why is that?
Any help would be appreciated.

thanks

package com.dots;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class Dots1Activity extends Activity
{
    private static final String TAG = "DotsActivity";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        TextView label = new TextView(this);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
             LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        label.setText("Click the circle!");
        CustomDrawableView view1 = new CustomDrawableView(this, 100, 100, 50, Color.RED);
        CustomDrawableView view2 = new CustomDrawableView(this, 200, 200, 25, Color.GREEN);
        CustomDrawableView view3 = new CustomDrawableView(this, 300, 300, 10, Color.WHITE);
        ll.addView(label, layoutParams);
        ll.addView(view1, layoutParams);
        ll.addView(view2, layoutParams);
        ll.addView(view3, layoutParams);
        setContentView(ll);
    }

}

 class CustomDrawableView extends View implements OnClickListener{
     private Context context;
    private int x, y, radius, color;
    public CustomDrawableView(Context context, int x, int y, int radius, int color) {
        super(context);
        this.context = context;
        this.x = x;
        this.y =y;
        this.radius = radius;
        this.color = color;
        setOnClickListener(this);
    }

    protected void onDraw(Canvas canvas) 
    {
        super.onDraw(canvas);
        this.setBackgroundColor(Color.LTGRAY);
        Paint paint = new Paint (Paint.ANTI_ALIAS_FLAG);
        paint.setColor(color);
        canvas.drawCircle(x, y, radius, paint);

    }

    public void onClick(View v) {
        Toast.makeText(this.context, 
                x+"-"+y+"-"+radius, 
                Toast.LENGTH_SHORT).show();  
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:20:15+00:00Added an answer on May 23, 2026 at 5:20 am
    ll.setOrientation(LinearLayout.HORIZONTAL);
    

    They are put next to each other. You can’t see them, because your display isn’t width naught. Put them in a HorizontalScrollView or make them aper VERTICAL.


    I’m not sure if this takes effect here, but i found this on the Android Documentation:

    Note that the framework will not draw
    views that are not in the invalid
    region. To force a view to draw, call
    invalidate().

    Try if this solves your problem (for the moment I guess).


    About your Code

    Something i noticed: The implemented interface for the onClickListener is View.OnClickListener:

    class CustomDrawableView extends View implements View.OnClickListener{ [...] }
    

    How to solve the Problem

    I looked around on the Android Docs and found this. They mantioned the method onMeasure(), which:

    Measure the view and its content to
    determine the measured width and the
    measured height.

    So I added it to your custom CustomDrawableView-class. Unfortuandly, you can’t pass the super.onMeasure()-method simple integers, you’ll need to decode them first, using the View.MeasureSpec-class:

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
            super.onMeasure(View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY),
                    View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY));
        }
    

    In the Example, I set both width and height to 100px. Also, I did some other improvements on your code:

    Dots1Activity-class

    import android.app.Activity;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    public class Dots1Activity extends Activity
    {
        private static final String TAG = "DotsActivity";
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            LinearLayout ll = new LinearLayout(this);
            ll.setOrientation(LinearLayout.VERTICAL);
            TextView label = new TextView(this);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            label.setText("Click the circle!");
            CustomDrawableView view1 = new CustomDrawableView(this, 50, 50, 50, Color.RED);
            CustomDrawableView view2 = new CustomDrawableView(this, 75, 75, 25, Color.GREEN);
            CustomDrawableView view3 = new CustomDrawableView(this, 85, 85, 10, Color.WHITE);
            ll.addView(label, layoutParams);
            ll.addView(view1, layoutParams);
            ll.addView(view2, layoutParams);
            ll.addView(view3, layoutParams);
            setContentView(ll);
        }
    
    }
    

    CustomDrawableView-class

    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.view.View;
    import android.widget.Toast;
    
    class CustomDrawableView extends View implements View.OnClickListener{
         private Context context;
        private int x, y, radius, color;
        public CustomDrawableView(Context context, int x, int y, int radius, int color) {
            super(context);
            this.context = context;
            this.x = x;
            this.y =y;
            this.radius = radius;
            this.color = color;
            setOnClickListener(this);
        }
    
        protected void onDraw(Canvas canvas) 
        {
            super.onDraw(canvas);
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(color);
            canvas.drawCircle(x, y, radius, paint);
        }
    
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
            super.onMeasure(View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY),
                    View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY));
        }
    
        public void onClick(View v) {
            Toast.makeText(this.context, 
                    x+"-"+y+"-"+radius, 
                    Toast.LENGTH_SHORT).show();  
        }
    }
    

    This code compiles, shows all the circles and the onClick-Event works, too.

    Although I have to say it was a bit of a challenge and I’m grateful for it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to record audio this.recorder = new android.media.MediaRecorder(); this.recorder.setAudioSource(android.media.MediaRecorder.AudioSource.MIC); this.recorder.setOutputFormat(android.media.MediaRecorder.OutputFormat.DEFAULT); this.recorder.setAudioEncoder(android.media.MediaRecorder.AudioEncoder.DEFAULT); this.recorder.setOutputFile(pruebaAudioRecorder.mp4); **this.recorder.prepare();**
I know that eclipse can do this, can Intellij via the new Android support
Firstly I am new to android and Java so this is a beginners question.
As we Android developers know, the SQLiteDatabase execSQL method can execute only one statement.
I just started development for Android, and I am stuck with this code: public
The new Android 2.1 SDK (version 7) has a new class called SignalStrength: http://developer.android.com/reference/android/telephony/SignalStrength.html
I'm new to Android dev. There's a very pretty state diagram of the MediaPlayer
I'm new to Android and I think I'm trying to do something really basic:
I am completely new to android, and pretty much a Java newb. I have
I'm pretty new to Android dev and still working out a lot of things.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.