Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>ll.setOrientation(LinearLayout.HORIZONTAL); </code></pre> <p>They are put next to each other. You can't see them, because your display isn't width naught. Put them in a <a href="http://developer.android.com/reference/android/widget/HorizontalScrollView.html" rel="nofollow">HorizontalScrollView</a> or make them aper <code>VERTICAL</code>.</p> <hr> <p>I'm not sure if this takes effect here, but i found this on the <a href="http://developer.android.com/reference/android/view/View.html#Drawing" rel="nofollow">Android Documentation</a>:</p> <blockquote> <p>Note that the framework will not draw views that are not in the invalid region. To force a view to draw, call invalidate().</p> </blockquote> <p><strike>Try if this solves your problem (for the moment I guess).</strike></p> <hr> <h2>About your Code</h2> <p>Something i noticed: The implemented interface for the <code>onClickListener</code> is <code>View.OnClickListener</code>:</p> <pre><code>class CustomDrawableView extends View implements View.OnClickListener{ [...] } </code></pre> <h2>How to solve the Problem</h2> <p>I looked around on the Android Docs and found <a href="http://developer.android.com/guide/topics/ui/custom-components.html#custom" rel="nofollow">this</a>. They mantioned the method <a href="http://developer.android.com/reference/android/view/View.html#onMeasure%28int,%20int%29" rel="nofollow">onMeasure()</a>, which:</p> <blockquote> <p>Measure the view and its content to determine the measured width and the measured height.</p> </blockquote> <p>So I added it to your custom <code>CustomDrawableView</code>-class. Unfortuandly, you can't pass the <code>super.onMeasure()</code>-method simple integers, you'll need to decode them first, using the <a href="http://developer.android.com/reference/android/view/View.MeasureSpec.html" rel="nofollow">View.MeasureSpec</a>-class:</p> <pre><code>protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ super.onMeasure(View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY)); } </code></pre> <p>In the Example, I set both width and height to 100px. Also, I did some other improvements on your code:</p> <h3>Dots1Activity-class</h3> <pre><code>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); } } </code></pre> <h3>CustomDrawableView-class</h3> <pre><code>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(); } } </code></pre> <p>This code compiles, shows all the circles and the onClick-Event works, too.</p> <p>Although I have to say it was a bit of a challenge and I'm grateful for it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload